Example usage for javax.swing ButtonGroup ButtonGroup

List of usage examples for javax.swing ButtonGroup ButtonGroup

Introduction

In this page you can find the example usage for javax.swing ButtonGroup ButtonGroup.

Prototype

public ButtonGroup() 

Source Link

Document

Creates a new ButtonGroup.

Usage

From source file:AddingDropDownMenus.java

public AddingDropDownMenus() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setJMenuBar(menuBar);/*from   w w  w.j a  va2  s  .  co  m*/
    JMenu fileMenu = new JMenu("File");
    JMenu elementMenu = new JMenu("Elements");
    newItem = fileMenu.add("New");
    openItem = fileMenu.add("Open");
    closeItem = fileMenu.add("Close");
    fileMenu.addSeparator();
    saveItem = fileMenu.add("Save");
    saveAsItem = fileMenu.add("Save As...");
    fileMenu.addSeparator();
    printItem = fileMenu.add("Print");
    elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
    elementMenu.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
    elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
    ButtonGroup types = new ButtonGroup();
    types.add(lineItem);
    types.add(rectangleItem);
    types.add(circleItem);
    elementMenu.addSeparator();
    elementMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
    elementMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
    menuBar.add(fileMenu);
    menuBar.add(elementMenu);
}

From source file:IsEDTExample.java

public IsEDTExample() {
    JTable table = new JTable(tableModel);
    table.setRowHeight(100);/* w  ww.  jav a  2 s  . c o m*/
    table.setDefaultRenderer(Object.class, new ColorRenderer());
    add(table);

    add(new JLabel("Thread Color Shade:"));
    ButtonGroup group = new ButtonGroup();
    JRadioButton redOption = new JRadioButton("Red");
    group.add(redOption);
    redOption.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            threadShade = RED;
        }
    });

    JRadioButton blueOption = new JRadioButton("Blue");
    group.add(blueOption);
    blueOption.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            threadShade = BLUE;
        }
    });

    JRadioButton greenOption = new JRadioButton("Green");
    group.add(greenOption);
    greenOption.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            threadShade = GREEN;
        }
    });

    redOption.setSelected(true);
    this.threadShade = RED;

    add(redOption);
    add(greenOption);
    add(blueOption);

    add(new JButton(new RandomColorAction()));

    this.keepRunning = true;
    this.colorShadeThread = new Thread(new RandomColorShadeRunnable());
    this.colorShadeThread.start();
}

From source file:MessageDigestTest.java

public MessageDigestFrame() {
    setTitle("MessageDigestTest");
    setSize(400, 200);/*  www .j  a v  a2s.c  o m*/
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    JPanel panel = new JPanel();
    ButtonGroup group = new ButtonGroup();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            JCheckBox b = (JCheckBox) event.getSource();
            setAlgorithm(b.getText());
        }
    };
    addCheckBox(panel, "SHA-1", group, true, listener);
    addCheckBox(panel, "MD5", group, false, listener);

    Container contentPane = getContentPane();

    contentPane.add(panel, "North");
    contentPane.add(new JScrollPane(message), "Center");
    contentPane.add(digest, "South");
    digest.setFont(new Font("Monospaced", Font.PLAIN, 12));

    setAlgorithm("SHA-1");

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    JMenuItem fileDigestItem = new JMenuItem("File digest");
    fileDigestItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            loadFile();
        }
    });
    menu.add(fileDigestItem);
    JMenuItem textDigestItem = new JMenuItem("Text area digest");
    textDigestItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            String m = message.getText();
            computeDigest(m.getBytes());
        }
    });
    menu.add(textDigestItem);
    menuBar.add(menu);
    setJMenuBar(menuBar);
}

From source file:Main.java

public Main() {
    JPanel northPanel = new JPanel();
    northPanel.setLayout(new GridLayout(3, 1, 0, 5));
    northPanel.add(label);// www.j  ava2  s.c o m
    northPanel.add(button);
    northPanel.add(comboBox);
    add(northPanel, BorderLayout.NORTH);
    JPanel southPanel = new JPanel();

    ItemHandler handler = new ItemHandler();
    southPanel.setLayout(new GridLayout(1, radio.length));

    ButtonGroup group = new ButtonGroup();
    for (int i = 0; i < radio.length; i++) {
        radio[i] = new JRadioButton(strings[i]);
        radio[i].addItemListener(handler);
        group.add(radio[i]);
        southPanel.add(radio[i]);
    }

    add(southPanel, BorderLayout.SOUTH);

    setSize(300, 200);
    setVisible(true);
    radio[0].setSelected(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:ToggleButtonCheckBoxRadioButton.java

public TogglePanel() {
    JToggleButton tog = new JToggleButton("Toggle");
    ItemListener listener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            AbstractButton src = (AbstractButton) (e.getSource());
            System.out.println("Toggle: " + src.getText());
        }/* w w  w  .j  a  v  a  2  s . c o  m*/
    };
    tog.addItemListener(listener);
    add(tog);

    JCheckBox cbox = new JCheckBox("Checkbox");
    cbox.addItemListener(listener);
    add(cbox);
    ButtonGroup btngroup = new ButtonGroup();
    for (int i = 1; i <= 3; i++) {
        JRadioButton radio = new JRadioButton("Radio " + i);
        btngroup.add(radio);
        radio.addItemListener(listener);
        add(radio);
    }
}

From source file:Wallpaper.java

private static JPanel createPanel() {
    JPanel p = new JPanel();

    ButtonGroup entreeGroup = new ButtonGroup();
    JRadioButton radioButton;//from   ww w . j  a v  a  2  s  . c  om
    p.add(radioButton = new JRadioButton("Beef", true));
    entreeGroup.add(radioButton);
    p.add(radioButton = new JRadioButton("Chicken"));
    entreeGroup.add(radioButton);
    p.add(radioButton = new JRadioButton("Vegetable"));
    entreeGroup.add(radioButton);

    p.add(new JCheckBox("Ketchup"));
    p.add(new JCheckBox("Mustard"));
    p.add(new JCheckBox("Pickles"));

    p.add(new JLabel("Special requests:"));
    p.add(new JTextField(20));

    JButton orderButton = new JButton("Place Order");
    p.add(orderButton);

    return p;
}

From source file:SimpleBufferedImageDemo.java

public SimpleBufferedImageDemo() {
    super();//from www.jav  a 2  s . com
    Container container = getContentPane();

    canvas = new DisplayCanvas();
    container.add(canvas);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(2, 2));
    panel.setBorder(new TitledBorder("Select an Option and Display Image..."));

    buffButton = new JRadioButton("Buffered");
    buffButton.addActionListener(new ButtonListener());
    nonBuffButton = new JRadioButton("Non-Buffered", true);
    nonBuffButton.addActionListener(new ButtonListener());
    ButtonGroup group = new ButtonGroup();
    group.add(buffButton);
    group.add(nonBuffButton);

    displayButton = new JButton("Display");
    displayButton.addActionListener(new ButtonListener());
    clearButton = new JButton("Clear");
    clearButton.addActionListener(new ButtonListener());

    panel.add(nonBuffButton);
    panel.add(buffButton);
    panel.add(displayButton);
    panel.add(clearButton);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowEventHandler());
    pack();
    setVisible(true);
}

From source file:LookAndFeelPrefs.java

/**
 * Create a menu of radio buttons listing the available Look and Feels. When
 * the user selects one, change the component hierarchy under frame to the new
 * LAF, and store the new selection as the current preference for the package
 * containing class c.//from   ww w. ja  va  2 s  .  co m
 */
public static JMenu createLookAndFeelMenu(final Class prefsClass, final ActionListener listener) {
    // Create the menu
    final JMenu plafmenu = new JMenu("Look and Feel");

    // Create an object used for radio button mutual exclusion
    ButtonGroup radiogroup = new ButtonGroup();

    // Look up the available look and feels
    UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();

    // Find out which one is currently used
    String currentLAFName = UIManager.getLookAndFeel().getClass().getName();

    // Loop through the plafs, and add a menu item for each one
    for (int i = 0; i < plafs.length; i++) {
        String plafName = plafs[i].getName();
        final String plafClassName = plafs[i].getClassName();

        // Create the menu item
        final JMenuItem item = plafmenu.add(new JRadioButtonMenuItem(plafName));
        item.setSelected(plafClassName.equals(currentLAFName));

        // Tell the menu item what to do when it is selected
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                // Set the new look and feel
                try {
                    UIManager.setLookAndFeel(plafClassName);
                } catch (UnsupportedLookAndFeelException e) {
                    // Sometimes a Look-and-Feel is installed but not
                    // supported, as in the Windows LaF on Linux platforms.
                    JOptionPane.showMessageDialog(plafmenu,
                            "The selected Look-and-Feel is " + "not supported on this platform.",
                            "Unsupported Look And Feel", JOptionPane.ERROR_MESSAGE);
                    item.setEnabled(false);
                } catch (Exception e) { // ClassNotFound or Instantiation
                    item.setEnabled(false); // shouldn't happen
                }

                // Make the selection persistent by storing it in prefs.
                Preferences p = Preferences.userNodeForPackage(prefsClass);
                p.put(PREF_NAME, plafClassName);

                // Invoke the supplied action listener so the calling
                // application can update its components to the new LAF
                // Reuse the event that was passed here.
                listener.actionPerformed(event);
            }
        });

        // Only allow one menu item to be selected at once
        radiogroup.add(item);
    }

    return plafmenu;
}

From source file:BoxLayoutTest.java

public BoxLayoutTest() {
    setTitle("BoxLayoutTest");
    setSize(300, 300);/*from   w w  w  .ja  v  a 2  s. com*/
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    horizontalBox = createBox(true, false);
    verticalBox = createBox(false, false);
    horizontalStrutsAndGlueBox = createBox(true, true);
    verticalStrutsAndGlueBox = createBox(false, true);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(3, 1, 3, 3));
    ButtonGroup directionGroup = new ButtonGroup();

    horizontalButton = addRadioButton(panel, directionGroup, "Horizontal", true);
    verticalButton = addRadioButton(panel, directionGroup, "Vertical", false);
    strutsAndGlueCheckBox = addCheckBox(panel, "Struts and Glue");

    Container contentPane = getContentPane();
    contentPane.add(panel, "South");
    contentPane.add(horizontalBox, "Center");
    currentBox = horizontalBox;
}

From source file:ClipArea.java

public ClipArea() {
    super();/*  w ww  .  j  ava  2 s.  co m*/
    Container contentPane = getContentPane();

    canvas = new MyCanvas();
    contentPane.add(canvas);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1, 2));
    clipButton = new JRadioButton("Clip", true);
    clipButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            canvas.clip = true;
            canvas.clipFurther = false;
            canvas.repaint();
        }
    });
    clipFurButton = new JRadioButton("Clip Further");
    clipFurButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            canvas.clipFurther = true;
            canvas.repaint();
        }
    });
    ButtonGroup group = new ButtonGroup();
    group.add(clipButton);
    group.add(clipFurButton);

    panel.add(clipButton);
    panel.add(clipFurButton);
    contentPane.add(BorderLayout.SOUTH, panel);

    // 4. Add a window listener to close the frame properly.
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    pack();
    setVisible(true);
}