Java Utililty Methods JButton

List of utility methods to do JButton

  1. HOME
  2. Java
  3. J
  4. JButton

Description

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

Method

void addActionToButton(JButton button, final Runnable action)
add Action To Button
button.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 action.run();
});
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 adjustButtonMargins(Insets margin, JButton... buttons)
adjust Button Margins
for (JButton button : buttons)
 button.setMargin(margin);
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 buttonState(JButton button, boolean boolArrayOfErrors[])
Takes a JButton and a boolean array of errors.
if (!isErrorPresent(boolArrayOfErrors)) {
 button.setEnabled(true);
} else {
 button.setEnabled(false);
void closeFrame(JButton thisButton)
Called by a button to close the containing window when actions are done.
((JFrame) thisButton.getTopLevelAncestor()).dispose();
void configureButton(JButton button)
Configures a jButton with default behaviour like the multi-click threshold.
button.setMultiClickThreshhold(500);
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));
...


AltStyle によって変換されたページ (->オリジナル) /