Logan0790
Irony Crafter
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:
3. Initialize your variable in your Main, variable frame for creating JFrame, and variable toggleBtn for JToggleButton.
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:
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:
Output:
Here's the full code of this tutorial:
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
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:
- import
java.awt.event.*
;
// used to access the ActionEvent and ActionListener class
- 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.
- JFrame
frame =
new
JFrame
(
"JToggle Component"
)
;
- 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:
- toggleBtn.addActionListener
(
new
ActionListener
(
)
{
- public
void
actionPerformed(
ActionEvent
e)
{
- JToggleButton
tBtn =
(
JToggleButton
)
e.getSource
(
)
;
- if
(
tBtn.isSelected
(
)
)
{
- JOptionPane
.showMessageDialog
(
null
, "Button is on!"
)
;
- }
else
{
- JOptionPane
.showMessageDialog
(
null
, "Button is off!"
)
;
- }
- }
- }
)
;
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:
- frame.getContentPane
(
)
.add
(
toggleBtn, "North"
)
;
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
- frame.setSize
(
300
, 125
)
;
- frame.setVisible
(
true
)
;
Output:

Here's the full code of this tutorial:
- import
java.awt.event.*
;
// used to access the ActionEvent and ActionListener class
- import
javax.swing.*
;
// used to access the JFrame, JOptionPane, JToggleButton
- public
class
jToggleButtonComponent {
- public
static
void
main(
String
[
]
args)
{
- JFrame
frame =
new
JFrame
(
"JToggle Component"
)
;
- JToggleButton
toggleBtn =
new
JToggleButton
(
"Toggle Me!"
)
;
- toggleBtn.addActionListener
(
new
ActionListener
(
)
{
- public
void
actionPerformed(
ActionEvent
e)
{
- JToggleButton
tBtn =
(
JToggleButton
)
e.getSource
(
)
;
- if
(
tBtn.isSelected
(
)
)
{
- JOptionPane
.showMessageDialog
(
null
, "Button is on!"
)
;
- }
else
{
- JOptionPane
.showMessageDialog
(
null
, "Button is off!"
)
;
- }
- }
- }
)
;
- frame.getContentPane
(
)
.add
(
toggleBtn, "North"
)
;
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
- frame.setSize
(
300
, 125
)
;
- frame.setVisible
(
true
)
;
- }
- }
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.