Java JButton

Java JButton

Java JButton class declaration Syntax
Java JButton class is used to create a labeled button.When JButton is clicked a certain action is implemented by class  addActionListener
To use JButton first you need to import it as a swing component.

Java JButton class declaration Syntax 

[public class JButton extends AbstractButton implements Accessible]


JButton Common used Constructors

Below  are some of the commonly used JButton constructors.
  • JButton()-create empty button with no text or icon.
  • JButton(String s)-create button with a string 
  • JButton(Icon icon)-create button with icon.

Common used JButton AbstractButton class methods.


Java JButton Example

[
import javax.swing.JFrame;
import javax.swing.JButton;
public class JavaJButton {

    public static void main(String args[])
    {
        JFrame frame=new JFrame("Jbutton Example");
        JButton button=new JButton("Save");

        button.setBounds(50,100,95,30);
        frame.add(button);
        frame.setSize(400,400);
        frame.setLayout(null);
        frame.setVisible(true);

    }
}
]


 Example to Display Icon to the JButton.

[
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;
public class JavaJButton {

    public static void main(String args[])
    {
        JFrame frame=new JFrame("Jbutton Example");
        JButton button=new JButton();
        button.setIcon(new ImageIcon(JavaJButton.class.getResource("save.png")));

        button.setBounds(100,100,95,30);
        frame.add(button);
        frame.setSize(400,400);
        frame.setLayout(null);
        frame.setVisible(true);



    }

}
]

Result
Java JButton


Adding ActionListener to JButton

[
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class JavaJButton {

    public static void main(String args[])
    {
        JFrame frame=new JFrame("Jbutton Example");
        JButton button=new JButton("Save");

        JTextField field=new JTextField();
        field.setBounds(50,50, 150,20);
        frame.add(field);
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                field.setText("I Love Java");

            }


        });


        button.setBounds(100,100,95,30);
        frame.add(button);
        frame.setSize(400,400);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setVisible(true);



    }

}
]

Output:

void addActionListener(ActionListener listener)-

When button is clicked below code is executed that set text in the JTextField to I love Java
[
button.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent event)
    {
        field.setText("I Love Java");

    }


});
]


COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Java JButton
Java JButton
Java JButton
https://1.bp.blogspot.com/-pAnm-hLlmOc/WK8v6waF_7I/AAAAAAAAG6g/NiMtxly1k-0jW94hohbPxQgrbJcXYo6igCLcB/s200/JavaJButton-min.PNG
https://1.bp.blogspot.com/-pAnm-hLlmOc/WK8v6waF_7I/AAAAAAAAG6g/NiMtxly1k-0jW94hohbPxQgrbJcXYo6igCLcB/s72-c/JavaJButton-min.PNG
Java Codes
http://www.download-all.net/2017/02/java-jbutton.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/02/java-jbutton.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy