List of usage examples for javax.swing JRadioButton addChangeListener
public void addChangeListener(ChangeListener l)
ChangeListener
to the button. From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Grouping Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton = new JRadioButton("A"); JRadioButton bRadioButton = new JRadioButton("B"); ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changEvent) { AbstractButton aButton = (AbstractButton) changEvent.getSource(); ButtonModel aModel = aButton.getModel(); boolean armed = aModel.isArmed(); boolean pressed = aModel.isPressed(); boolean selected = aModel.isSelected(); System.out.println("Changed: " + armed + "/" + pressed + "/" + selected); }/*from w w w . j a v a 2 s.co m*/ }; panel.add(aRadioButton); group.add(aRadioButton); panel.add(bRadioButton); group.add(bRadioButton); aRadioButton.addChangeListener(changeListener); bRadioButton.addChangeListener(changeListener); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); }
From source file:GroupRadio.java
public static Container createRadioButtonGrouping(String elements[], String title, ActionListener actionListener, ItemListener itemListener, ChangeListener changeListener) { JPanel panel = new JPanel(new GridLayout(0, 1)); // If title set, create titled border if (title != null) { Border border = BorderFactory.createTitledBorder(title); panel.setBorder(border);/*from w ww. j a va2s . co m*/ } // Create group ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton; // For each String passed in: // Create button, add to panel, and add to group for (int i = 0, n = elements.length; i < n; i++) { aRadioButton = new JRadioButton(elements[i]); panel.add(aRadioButton); group.add(aRadioButton); if (actionListener != null) { aRadioButton.addActionListener(actionListener); } if (itemListener != null) { aRadioButton.addItemListener(itemListener); } if (changeListener != null) { aRadioButton.addChangeListener(changeListener); } } return panel; }
From source file:net.sf.jabref.gui.mergeentries.MergeEntries.java
/** * Main function for building the merge entry JPanel *//*from w w w. jav a 2 s .c om*/ private void initialize() { doneBuilding = false; setupFields(); // Fill diff mode combo box for (String diffText : DIFF_MODES) { diffMode.addItem(diffText); } diffMode.setSelectedIndex(Math.min(Globals.prefs.getInt(JabRefPreferences.MERGE_ENTRIES_DIFF_MODE), diffMode.getItemCount() - 1)); diffMode.addActionListener(e -> { updateTextPanes(differentFields); storePreference(); }); // Create main layout String colSpecMain = "left:pref, 5px, center:3cm:grow, 5px, center:pref, 3px, center:pref, 3px, center:pref, 5px, center:3cm:grow"; String colSpecMerge = "left:pref, 5px, fill:3cm:grow, 5px, center:pref, 3px, center:pref, 3px, center:pref, 5px, fill:3cm:grow"; String rowSpec = "pref, pref, 10px, fill:5cm:grow, 10px, pref, 10px, fill:3cm:grow"; StringBuilder rowBuilder = new StringBuilder(""); for (int i = 0; i < allFields.size(); i++) { rowBuilder.append("pref, 2dlu, "); } rowBuilder.append("pref"); JPanel mergePanel = new JPanel(); FormLayout mainLayout = new FormLayout(colSpecMain, rowSpec); FormLayout mergeLayout = new FormLayout(colSpecMerge, rowBuilder.toString()); mainPanel.setLayout(mainLayout); mergePanel.setLayout(mergeLayout); CellConstraints cc = new CellConstraints(); mainPanel.add(boldFontLabel(Localization.lang("Use")), cc.xyw(4, 1, 7, "center, bottom")); mainPanel.add(diffMode, cc.xy(11, 1, "right, bottom")); // Set headings JLabel[] headingLabels = new JLabel[6]; for (int i = 0; i < 6; i++) { headingLabels[i] = boldFontLabel(COLUMN_HEADINGS[i]); mainPanel.add(headingLabels[i], cc.xy(1 + (i * 2), 2)); } mainPanel.add(new JSeparator(), cc.xyw(1, 3, 11)); // Start with entry type mergePanel.add(boldFontLabel(Localization.lang("Entry type")), cc.xy(1, 1)); JTextPane leftTypeDisplay = getStyledTextPane(); leftTypeDisplay.setText(HTML_START + leftEntry.getType() + HTML_END); mergePanel.add(leftTypeDisplay, cc.xy(3, 1)); if (leftEntry.getType().equals(rightEntry.getType())) { identicalTypes = true; } else { identicalTypes = false; ButtonGroup group = new ButtonGroup(); typeRadioButtons = new ArrayList<>(2); for (int k = 0; k < 3; k += 2) { JRadioButton button = new JRadioButton(); typeRadioButtons.add(button); group.add(button); mergePanel.add(button, cc.xy(5 + (k * 2), 1)); button.addChangeListener(e -> updateAll()); } typeRadioButtons.get(0).setSelected(true); } JTextPane rightTypeDisplay = getStyledTextPane(); rightTypeDisplay.setText(HTML_START + rightEntry.getType() + HTML_END); mergePanel.add(rightTypeDisplay, cc.xy(11, 1)); // For all fields in joint add a row and possibly radio buttons int row = 2; int maxLabelWidth = -1; for (String field : allFields) { JLabel label = boldFontLabel(new SentenceCaseFormatter().format(field)); mergePanel.add(label, cc.xy(1, (2 * row) - 1, "left, top")); Optional<String> leftString = leftEntry.getFieldOptional(field); Optional<String> rightString = rightEntry.getFieldOptional(field); if (leftString.equals(rightString)) { identicalFields.add(field); } else { differentFields.add(field); } maxLabelWidth = Math.max(maxLabelWidth, label.getPreferredSize().width); // Left text pane if (leftString.isPresent()) { JTextPane tf = getStyledTextPane(); mergePanel.add(tf, cc.xy(3, (2 * row) - 1, "f, f")); leftTextPanes.put(field, tf); } // Add radio buttons if the two entries do not have identical fields if (identicalFields.contains(field)) { mergedEntry.setField(field, leftString.get()); // Will only happen if both entries have the field and the content is identical } else { ButtonGroup group = new ButtonGroup(); List<JRadioButton> list = new ArrayList<>(3); for (int k = 0; k < 3; k++) { JRadioButton button = new JRadioButton(); group.add(button); mergePanel.add(button, cc.xy(5 + (k * 2), (2 * row) - 1)); button.addChangeListener(e -> updateAll()); list.add(button); } radioButtons.put(field, list); if (leftString.isPresent()) { list.get(0).setSelected(true); if (!rightString.isPresent()) { list.get(2).setEnabled(false); } } else { list.get(0).setEnabled(false); list.get(2).setSelected(true); } } // Right text pane if (rightString.isPresent()) { JTextPane tf = getStyledTextPane(); mergePanel.add(tf, cc.xy(11, (2 * row) - 1, "f, f")); rightTextPanes.put(field, tf); } row++; } scrollPane = new JScrollPane(mergePanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setBorder(BorderFactory.createEmptyBorder()); updateTextPanes(allFields); mainPanel.add(scrollPane, cc.xyw(1, 4, 11)); mainPanel.add(new JSeparator(), cc.xyw(1, 5, 11)); // Synchronize column widths String[] rbAlign = { "right", "center", "left" }; mainLayout.setColumnSpec(1, ColumnSpec.decode(Integer.toString(maxLabelWidth) + "px")); Integer maxRBWidth = -1; for (int k = 2; k < 5; k++) { maxRBWidth = Math.max(maxRBWidth, headingLabels[k].getPreferredSize().width); } for (int k = 0; k < 3; k++) { mergeLayout.setColumnSpec(5 + (k * 2), ColumnSpec.decode(rbAlign[k] + ":" + maxRBWidth + "px")); } // Setup a PreviewPanel and a Bibtex source box for the merged entry mainPanel.add(boldFontLabel(Localization.lang("Merged entry")), cc.xyw(1, 6, 6)); entryPreview = new PreviewPanel(null, mergedEntry, null, Globals.prefs.get(JabRefPreferences.PREVIEW_0)); mainPanel.add(entryPreview, cc.xyw(1, 8, 6)); mainPanel.add(boldFontLabel(Localization.lang("Merged BibTeX source code")), cc.xyw(8, 6, 4)); sourceView = new JTextArea(); sourceView.setLineWrap(true); sourceView.setFont(new Font("Monospaced", Font.PLAIN, Globals.prefs.getInt(JabRefPreferences.FONT_SIZE))); mainPanel.add(new JScrollPane(sourceView), cc.xyw(8, 8, 4)); sourceView.setEditable(false); // Add some margin around the layout mainLayout.appendRow(RowSpec.decode(MARGIN)); mainLayout.appendColumn(ColumnSpec.decode(MARGIN)); mainLayout.insertRow(1, RowSpec.decode(MARGIN)); mainLayout.insertColumn(1, ColumnSpec.decode(MARGIN)); // Everything done, allow any action to actually update the merged entry doneBuilding = true; updateAll(); // Show what we've got mainPanel.setVisible(true); javax.swing.SwingUtilities.invokeLater(() -> scrollPane.getVerticalScrollBar().setValue(0)); }
From source file:edu.ku.brc.af.auth.UserAndMasterPasswordMgr.java
/** * Displays a dialog used for editing the Master Username and Password. * @param isLocal whether u/p is stored locally or not * @param usrName/*from w w w .jav a2 s.c o m*/ * @param dbName * @param masterPath the path to the password * @return whether to ask for the information because it wasn't found */ protected boolean askForInfo(final Boolean isLocal, final String usrName, final String dbName, final String masterPath) { loadAndPushResourceBundle("masterusrpwd"); FormLayout layout = new FormLayout("p, 2px, f:p:g, 4px, p, 4px, p, 4px, p", "p,2px,p,2px,p,2dlu,p,2dlu,p"); PanelBuilder pb = new PanelBuilder(layout); pb.setDefaultDialogBorder(); ButtonGroup group = new ButtonGroup(); final JRadioButton isNetworkRB = new JRadioButton(getResourceString("IS_NET_BASED")); final JRadioButton isPrefBasedRB = new JRadioButton(getResourceString("IS_ENCRYPTED_KEY")); isPrefBasedRB.setSelected(true); group.add(isNetworkRB); group.add(isPrefBasedRB); final JTextField keyTxt = createTextField(35); final JTextField urlTxt = createTextField(35); final JLabel keyLbl = createI18NFormLabel("ENCRYPTED_USRPWD"); final JLabel urlLbl = createI18NFormLabel("URL"); final JButton createBtn = createI18NButton("CREATE_KEY"); final JButton copyCBBtn = createIconBtn("ClipboardCopy", IconManager.IconSize.Std24, "CPY_TO_CB_TT", null); final JButton pasteCBBtn = createIconBtn("ClipboardPaste", IconManager.IconSize.Std24, "CPY_FROM_CB_TT", null); // retrieves the encrypted key for the current settings in the dialog String dbNameFromForm = AppPreferences.getLocalPrefs().get("login.databases_selected", null); if (isNotEmpty(dbNameFromForm) && isNotEmpty(usersUserName)) { String masterKey = getMasterPrefPath(usersUserName, dbNameFromForm, true); if (isNotEmpty(masterKey)) { String encryptedKey = AppPreferences.getLocalPrefs().get(masterKey, null); if (isNotEmpty(encryptedKey) && !encryptedKey.startsWith("http")) { keyTxt.setText(encryptedKey); } } } CellConstraints cc = new CellConstraints(); int y = 1; pb.add(createI18NFormLabel("MASTER_LOC"), cc.xywh(1, y, 1, 3)); pb.add(isPrefBasedRB, cc.xy(3, y)); y += 2; pb.add(isNetworkRB, cc.xy(3, y)); y += 2; pb.addSeparator("", cc.xyw(1, y, 9)); y += 2; pb.add(keyLbl, cc.xy(1, y)); pb.add(keyTxt, cc.xy(3, y)); pb.add(createBtn, cc.xy(5, y)); pb.add(copyCBBtn, cc.xy(7, y)); pb.add(pasteCBBtn, cc.xy(9, y)); y += 2; pb.add(urlLbl, cc.xy(1, y)); pb.add(urlTxt, cc.xy(3, y)); y += 2; boolean isEditMode = isLocal != null && isNotEmpty(masterPath); if (isEditMode) { isPrefBasedRB.setSelected(isLocal); if (isLocal) { keyTxt.setText(masterPath); } else { urlTxt.setText(masterPath); } } copyCBBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Copy to Clipboard UIHelper.setTextToClipboard(keyTxt.getText()); } }); pasteCBBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { keyTxt.setText(UIHelper.getTextFromClipboard()); } }); final CustomDialog dlg = new CustomDialog((Frame) null, getResourceString("MASTER_TITLE"), true, CustomDialog.OKCANCELHELP, pb.getPanel()); if (!isEditMode) { dlg.setOkLabel(getResourceString("CONT")); dlg.setCancelLabel(getResourceString("BACK")); } dlg.setHelpContext("MASTERPWD_MAIN"); dlg.createUI(); dlg.getOkBtn().setEnabled(false); urlLbl.setEnabled(false); urlTxt.setEnabled(false); copyCBBtn.setEnabled(true); pasteCBBtn.setEnabled(true); DocumentListener dl = new DocumentAdaptor() { @Override protected void changed(DocumentEvent e) { dlg.getOkBtn().setEnabled((isPrefBasedRB.isSelected() && !keyTxt.getText().isEmpty()) || (isNetworkRB.isSelected() && !urlTxt.getText().isEmpty())); } }; keyTxt.getDocument().addDocumentListener(dl); urlTxt.getDocument().addDocumentListener(dl); ChangeListener chgListener = new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { boolean isNet = isNetworkRB.isSelected(); keyLbl.setEnabled(!isNet); keyTxt.setEnabled(!isNet); createBtn.setEnabled(!isNet); copyCBBtn.setEnabled(!isNet); pasteCBBtn.setEnabled(!isNet); urlLbl.setEnabled(isNet); urlTxt.setEnabled(isNet); dlg.getOkBtn().setEnabled((isPrefBasedRB.isSelected() && !keyTxt.getText().isEmpty()) || (isNetworkRB.isSelected() && !urlTxt.getText().isEmpty())); } }; isNetworkRB.addChangeListener(chgListener); isPrefBasedRB.addChangeListener(chgListener); boolean isPref = AppPreferences.getLocalPrefs().getBoolean(getIsLocalPrefPath(usrName, dbName, true), true); isNetworkRB.setSelected(!isPref); isPrefBasedRB.setSelected(isPref); createBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String[] keys = getUserNamePasswordKey(); if (keys != null && keys.length == 4) { String encryptedStr = encrypt(keys[0], keys[1], keys[3]); if (encryptedStr != null) { keyTxt.setText(encryptedStr); dlg.getOkBtn().setEnabled(true); usersUserName = keys[2]; } } } }); popResourceBundle(); dlg.setVisible(true); if (!dlg.isCancelled()) { String value; if (isNetworkRB.isSelected()) { value = StringEscapeUtils.escapeHtml(urlTxt.getText()); } else { value = keyTxt.getText(); } AppPreferences.getLocalPrefs().putBoolean(getIsLocalPrefPath(usrName, dbName, true), !isNetworkRB.isSelected()); AppPreferences.getLocalPrefs().put(getMasterPrefPath(usrName, dbName, true), value); return true; } return false; }