Changethe Label of a JButton Component in Java

Description

The following code shows how to changethe Label of a JButton Component.

Example


import javax.swing.JButton;
import javax.swing.JFrame;
/*w  w w.  j  av a2 s.co  m*/
public class Main extends JFrame {

  public Main() {
    setSize(300, 150);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JButton button = new JButton("java2s.com");
      button.setText("New Label");
      // Remove the label; this is useful for a button with only an icon
      //button.setText(null);
    
    add(button);
    setVisible(true);
  }

  public static void main(String[] args) {
    new Main();
  }
}

The code above generates the following result.

Changethe Label of a JButton Component in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer