• We just launched and are currently in beta. Join us as we build and grow the community.

JToggleButton Component in Java

Logan0790

Irony Crafter
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
151
Likes
66
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This is a tutorial in which we will going to create a program that has the JToggleButton Component using Java. The JToggleButton is a button which implements of a two-state button; an on or off button.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of jToggleButtonComponent.java.

2. Import the following packages:
  1. import

    java.awt.event.*

    ;

    // used to access the ActionEvent and ActionListener class
  2. import

    javax.swing.*

    ;

    // used to access the JFrame, JOptionPane, JToggleButton

3. Initialize your variable in your Main, variable frame for creating JFrame, and variable toggleBtn for JToggleButton.
  1. JFrame

    frame =

    new

    JFrame

    (

    "JToggle Component"

    )

    ;
  2. JToggleButton

    toggleBtn =

    new

    JToggleButton

    (

    "Toggle Me!"

    )

    ;

4. Now, we will add an ActionListener to the toggle button. This trigger to have an ActionEvent when clicking the toggle button. Have this code below:

  1. toggleBtn.addActionListener

    (

    new

    ActionListener

    (

    )

    {

  2. public

    void

    actionPerformed(

    ActionEvent

    e)

    {
  3. JToggleButton

    tBtn =

    (

    JToggleButton

    )

    e.getSource

    (

    )

    ;
  4. if

    (

    tBtn.isSelected

    (

    )

    )

    {
  5. JOptionPane

    .showMessageDialog

    (

    null

    , "Button is on!"

    )

    ;
  6. }

    else

    {
  7. JOptionPane

    .showMessageDialog

    (

    null

    , "Button is off!"

    )

    ;
  8. }
  9. }
  10. }

    )

    ;

As you have seen the code above, we have created again a JToggleButton having a variable named of tBtn. We used the method getSource to get the action out of the JToggleButton. When clicking the JToggleButton to determine if it's on or off, we will use isSelected method. When we click first the toggle button, it will prompt to the user that the button is on using the JOptionPane. When clicking again the button, it will display that the button is off.

5. Now, add the JToggleButton variable to the frame using the default BorderLayout of north position of the getContentPane method. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:

  1. frame.getContentPane

    (

    )

    .add

    (

    toggleBtn, "North"

    )

    ;
  2. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  3. frame.setSize

    (

    300

    , 125

    )

    ;
  4. frame.setVisible

    (

    true

    )

    ;


Output:

jtoggle.png


Here's the full code of this tutorial:

  1. import

    java.awt.event.*

    ;

    // used to access the ActionEvent and ActionListener class
  2. import

    javax.swing.*

    ;

    // used to access the JFrame, JOptionPane, JToggleButton


  3. public

    class

    jToggleButtonComponent {
  4. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  5. JFrame

    frame =

    new

    JFrame

    (

    "JToggle Component"

    )

    ;
  6. JToggleButton

    toggleBtn =

    new

    JToggleButton

    (

    "Toggle Me!"

    )

    ;

  7. toggleBtn.addActionListener

    (

    new

    ActionListener

    (

    )

    {

  8. public

    void

    actionPerformed(

    ActionEvent

    e)

    {
  9. JToggleButton

    tBtn =

    (

    JToggleButton

    )

    e.getSource

    (

    )

    ;
  10. if

    (

    tBtn.isSelected

    (

    )

    )

    {
  11. JOptionPane

    .showMessageDialog

    (

    null

    , "Button is on!"

    )

    ;
  12. }

    else

    {
  13. JOptionPane

    .showMessageDialog

    (

    null

    , "Button is off!"

    )

    ;
  14. }
  15. }
  16. }

    )

    ;

  17. frame.getContentPane

    (

    )

    .add

    (

    toggleBtn, "North"

    )

    ;
  18. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  19. frame.setSize

    (

    300

    , 125

    )

    ;
  20. frame.setVisible

    (

    true

    )

    ;


  21. }
  22. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,736

324,744

Top