The list of methods to do JButton are organized into topic(s).
void
addCRListener(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();
});
...
void
addEnterListener(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();
});
...
void
adjustButtonWidth(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();
...
void
btnHover(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));
void
closeFrame(JButton thisButton) Called by a button to close the containing window when actions are done.
((JFrame) thisButton.getTopLevelAncestor()).dispose();
void
considerarSetaComoTab(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));
...