Java Utililty Methods JButton

List of utility methods to do JButton

Description

The list of methods to do JButton are organized into topic(s).

Method

voidaddActionToButton(JButton button, final Runnable action)
add Action To Button
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        action.run();
});
voidaddCRListener(JComponent c, final JButton b)
Pressing Enter on the given component will act as clicking on the given button.
c.addKeyListener(new KeyAdapter() {
    @Override
    public void keyTyped(KeyEvent e) {
        if (e.getKeyChar() == KeyEvent.VK_ENTER) {
            b.doClick();
});
...
voidaddEnterListener(JComponent c, final JButton b)
This method implements hotkey binding for [Enter] for currently focused JComponent with a "Ok" Button.
c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), "pressOK");
c.getActionMap().put("pressOK", new AbstractAction() {
    private static final long serialVersionUID = 1L;
    @Override
    public void actionPerformed(ActionEvent next) {
        b.doClick();
});
...
voidadjustButtonMargins(Insets margin, JButton... buttons)
adjust Button Margins
for (JButton button : buttons)
    button.setMargin(margin);
voidadjustButtonWidth(JButton button, int minWidth, int minHeight)
adjust Button Width
Font f = button.getFont();
if (f != null) {
    FontMetrics fm = button.getFontMetrics(f);
    if (fm != null) {
        Rectangle2D bounds = f.getStringBounds(button.getText(), fm.getFontRenderContext());
        int width = (int) bounds.getWidth() + 2;
        int height = Math.max((int) bounds.getHeight(), minHeight);
        Dimension pref = button.getPreferredSize();
...
voidbtnHover(JButton btn)
btn Hover
btn.setContentAreaFilled(true);
btn.setFocusPainted(false);
btn.setMargin(new Insets(0, 0, 0, 0));
btn.setBorderPainted(false);
btn.setOpaque(false);
btn.setCursor(new Cursor(Cursor.HAND_CURSOR));
voidbuttonState(JButton button, boolean boolArrayOfErrors[])
Takes a JButton and a boolean array of errors.
if (!isErrorPresent(boolArrayOfErrors)) {
    button.setEnabled(true);
} else {
    button.setEnabled(false);
voidcloseFrame(JButton thisButton)
Called by a button to close the containing window when actions are done.
((JFrame) thisButton.getTopLevelAncestor()).dispose();
voidconfigureButton(JButton button)
Configures a jButton with default behaviour like the multi-click threshold.
button.setMultiClickThreshhold(500);
voidconsiderarSetaComoTab(JButton comp)
considerar Seta Como Tab
Set<AWTKeyStroke> newKeystrokes;
newKeystrokes = new HashSet<AWTKeyStroke>(
        comp.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
newKeystrokes.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_RIGHT, 0));
newKeystrokes.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_KP_RIGHT, 0));
comp.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newKeystrokes);
newKeystrokes = new HashSet<AWTKeyStroke>(
        comp.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
...