Add two buttons if another button has been pressed - java

b.button1 = new JButton("Deal");
b.button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
b.button2 = new JButton("Hit");
panel.add(b.button2);
panel.validate();
b.button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
}
});
b.button3 = new JButton("Stay");
panel.add(b.button3);
panel.validate();
b.button3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//code
}
}
});
So I want The Buttons Hit and Stay to be added as soon as the Deal button has been pressed. I searched for a solution and found the panel.validate() method. I used it but now if I press the Deal button it only adds the Hit button.

You could add the buttons before and make them "hidden". If you press the button, you can "show" them to include them.

Related

Java Swing how to define multiple focus listeners on JTextFields but with different button actions

I am making JTextFields which should be populated when click on a button.
Suppose for an example:
txtField1.addFocusListener(new FocusListener() {
JTextField field = txtField1;
#Override
public void focusGained(FocusEvent e) {
btnMain_0.addActionListener(ee -> {
if (field.getText().length() > 4)
return;
else
field.setText((field.getText() + "0"));
});
}
#Override
public void focusLost(FocusEvent e) {
}
});
txtField2.addFocusListener(new FocusListener() {
JTextField field = txtField2;
#Override
public void focusGained(FocusEvent e) {
btnMain_0.addActionListener(ee -> {
if (field.getText().length() > 4)
return;
else
field.setText((field.getText() + "0"));
});
}
#Override
public void focusLost(FocusEvent e) {
}
});
But if I click on txtField1 and press btnMain_0, it enter 0.
Then if I click on txtField2 and press btnMain_0, it enter 00 (it considered pressing btnMain_0 two times).
How I can make it? Is there better solution two run listeners from list of jtextfields?
You can define a custom TextAction and add it to your buttons.
The TextAction allows you to track the last text component that had focus (before you click on the button).
Something like:
class KeyboardAction extends TextAction
{
private String letter;
public KeyboardAction(String letter)
{
super(letter);
this.letter = letter;
}
public void actionPerformed(ActionEvent e)
{
JTextComponent component = getFocusedComponent();
component.setCaretPosition( component.getDocument().getLength() );
component.replaceSelection( letter );
}
}
You then use the class like:
jButton1 = new JButton( new KeyboardAction("1") );
jButton2 = new JButton( new KeyboardAction("2") );
or you add the Action to an existing button by using:
button.setAction( new KeyboardAction("1") );

Adding a 3rd button to JOptionPane drop down menu

I am relatively new to Java and have been trying to figure out a way to create a JOption window that not only has a drop down option with an "Ok" and "Cancel" button, but also adds an additional button called "Back." So far none of my attempts have successfully been able to add this Back button and every time I run the code it simply brings up the traditional dropdown/ok/cancel type window. Additionally I would also like the window to close when a button has been clicked, but I have been unsuccessful in that as well. Here is the code I have so far, not entirely sure what's missing/wrong with it:
public static String sampleWindow(){
JButton jbt_ok = new JButton("OK");
JButton jbt_back = new JButton("Back");
JButton jbt_cancel = new JButton("Cancel");
boolean greyOutBackButton = false;
jbt_ok.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("OK was clicked");
}
});
jbt_cancel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Cancel was clicked");
}
});
if(greyOutBackButton)
jbt_back.setEnabled(false);
else
jbt_back.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Back was clicked");
}
});
Object[] options = {jbt_ok, jbt_back, jbt_cancel};
Object selectionObject = JOptionPane.showInputDialog(null, "message", "", JOptionPane.DEFAULT_OPTION, null, options, options[0]);
if (selectionObject == null)
System.exit(0);
String selectionString = selectionObject.toString();
System.out.println("Selection String: "+ selectionString);
return selectionString;
}

Not able to call an action listener for a radio button

I am writing a code where there is a tabbedpane containing 4 tabs. Each tab is containing some radio buttons and a button. the button should act according the pressed radio button. Following is my code for it.
if(radiobtn1.isSelected()==true)
` {
System.out.println("option 1 selected");
radiobtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// action for radio button 1
}
});
}
});
}
else if(radiobtn2.isSelected()==true){
System.out.println("option 2 selected");
radiobtn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// action for radio button 2
}
});
}
});
}
}
But whenever I am pressing a radio button its action listener is not getting called. Is there anything wrong in my code?
Modify your code as below
rdbtnSample.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if (rdbtnSample.isSelected())
System.out.println("Button Pressed");
else
System.out.println("Button Released");
}
});
add the if condition inside the action performed.
By default radio button will be deselected.
I think you should use the data or the switch parameters to control the action content. Not by the dynamic control the action listener in the buttons.
Just like use the
int radioButtonIndexPressed = 0;
and in the button press action , use the radioButtonIndexPressed to decide the real action.

How to assign a method to a button when pressed or released with ActionListener in Java?

I am new to coding GUIs in Java and I am trying to just print a message on the terminal when a button is pressed and another one when it is released.
This is what I have for a regular button pressing.
leftButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Pressed");
}
});
I did this with the help of IntelliJ IDEA. I want to make the button send a message when pressed and a different thing when released.
You can just add a simple MouseAdapter, like this:
MouseAdapter ma = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("Pressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Released");
}
};
leftButton.addMouseListener(ma);
frame.add(button);
This will detect when it is the mouse is pressed on the button or released on the button.
If you want, you can also add a mouseClicked() method, mouseExited(), mouseEntered(), mouseMoved(), and (many) more methods in your MouseAdapter. Check out this JavaDoc
Use custom class and use it
leftButton.getModel().addChangeListener(new BtnCusttomListener());
private class BtnCusttomListener implements ChangeListener {
private boolean pressed = false; // holds the last pressed state of the button
#Override
public void stateChanged(ChangeEvent e) {
ButtonModel Buttonmodel = (ButtonModel) e.getSource();
// if the current state differs from the previous state
if (model.isPressed() != pressed) {
String text = "Button pressed: " + model.isPressed() + "\n";
textArea.append(text);
pressed = model.isPressed();
}
}
}
You can use a MouseListener instead:
leftButton.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
// The mouse button was pressed and released
}
#Override
public void mousePressed(MouseEvent e) {
// The mouse button was pressed
}
#Override
public void mouseReleased(MouseEvent e) {
// The mouse button was released
}
#Override
public void mouseEntered(MouseEvent e) {
// The cursor entered the bounds of the button (i.e. hovering)
}
#Override
public void mouseExited(MouseEvent e) {
// The cursor exited the bounds of the button
}
});

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!

Categories