Example usage for javax.swing ButtonGroup getButtonCount

List of usage examples for javax.swing ButtonGroup getButtonCount

Introduction

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

Prototype

public int getButtonCount() 

Source Link

Document

Returns the number of buttons in the group.

Usage

From source file:Main.java

public static int getButtonGroupSelectIndex(ButtonGroup bg) {
    if (bg.getButtonCount() <= 0) {
        return -1;
    }/*  ww  w  .  j  a v  a2s. c o m*/
    Enumeration<AbstractButton> bEnum = bg.getElements();
    for (int i = 0; bEnum.hasMoreElements(); i++) {
        AbstractButton temp = bEnum.nextElement();
        if (temp.isSelected()) {
            return i;
        }

    }
    return -1;
}

From source file:StrokeTest.java

/**
 * Makes a radio button to change the cap style.
 * @param label the button label/*from  www.  j  ava2 s  . c  o  m*/
 * @param style the cap style
 * @param group the radio button group
 */
private void makeCapButton(String label, final int style, ButtonGroup group) {
    // select first button in group
    boolean selected = group.getButtonCount() == 0;
    JRadioButton button = new JRadioButton(label, selected);
    buttonPanel.add(button);
    group.add(button);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            canvas.setCap(style);
        }
    });
}

From source file:StrokeTest.java

/**
 * Makes a radio button to change the join style.
 * @param label the button label//from   www .j  ava 2s  .  c o m
 * @param style the join style
 * @param group the radio button group
 */
private void makeJoinButton(String label, final int style, ButtonGroup group) {
    // select first button in group
    boolean selected = group.getButtonCount() == 0;
    JRadioButton button = new JRadioButton(label, selected);
    buttonPanel.add(button);
    group.add(button);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            canvas.setJoin(style);
        }
    });
}

From source file:StrokeTest.java

/**
 * Makes a radio button to set solid or dashed lines
 * @param label the button label//from   w ww. j  a va  2 s. co  m
 * @param style false for solid, true for dashed lines
 * @param group the radio button group
 */
private void makeDashButton(String label, final boolean style, ButtonGroup group) {
    // select first button in group
    boolean selected = group.getButtonCount() == 0;
    JRadioButton button = new JRadioButton(label, selected);
    buttonPanel.add(button);
    group.add(button);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            canvas.setDash(style);
        }
    });
}

From source file:Main.java

public Main() {
    JRadioButton radMarriedYes = new JRadioButton("Yes?", true);
    JRadioButton radMarriedNo = new JRadioButton("No?", false);
    JRadioButton radGolfYes = new JRadioButton("Yes?", false);
    JRadioButton radGolfNo = new JRadioButton("No?", true);

    ButtonGroup radioGroup1 = new ButtonGroup();
    ButtonGroup radioGroup2 = new ButtonGroup();

    setLayout(null);/*from  w ww  .j  av  a 2  s  . c  om*/

    add(radMarriedYes);
    add(radMarriedNo);
    add(radGolfYes);
    add(radGolfNo);

    radioGroup1.add(radMarriedYes);
    radioGroup1.add(radMarriedNo);
    System.out.println(radioGroup1.getButtonCount());

    radioGroup2.add(radGolfYes);
    radioGroup2.add(radGolfNo);

    radMarriedYes.setBounds(30, 50, 50, 20);
    radMarriedNo.setBounds(30, 80, 50, 20);

    radGolfYes.setBounds(150, 50, 50, 20);
    radGolfNo.setBounds(150, 80, 50, 20);
}

From source file:MessageDigestTest.java

/**
 * Adds a radio button to select an algorithm.
 * @param c the container into which to place the button
 * @param name the algorithm name/*from   ww w . j  a  v a 2s.c om*/
 * @param g the button group
 */
public void addRadioButton(Container c, final String name, ButtonGroup g) {
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            setAlgorithm(name);
        }
    };
    JRadioButton b = new JRadioButton(name, g.getButtonCount() == 0);
    c.add(b);
    g.add(b);
    b.addActionListener(listener);
}

From source file:com.diversityarrays.kdxplore.trials.SampleGroupExportDialog.java

public SampleGroupExportDialog(Window owner, String title, Trial trial, KdxploreDatabase kdxploreDatabase,
        DeviceType deviceType, DeviceIdentifier devid, SampleGroup sampleGroup,
        Set<Integer> excludeTheseTraitIds, Map<Integer, Trait> allTraitIds, Set<Integer> excludeThesePlotIds) {
    super(owner, title, ModalityType.APPLICATION_MODAL);

    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    setGlassPane(backgroundRunner.getBlockingPane());

    this.allTraits = allTraitIds;
    this.trial = trial;
    this.kdxploreDatabase = kdxploreDatabase;
    this.sampleGroup = sampleGroup;
    this.excludeTheseTraitIds = excludeTheseTraitIds;
    this.excludeThesePlotIds = excludeThesePlotIds;

    String deviceName = devid == null ? "Unknown_" + deviceType.name() : devid.getDeviceName();

    if (DeviceType.FOR_SCORING.equals(deviceType)) {
        if (!Check.isEmpty(sampleGroup.getOperatorName())) {
            deviceName = sampleGroup.getOperatorName();
        }//  w ww  . ja v  a 2s . c  o  m
    }

    File directory = KdxplorePreferences.getInstance().getOutputDirectory();
    if (directory == null) {
        directory = new File(System.getProperty("user.home"));
    }
    String filename = Util.getTimestampedOutputFileName(trial.getTrialName(), deviceName);

    File outfile = new File(directory, filename);

    filepathText.setText(outfile.getPath());

    filepathText.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void removeUpdate(DocumentEvent e) {
            updateButtons();
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            updateButtons();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            updateButtons();
        }
    });
    updateButtons();

    boolean developer = RunMode.getRunMode().isDeveloper();
    oldKdsmartOption.setForeground(Color.BLUE);
    oldKdsmartOption.setToolTipText("For ElapsedDays value compatiblity with older versions of KDSmart");

    Box exportForOptionsBox = Box.createHorizontalBox();
    for (JComponent comp : exportForOptions) {
        if (developer || comp != oldKdsmartOption) {
            exportForOptionsBox.add(comp);
        }
    }

    Map<JRadioButton, OutputOption> optionByRb = new HashMap<>();
    ActionListener rbListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            selectedOutputOption = optionByRb.get(e.getSource());
            boolean enb = selectedOutputOption.exportFor != null;
            for (JComponent comp : exportForOptions) {
                if (comp == wantMediaFilesOption) {
                    comp.setEnabled(enb && selectedOutputOption.supportsMediaFiles());
                } else if (comp == kdsmartVersion3option) {
                    comp.setEnabled(enb && selectedOutputOption.usesWorkPackage());
                } else {
                    comp.setEnabled(enb);
                }
            }
        }
    };

    boolean anySamplesForIndividuals = true;
    try {
        anySamplesForIndividuals = kdxploreDatabase.getAnySamplesForIndividuals(sampleGroup);
    } catch (IOException e) {
        Shared.Log.w("SampleGroupExportDialog", "getAnySamplesForIndividuals", e);
    }

    ButtonGroup bg = new ButtonGroup();
    Box radioButtons = Box.createHorizontalBox();

    List<OutputOption> options = new ArrayList<>();
    Collections.addAll(options, OutputOption.values());

    if (!anySamplesForIndividuals) {
        // No relevant samples so don't bother offering the option.
        options.remove(OutputOption.CSV_FULL);
    }

    for (OutputOption oo : options) {
        if (!developer && OutputOption.JSON == oo) {
            continue;
        }

        JRadioButton rb = new JRadioButton(oo.displayName);
        if (OutputOption.KDX == oo) {
            kdxExportButton = rb;
        }

        if (OutputOption.ZIP == oo) {
            zipExportButton = rb;
        }

        rb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                exportExclusionBox.selectAndDeactivateButtons(
                        kdxExportButton.isSelected() || zipExportButton.isSelected());
            }
        });

        if (OutputOption.JSON == oo) {
            rb.setForeground(Color.BLUE);
            rb.setToolTipText("Developer Only");
        }
        bg.add(rb);
        optionByRb.put(rb, oo);
        radioButtons.add(rb);
        rb.addActionListener(rbListener);
        if (bg.getButtonCount() == 1) {
            rb.doClick();
        }
    }

    Box additionalOptionsBox = Box.createHorizontalBox();
    additionalOptionsBox.add(this.wantMediaFilesOption);
    additionalOptionsBox.add(this.kdsmartVersion3option);

    dbVersionLabel.setToolTipText(TTT_DATABASE_VERSION_FOR_EXPORT);
    databaseVersionChoices.setToolTipText(TTT_DATABASE_VERSION_FOR_EXPORT);

    JPanel panel = new JPanel();
    GBH gbh = new GBH(panel);
    int y = 0;

    gbh.add(0, y, 1, 1, GBH.NONE, 0, 1, GBH.EAST, "Output File:");
    gbh.add(1, y, 1, 1, GBH.HORZ, 1, 1, GBH.CENTER, filepathText);
    gbh.add(2, y, 1, 1, GBH.NONE, 1, 1, GBH.WEST, new JButton(browseFileAction));
    ++y;

    gbh.add(0, y, 1, 1, GBH.NONE, 0, 1, GBH.EAST, "Options:");
    gbh.add(1, y, 2, 1, GBH.HORZ, 1, 1, GBH.CENTER, radioButtons);
    ++y;

    gbh.add(0, y, 3, 1, GBH.HORZ, 1, 1, GBH.CENTER, exportExclusionBox);
    ++y;

    gbh.add(0, y, 3, 1, GBH.HORZ, 1, 1, GBH.CENTER, additionalOptionsBox);
    ++y;

    gbh.add(0, y, 1, 1, GBH.NONE, 0, 1, GBH.EAST, dbVersionLabel);
    gbh.add(1, y, 2, 1, GBH.HORZ, 1, 1, GBH.CENTER, BoxBuilder.horizontal().add(databaseVersionChoices).get());

    gbh.add(1, y, 2, 1, GBH.HORZ, 1, 1, GBH.CENTER, exportForOptionsBox);
    ++y;

    Box buttons = Box.createHorizontalBox();
    buttons.add(Box.createHorizontalGlue());
    buttons.add(new JButton(cancelAction));
    buttons.add(new JButton(exportAction));
    buttons.add(new JButton(exportAndCloseAction));

    Container cp = getContentPane();

    cp.add(panel, BorderLayout.CENTER);
    cp.add(buttons, BorderLayout.SOUTH);

    pack();
}

From source file:org.jets3t.gui.UserInputFields.java

/**
 * Builds a user input panel matching the fields specified in the uploader.properties file.
 *
 * @param fieldsPanel/*w  ww.j  a  v  a2 s .  com*/
 * the panel component to add prompt and user input components to.
 * @param uploaderProperties
 * properties specific to the Uploader application that includes the field.* settings
 * necessary to build the User Inputs screen.
 *
 * @return
 * true if there is at least one valid user input field, false otherwise.
 */
public boolean buildFieldsPanel(JPanel fieldsPanel, Jets3tProperties uploaderProperties) {
    int fieldIndex = 0;

    for (int fieldNo = 0; fieldNo < 100; fieldNo++) {
        String fieldName = uploaderProperties.getStringProperty("field." + fieldNo + ".name", null);
        String fieldType = uploaderProperties.getStringProperty("field." + fieldNo + ".type", null);
        String fieldPrompt = uploaderProperties.getStringProperty("field." + fieldNo + ".prompt", null);
        String fieldOptions = uploaderProperties.getStringProperty("field." + fieldNo + ".options", null);
        String fieldDefault = uploaderProperties.getStringProperty("field." + fieldNo + ".default", null);

        if (fieldName == null) {
            log.debug("No field with index number " + fieldNo);
            continue;
        } else {
            if (fieldType == null || fieldPrompt == null) {
                log.warn("Field '" + fieldName + "' missing .type or .prompt properties");
                continue;
            }

            if ("message".equals(fieldType)) {
                JHtmlLabel label = skinsFactory.createSkinnedJHtmlLabel(fieldName);
                label.setText(fieldPrompt);
                label.setHyperlinkeActivatedListener(hyperlinkListener);
                fieldsPanel.add(label, new GridBagConstraints(0, fieldIndex++, 1, 1, 0, 0,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insetsDefault, 0, 0));
            } else if ("radio".equals(fieldType)) {
                if (fieldOptions == null) {
                    log.warn(
                            "Radio button field '" + fieldName + "' is missing the required .options property");
                    continue;
                }

                JHtmlLabel label = skinsFactory.createSkinnedJHtmlLabel(fieldName);
                label.setText(fieldPrompt);
                label.setHyperlinkeActivatedListener(hyperlinkListener);
                fieldsPanel.add(label, new GridBagConstraints(0, fieldIndex++, 1, 1, 0, 0,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insetsDefault, 0, 0));

                JPanel optionsPanel = skinsFactory.createSkinnedJPanel("OptionsPanel");
                optionsPanel.setLayout(new GridBagLayout());
                int columnOffset = 0;
                ButtonGroup buttonGroup = new ButtonGroup();
                StringTokenizer st = new StringTokenizer(fieldOptions, ",");
                while (st.hasMoreTokens()) {
                    String option = st.nextToken();
                    JRadioButton radioButton = skinsFactory.createSkinnedJRadioButton(fieldName);
                    radioButton.setText(option);
                    buttonGroup.add(radioButton);

                    if (fieldDefault != null && fieldDefault.equals(option)) {
                        // This option is the default one.
                        radioButton.setSelected(true);
                    } else if (buttonGroup.getButtonCount() == 1) {
                        // Make first button the default.
                        radioButton.setSelected(true);
                    }

                    optionsPanel.add(radioButton, new GridBagConstraints(columnOffset++, 0, 1, 1, 0, 0,
                            GridBagConstraints.WEST, GridBagConstraints.NONE, insetsDefault, 0, 0));
                }
                fieldsPanel.add(optionsPanel, new GridBagConstraints(0, fieldIndex++, 1, 1, 0, 0,
                        GridBagConstraints.WEST, GridBagConstraints.NONE, insetsNone, 0, 0));

                userInputComponentsMap.put(fieldName, buttonGroup);
            } else if ("selection".equals(fieldType)) {
                if (fieldOptions == null) {
                    log.warn(
                            "Radio button field '" + fieldName + "' is missing the required .options property");
                    continue;
                }

                JHtmlLabel label = skinsFactory.createSkinnedJHtmlLabel(fieldName);
                label.setText(fieldPrompt);
                label.setHyperlinkeActivatedListener(hyperlinkListener);
                fieldsPanel.add(label, new GridBagConstraints(0, fieldIndex++, 1, 1, 0, 0,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insetsDefault, 0, 0));

                JComboBox comboBox = skinsFactory.createSkinnedJComboBox(fieldName);
                StringTokenizer st = new StringTokenizer(fieldOptions, ",");
                while (st.hasMoreTokens()) {
                    String option = st.nextToken();
                    comboBox.addItem(option);
                }

                if (fieldDefault != null) {
                    comboBox.setSelectedItem(fieldDefault);
                }

                fieldsPanel.add(comboBox, new GridBagConstraints(0, fieldIndex++, 1, 1, 1, 0,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insetsDefault, 0, 0));

                userInputComponentsMap.put(fieldName, comboBox);
            } else if ("text".equals(fieldType)) {
                JHtmlLabel label = skinsFactory.createSkinnedJHtmlLabel(fieldName);
                label.setText(fieldPrompt);
                label.setHyperlinkeActivatedListener(hyperlinkListener);
                fieldsPanel.add(label, new GridBagConstraints(0, fieldIndex++, 1, 1, 0, 0,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insetsDefault, 0, 0));
                JTextField textField = skinsFactory.createSkinnedJTextField(fieldName);
                if (fieldDefault != null) {
                    textField.setText(fieldDefault);
                }

                fieldsPanel.add(textField, new GridBagConstraints(0, fieldIndex++, 1, 1, 1, 0,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insetsDefault, 0, 0));

                userInputComponentsMap.put(fieldName, textField);
            } else if ("password".equals(fieldType)) {
                JHtmlLabel label = skinsFactory.createSkinnedJHtmlLabel(fieldName);
                label.setText(fieldPrompt);
                label.setHyperlinkeActivatedListener(hyperlinkListener);
                fieldsPanel.add(label, new GridBagConstraints(0, fieldIndex++, 1, 1, 0, 0,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insetsDefault, 0, 0));
                JPasswordField passwordField = skinsFactory.createSkinnedJPasswordField(fieldName);
                if (fieldDefault != null) {
                    passwordField.setText(fieldDefault);
                }

                fieldsPanel.add(passwordField, new GridBagConstraints(0, fieldIndex++, 1, 1, 1, 0,
                        GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, insetsDefault, 0, 0));

                userInputComponentsMap.put(fieldName, passwordField);
            } else if (fieldType.equals("textarea")) {
                JHtmlLabel label = skinsFactory.createSkinnedJHtmlLabel(fieldName);
                label.setText(fieldPrompt);
                label.setHyperlinkeActivatedListener(hyperlinkListener);
                fieldsPanel.add(label, new GridBagConstraints(0, fieldIndex++, 2, 1, 0, 0,
                        GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, insetsDefault, 0, 0));
                JTextArea textArea = skinsFactory.createSkinnedJTextArea(fieldName);
                textArea.setLineWrap(true);
                if (fieldDefault != null) {
                    textArea.setText(fieldDefault);
                }

                JScrollPane scrollPane = skinsFactory.createSkinnedJScrollPane(fieldName);
                scrollPane.setViewportView(textArea);
                fieldsPanel.add(scrollPane, new GridBagConstraints(0, fieldIndex++, 2, 1, 1, 1,
                        GridBagConstraints.WEST, GridBagConstraints.BOTH, insetsDefault, 0, 0));

                userInputComponentsMap.put(fieldName, textArea);
            } else {
                log.warn("Unrecognised .type setting for field '" + fieldName + "'");
            }
        }
    }

    return isUserInputFieldsAvailable();
}

From source file:org.pentaho.ui.xul.swing.tags.SwingRadio.java

@Override
public void onDomReady() {
    if (this.group != null && getDocument() != null && getDocument().getRootElement() instanceof SwingRoot) {
        ButtonGroup buttonGroup = ((SwingRoot) getDocument().getRootElement()).getButtonGroup(group);
        AbstractButton button = (AbstractButton) getManagedObject();
        buttonGroup.add(button);/*  w w  w  .j  av a  2 s.  c o m*/
        if (buttonGroup.getButtonCount() == 1) {
            // first button in, TODO: remove once selected="true" attribute supported
            button.setSelected(true);
        }
    }
}