create an exit button in java? - java

I'm a beginner in java, I'm practicing a Project that have should create an exit button that exit the program when we press it. But when I run this project in JDK the exit button is not working.
How can I make exit button work?

There two ways for closing a window:
X exit button at the top side of the window.
Custom exit button.
Solution for 1
To achieve it you should set for the JFrame the suitable default action you want to produce when clicking on X.
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Solution for 2
this.dispose();

if you want to create a message of yes_no option than follow the given code whenever u press the exit button you have created in J Frame so it will show a dialog message confirm if you want to Exit click yes so your app will be closed.
private void JbtnExitActionPerformed(java.awt.event.ActionEvent evt) {
Frame = new JFrame("Exit");
if (JOptionPane.showConfirmDialog( Frame,"confirm if you Want to Exit","Name of the Application or Title",
JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
System.exit(0);

If you want a JButton that closes the application, you would create the button:
JButton Button = new JButton("Close");
Then you would add a custom handler to the button:
Button.addActionListener (new ActionListener ()) {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
};
Then you would add the button to the frame or panel.
frame.add(button);
(If your frame is called frame)

Related

java.swing and java.awt how to check if a button is activated or not? [duplicate]

How can I check that JButton is pressed? I know that there is a method that its name is "isEnabled"
So I try to write a code to test.
this code have 2 Jbuttons which are "Add" Button and "Checkout" button.
the code will show the "Add button is pressed" message when I press "Checkout" button after I press "Add" button but If the "Add" Button is not pressed before the "Checkout" Button is pressed, the code will show the "Add Button is not pressed" message.
Here the code:
final JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
panel.add(btnAdd);
JButton btnConfirm = new JButton("Check Out");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnAdd.isEnabled()) {
System.out.println("Add Button is pressed");
}
if (!btnAdd.isEnabled()) {
System.out.println("Add Button is not pressed");
}
}
});
When I run this code,the code give only the " Add button is pressed" although I didn't press the "Add" Button. Why does it occur like that?
JButton has a model which answers these question:
isArmed(),
isPressed(),
isRollOVer()
etc. Hence you can ask the model for the answer you are seeking:
if(jButton1.getModel().isPressed())
System.out.println("the button is pressed");
Seems you need to use JToggleButton :
JToggleButton tb = new JToggleButton("push me");
tb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JToggleButton btn = (JToggleButton) e.getSource();
btn.setText(btn.isSelected() ? "pushed" : "push me");
}
});
JButton#isEnabled changes the user interactivity of a component, that is, whether a user is able to interact with it (press it) or not.
When a JButton is pressed, it fires a actionPerformed event.
You are receiving Add button is pressed when you press the confirm button because the add button is enabled. As stated, it has nothing to do with the pressed start of the button.
Based on you code, if you tried to check the "pressed" start of the add button within the confirm button's ActionListener it would always be false, as the button will only be in the pressed state while the add button's ActionListeners are being called.
Based on all this information, I would suggest you might want to consider using a JCheckBox which you can then use JCheckBox#isSelected to determine if it has being checked or not.
Take a closer look at How to Use Buttons for more details
Just do System.out.println(e.getActionCommand()); inside actionPerformed(ActionEvent e) function. This will tell you which command is just performed.
or
if(e.getActionCommand().equals("Add")){
System.out.println("Add button pressed");
}
The method you are trying to use checks if the button is active:
btnAdd.isEnabled()
When enabled, any component associated with this object is active and able to fire this object's actionPerformed method.
This method does not check if the button is pressed.
If i understand your question correctly, you want to disable your "Add" button after the user clicks "Check out".
Try disabling your button at start: btnAdd.setEnabled(false) or after the user presses "Check out"
Use this Command
if(JButton.getModel().isArmed()){
//your code here.
//your code will be only executed if JButton is clicked.
}
Answer is short. But it worked for me. Use the variable name of your button instead of "JButton".

How to read from URL after a button press

Ok hello, What I want is when my button is pressed I want read the text from a URL and display it in the GUI,
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
//???????????????????
}
I am very confused on this. I saw it in a Open Source project and I can't get it to work :/ All I really want is it to open up to a raw github file (in the GUI) to display the contents of the github.
This is the github link
I want the text to display as if it where actually in the application.
Thanks for any help
What I would do, is go ahead and make a new JFrame, named something different than your previous one, but set it's visibility to false. Within that new JFrame, "ex", add a layout and then add you github info into the layout.
ex:
JFrame ex = new JFrame();
ex.setSize(50,50);
ex.setVisible(false);
ex.setResizable(falsE);
ex.setDefaultCloseOperation(JFrame.Dispose);
//Use "dispose" to exit the window but not terminate your program
and then, when you have a JButton clicked, or whatever you're using, use an actionevent.
ex:
jButton1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("JButton1 Button pressed");
ex.setVisible(true);
}
});

How can I check that JButton is pressed? If the isEnable() is not work?

How can I check that JButton is pressed? I know that there is a method that its name is "isEnabled"
So I try to write a code to test.
this code have 2 Jbuttons which are "Add" Button and "Checkout" button.
the code will show the "Add button is pressed" message when I press "Checkout" button after I press "Add" button but If the "Add" Button is not pressed before the "Checkout" Button is pressed, the code will show the "Add Button is not pressed" message.
Here the code:
final JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
panel.add(btnAdd);
JButton btnConfirm = new JButton("Check Out");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnAdd.isEnabled()) {
System.out.println("Add Button is pressed");
}
if (!btnAdd.isEnabled()) {
System.out.println("Add Button is not pressed");
}
}
});
When I run this code,the code give only the " Add button is pressed" although I didn't press the "Add" Button. Why does it occur like that?
JButton has a model which answers these question:
isArmed(),
isPressed(),
isRollOVer()
etc. Hence you can ask the model for the answer you are seeking:
if(jButton1.getModel().isPressed())
System.out.println("the button is pressed");
Seems you need to use JToggleButton :
JToggleButton tb = new JToggleButton("push me");
tb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JToggleButton btn = (JToggleButton) e.getSource();
btn.setText(btn.isSelected() ? "pushed" : "push me");
}
});
JButton#isEnabled changes the user interactivity of a component, that is, whether a user is able to interact with it (press it) or not.
When a JButton is pressed, it fires a actionPerformed event.
You are receiving Add button is pressed when you press the confirm button because the add button is enabled. As stated, it has nothing to do with the pressed start of the button.
Based on you code, if you tried to check the "pressed" start of the add button within the confirm button's ActionListener it would always be false, as the button will only be in the pressed state while the add button's ActionListeners are being called.
Based on all this information, I would suggest you might want to consider using a JCheckBox which you can then use JCheckBox#isSelected to determine if it has being checked or not.
Take a closer look at How to Use Buttons for more details
Just do System.out.println(e.getActionCommand()); inside actionPerformed(ActionEvent e) function. This will tell you which command is just performed.
or
if(e.getActionCommand().equals("Add")){
System.out.println("Add button pressed");
}
The method you are trying to use checks if the button is active:
btnAdd.isEnabled()
When enabled, any component associated with this object is active and able to fire this object's actionPerformed method.
This method does not check if the button is pressed.
If i understand your question correctly, you want to disable your "Add" button after the user clicks "Check out".
Try disabling your button at start: btnAdd.setEnabled(false) or after the user presses "Check out"
Use this Command
if(JButton.getModel().isArmed()){
//your code here.
//your code will be only executed if JButton is clicked.
}
Answer is short. But it worked for me. Use the variable name of your button instead of "JButton".

Popup existing Jframe when clicking button in another Jframe

i am working in JAVA GUI project with netbeans ,,
I just created Jframe and put a button on it ,,
I created also another JFrame and added many labels
I am asking how can the second JFrame appears when i click on the button in the first JFrame
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new SecondFrame().setVisible(true);
FirstFrame.this.dispose(); // if you want the first frame to close
}
Check out How to Write an Action Listener for more information.

Opening a new GUI window on a main GUI using GUI builder on netbeans

I am playing about with GUI builder and i was wondering if there is an easy way to open the register window through the current main window (in reference to the page below). I am trying to do this through the menu bar.
I've been trying all day, because GUI Builder generates some code, its not possible to edit this code.
Thanks For the help!
Create a separate class which extends JDialog class and add your GUI components:
public Register extends JDialog {
//Make GUI
setModalityType(ModalityType.APPLICATION_MODAL); //Make it modal
}
Add ActionListener to that menu item which is supposed to open a register window:
mnuItmRegisteration.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Register r = new Register();
r.setVisible(true);
}
});
Right click on that shortcut button, click Events, click ActionPreformed.
There you should write codes to make your register window appear.
An example:
private void RegisterationEventActionPerformed(java.awt.event.ActionEvent evt) {
JFrame Register = new Register();
Register.setVisible(true);
}
Remember to make another JFrame called ("Register" assuming u are using the code i gave) at the same package as your current JFrame
Maybe u would probably should use the run button (The button with a Green Triangle or Arrow), run it try to press the menu item, it should appear the register window.

Categories

Resources