Have a main frame that has a button.
Once that button on the main frame gets clicked, a custom dialog box with two buttons appears(setVisible(true)). That dialog box has a bunch of textboxs, spinners, radio buttons... One of the buttons on that dialog is called "Cancel".
What I'm trying to do is make it so when I click Cancel, all of the fields get cleared/reset to default values and then close the dialog(setVisible(false)).
Problem is it doesn't reset radio buttons and also I get exception when the dialog tried to reset the date because of IllegalArumentException(so I removed it).
Also is there a Date control in NetBeans? I use spinner with modified model but it's a little awkward, plus it makes it hard to reset the date?
private void btnAcceptActionPerformed(java.awt.event.ActionEvent evt) {
String err = "";
if(txtFirstName.getText() == "")
err += "First Name is required";
if(txtLastName.getText() == "")
err += "Last Name is required";
if(txtId.getText() == "")
err += "Id is required";
javax.swing.JOptionPane.showMessageDialog(this.CreateReservation, err);
}
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
txtFirstName.setText("");
txtLastName.setText("");
spinAge.setValue(18);
txtId.setText("");
radio1.setSelected(false);
radio2.setSelected(false);
DialogCustom.setVisible(false);
}
Date Control: check out JXDatePicker
Design issue: why reset the fileds when the dialog is closed?
Why not set the proper state before it's shown?
(just my opinion)
radio1.setSelected(false) should work IMO - there must be another issue.
Related
I have a pop-up which opens when clicking the button printName. The pop-up has a check-box. The check-box when checked prints name in the pop-up and when unchecked clears name from the pop-up.
The problem happens when the pop-up is closed and reopened. The checkbox irrespective of being default checked does not invoke the actionListener(does not print the name).
So now I am trying to invoke the printMyName function just as the pop-up is generated from the listener initialized for the pop-up (printName) button
printName pop-up structure -
public NameDisplayPanel
(
NameEvent name,
NameDisplayPanelListener listener
)
{
this.name = name;
this.listener = listener;
//Some code
}
Here is the code to invoke printMyName when the check-box is toggled -
CB = new JCheckBox("Display Selected", false);
CB.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (((JCheckBox) e.getSource()).isSelected()) CB.setSelected(true);
else CB.setSelected(false);
printMyName();
}
}
);
CB.setSelected(true); //Checks the checkbox true everytime the window reopens
// Trying to implement method to invoke printMyName everytime when NameDisplayPanel pop-up is created.
Any leads for how to implement that listener?
Sets the state of the button. Note that this method does not trigger an actionEvent. Call doClick to perform a programmatic action change.
API Java Doc
Trying to write a Swing GUI. When clicking a button, I want to test if the other button has already been clicked. If it has, then execute the statements in the "if" statements. But it looks like the "if" statements never execute?
private void radSingleBurgerActionPerformed(java.awt.event.ActionEvent evt) {
if(radDoubleBurger.isSelected()){
newItemPrice = Double.parseDouble(lblItemPrice.getText());
newItemPrice -= doublePrice;
lblTest.setText(String.valueOf(newItemPrice));//test to see if working
}
lblItemPrice.setText(String.valueOf(newItemPrice += singlePrice));
}
private void radDoubleBurgerActionPerformed(java.awt.event.ActionEvent evt) {
if(radSingleBurger.isSelected()){
newItemPrice = Double.parseDouble(lblItemPrice.getText());
newItemPrice -= singlePrice;
lblTest.setText(String.valueOf(newItemPrice));//test to see if working
}
lblItemPrice.setText(String.valueOf(newItemPrice += doublePrice));
}
Clicking a JButton does not make it (permanently) selected. Clicking is only a temporary action.
Maybe you want to use a JToggleButton. This allows you to click a button to make it selected and then click it again to make it unselected.
Read the section from the Swing tutorial on How to Use Buttons for more information.
Or if you just want to know if the user has clicked on a regular JButton, then you will need to maintain a Boolean variable yourself that you update in the ActionListener of the button.
In the below code, I have a label named card with a mouse click event. I only want the click event to implement once. Meaning it will implement the first time I click the label, but not the following times. How do I do this? I imagine I must disable its Listener.
private void cardMouseClicked(java.awt.event.MouseEvent evt) {
// displays backside of each flashcards when label (flashcard) is clicked
i++;
card.setText(cardB[i]);
}
I think we all would do the same.
It's really simple. Just declare a boolean then change its status when you click the first time.
boolean labelClicked = false;
private void cardMouseClicked(java.awt.event.MouseEvent evt) {
// displays backside of each flashcards when label (flashcard) is clicked
if(!labelClicked){
i++;
card.setText(cardB[i]);
labelClicked=true;
}
else{
//doNothing
}
}
I am pretty new to programming and am trying to make a Minesweeper GUI. The game worked perfectly right clicking a JToggleButton displayed a "B" for bomb on the button, but when I replaced the setText() with setIcon() in the mouselistener it shows the icon when both left and right clicking occurs. I didn't have this problem when setText().
public void mousePressed(MouseEvent e){
if(e.isMetaDown())
if(btnPresses == 0)
{
startTime = System.currentTimeMillis();
btnPresses++;
}
//if(btn[y][x].getText().equals("B"))
if(btn[y][x].getIcon()==flag)
{
//btn[y][x].setText("");
btn[y][x].setIcon(null);
if(bombs[y][x]!=BOMB)
markers++;
}
else
{
//btn[y][x].setText("B");
btn[y][x].setIcon(flag);
if(bombs[y][x]==BOMB)
markers++;
else
markers--;
}
I added a btn[y][x].setIcon(null) to the actionlistener, which causes the flag icon to appear only briefly when left clicking but I'd rather it not appear at all.
You need to distinguish between a left mouse button click, MouseEvent.BUTTON3, and a right mouse button click, MouseEvent.BUTTON3, and then act accordingly. For example, when I did something like this, I set the "flag" boolean in my model (using MVC) via:
#Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
model.upDateButtonFlag();
}
}
The MouseListener should be used only to set or unset the flag. Otherwise you should have your JButton respond via its ActionListener for left button clicks.
Add a System.err.println("" + System.currentTimeMillis() + " " + e); to the beginning of your handler. I strongly suspect your code is being called more times than you think - as a single click can generate multiple events. Once you know what is going on, it should be easy to fix.
This is a question on how to detect which button was clicked in the MessageBox/Dialog.
GXT 2.1 or 2.2 only. Please do not answer using GXT 3.
Ideally, this is how I could do a confirm dialog.
final MessageBox box = MessageBox.confirm(
"Confirm kill avatar",
"Please remove " + getAvatar().getName(),
new Listener<MessageBoxEvent>()
{
#Override
public void handleEvent(MessageBoxEvent be)
{
Button clicked = be.getButtonClicked();
if (clicked == box.getDialog().getButtonById("yes"))
deleteAvatar();
else
Info.display("Action cancelled");
}
});
However. since box has not been defined, box.getDialog() would be NPE,
and compiler preempts that by croaking "box not initialised",
and cannot initialise because box has to be final,
box has to be final because it is used in the anon Listener class.
Instead, I have to compare buttons using the button text. Which is not i18n friendly. Very bad practice.
#Override
public void handleEvent(MessageBoxEvent be)
{
Button clicked = be.getButtonClicked();
if (clicked.getText().equals("Yes")))
deleteAvatar();
else
Info.display("Action cancelled");
}
In GXT 2.2, is this the recommended way? Or is there a better way to detect button being pressed, i18n-friendly?
I SHOULD compare buttons NOT the text of the buttons.
You can use:
if (Dialog.CANCEL.equals(be.getButtonClicked().getItemId())) {
//do action
}
Never mind.
I should simply construct my own confirm/alert/etc from Dialog and provide my own submit/cancel buttons with the appropriate listeners.
Messagebox is but a sandbox/example on how to do simple gxt dialogs.