I'm unable to fix the JOption error in Java, GUI - java

1- I don't get why "frmLOGIN" is giving me an error. What should I input instead?
2- what's the right syntax for JOption here? why is that an error?
I tried changing JOption to JOptionpane but I don't think that fixes the issue
I also tried changing frmLOGIN to lblLogIn but that changes the JFrame which shouldn't occur.
JButton btnExit = new JButton("EXIT");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frmLOGIN = new JFrame("EXIT");
if (JOption.showConfirmDialog(frmLOGIN, "Confirm if you want to exit", "LOGIN"),
JOptionPane.YES_NO_OPTION)==JOptionPane.YES_NO_OPTION){
System.exit(0);
}
}
this is an image go my GUI so far

your code should be like this :
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(frmLOGIN, "Confirm if you want to exit", "LOGIN",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});

It should be
if (JOptionPane.showConfirmDialog(frmLOGIN, "Confirm if you want to exit", "LOGIN",
JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
I.e. replace by YES_OPTION

Related

JOptionPane to appear on selected JCheckBox

Hi all I am having some difficulties with adding a joptionpane in JcheckBox listener
public void itemStateChanged(ItemEvent evt) {
if(evt.getStateChange() == ItemEvent.SELECTED){
///some code
JOptionPane.showMessageDialog(null, "Message", "Alert",
JOptionPane.INFORMATION_MESSAGE);
}
}
so it works fine,but the problem is that the JCheckBox gets selected and immediately deselected how can I manage to fix this ?
Cheers
There are a couple of suggestions here (solution) to use an action listener instead of a item listener. This does work, however, I though it strange given that all the texts I have suggest an item listener is the expected type of listener for a check box.
In fact, this is a known bug as acknowledged by Oracle Bug ID:6924233 The JOptionPane apparently causes another event to be generated.
The recommended fix is to invoke the JOptionPane using invokeLater. This works fine and involves only a minor code change to a program already using an item listener for other purposes.
The problem must be in "///some code" as the following test program works for me in Java 6:
public class CheckBoxItemListener {
public static void main(String[] args) {
final JCheckBox checkBox = new JCheckBox("Click me");
JFrame frame = new JFrame("CheckBox Item Listener");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 300, 300);
frame.add(checkBox);
frame.setVisible(true);
checkBox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.SELECTED){
JOptionPane.showMessageDialog(null, "Message", "Alert",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
}
}
Have a look in the omitted code for setSelected or doClick calls.

Handling exit button which is create automatically?

I am building a text editor and I don't know how to handle a listener on Swing exit button, which is create automatically.
I want to use dialogs when user doesn't save file, for example press exit button.
Assuming you have a handle on your window, assuming it's a Window object (e.g. a JFrame or other kind of window), you can listen to WindowEvent events. Here is an example with windowClosed, you can replace it with windowClosing if you need to intercept it before.
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
// do something here
}
});
final JFrame f = new JFrame("Good Location & Size");
// make sure the exit operation is correct.
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent we) {
// pop the dialog here, and if the user agrees..
System.exit(0);
}
});
As seen in this answer to Best practice for setting JFrame locations, which serializes the frame location & size before exiting.
Go stepwise:
Declare a boolean variable saved and set its default value to false.
When user saves the file, change it to true
When exit button is pressed, check the variable.
If true, exit, else, prompt user for saving file.
So, finally this code snippet looks like:
public boolean saved = false;
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saved = true;
//Code to save file
}
});
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(saved)
System.exit(0);
else {
//Code to prompt user to save file
}
}
});

Customize JOptionPane

I was thinking about to change JOptionPane.ShowMessageDialouge(null,"Output"); in JAVA. Basically i need to add print button along ok button on dialogue so if user click on ok simply close it or if click on print button then proceed to actionevent against print button.
You just have to use the showOptionDialog method. Have a look at the Oracle documentation.
Here is the solution for closing the dialog when clicking "OK" : JOptionPane cancel button.
Exemple where you have custom buttons :
JButton but_print = new JButton("PRINT");
JButton but_ok = new JButton("OK");
but_ok.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//Close the dialog
}
});
JOptionPane.showOptionDialog(null, "Test message", "Test", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{but_print,but_ok}, but_ok);
Thanks for help guys. I just found even better thing and it is working right as i wanted.
int response = JOptionPane.showConfirmDialog(null, s+"Print?",cnicstring,JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
//System.out.println("Pressed NO Button");
} else if (response == JOptionPane.YES_OPTION) {
// System.out.println("Pressed Yes Button");
txtarea.print();
} else if (response == JOptionPane.CLOSED_OPTION) {
//System.out.println("JOptionPane closed");
}

Remove button with ActionListener from JFrame

Basically I have a class that extends JFrame, and I'm trying to have an ActionListener remove a button after another button has been clicked, it doesn't seem to be working...
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("Registered existing character click...");
getContentPane().remove(loginButton);
frame.remove(newAccountButton);
frame.revalidate();
frame.repaint();
//add(lookUp);
//add(searchButton);
bg.setIcon(rsLgn);
// lookUp.requestFocus();
// setComponentZOrder(searchButton, 0);
// setComponentZOrder(lookUp, 0);
}
});
EDIT: Currently, the button is invisible, so I wouldn't technically know if it was removed or not, but the System.out.println is definitely still outputting the original message of "Registered existing character click..."

How to enable / disable buttons in java, eclipse?

How can I disable buttons with the press of one button and when the task that the button which has been pressed was assigned to has been done then I want all the buttons to be enabled. for example;
UpButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
DownButton.setEnabled(false);
LeftButton.setEnabled(false);
RightButton.setEnabled(false);
System.out.printline("Up Button");
}
});
DownButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
UpButton.setEnabled(false);
LeftButton.setEnabled(false);
RightButton.setEnabled(false);
System.out.printline("Down Button");
}
});
LeftButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
DownButton.setEnabled(false);
UpButton.setEnabled(false);
RightButton.setEnabled(false);
System.out.printline("Left Button");
}
});
RightButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
DownButton.setEnabled(false);
LeftButton.setEnabled(false);
UpButton.setEnabled(false);
System.out.printline("Right Button");
}
});
I have tried to do it but it does not work. What I want is, for example, my Upbutton is printing out ("Up button"), so when a user presses this button I want all the other buttons to be disabled until it has finished doing the command that it was suppose to do, in this case print out a text but later on I will be adding things like adding up two user inputs e.g. I will have a button that will ask the user to type number 1 and then number 2 and then the computer will add them up and during this process I want all the buttons to be disabled expect the one that has been clicked on, until the user has given all numbers and the computer has given the output.
I hope I have explained myself properly, if not please let me know and I will try my best to give more information. Thanks in advance.
How about using Command objects? Each command object might have execute() and canExecute() methods at least. Then;
btnA.setEnabled( cmdA.canExecute() );
btnA.addActionListener( event-> {
cmdA.execute();
btnB.setEnabled( cmdA.canExecute() );
});
That seems right. Maybe you want them invisible instead of disabled...
In that case:
LeftButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DownButton.setVisible(false);
UpButton.setVisible(false);
RightButton.setVisible(false);
System.out.printline("Left Button");
HereĀ“s an example of something that works:
private void jButtonChangeChampActionPerformed(java.awt.event.ActionEvent evt) {
jComboBoxPlayer.setEnabled(true);
jComboBoxChampion.setEnabled(true);
jButtonSelecionar.setVisible(true);
jButtonMagias.setVisible(false);
}
What if you try this:
UpButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
UpButtonActionPerformed(evt);
}
});
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DownButton.setEnabled(false);
LeftButton.setEnabled(false);
RightButton.setEnabled(false);
System.out.printline("Up Button");
}
It works here...
If you want to Set the Button Disabled , You can use the
btnExample.setDisable(true); // This will disable the Button
btnBreak.setVisible(false); // This will make the button Disappear visually only.
Hope this help guys.
You have to first disable the other buttons, finish your task and then enable the buttons again.
For example:
UpButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
// first disable all other buttons
DownButton.setEnabled(false);
LeftButton.setEnabled(false);
RightButton.setEnabled(false);
System.out.println("Up Button");
// do rest of your tasks here.
// now enable them again
DownButton.setEnabled(true);
LeftButton.setEnabled(true);
RightButton.setEnabled(true);
}
});
Similarly, setup the Listeners for the other buttons.
Hope this helps!

Resources