I'm new in Java swing, and I have a problem. I made for-loop for creating buttons and now I want automatically give them names or some kind of marks for future recognition (I will need name of clicked button to make it a variable).
How can I give them names in my loop? Thank you.
Here is code of my for-loop:
for (int aa=1; aa<65; aa++)
{
JButton button = new SquareButton("");
gui.add(button);
button.addActionListener((ActionListener) button);
}
I will need name of clicked button to make it a variable).
You don't need a variable to work with the clicked button. Instead you get a reference to the button that was clicked from the ActionListener code:
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
// do processing on the clicked button.
}
Related
I have 3 buttons
b1
b2
b3
I want to now have these buttons be pressed in turns.
So turn one I press and turn 2 another person presses.
So after turn two, I will compare the names of the buttons.
b1.addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent event ) {
b1.setEnabled(false);
if (!b1.isEnabled() && !b2.isEnabled()) {
//computeWinner(b1.getText(), b2.getText());
} else if(!b1.isEnabled() && !b3.isEnabled()) {
//computeWinner(b1.getText(), b2.getText());
}
}
});
This was what I thought would work, but there are many things wrong with this,
First, since I disable the buttons the second user always has one less option. and second the if statements do not seem to work? how should I compare the
JButton b3 = new JButton ("hello"); <- hello lable of the buttons?
EDIT:
I was able to successfully compare the two buttons. Now my only problem is that for the second player one of the buttons are disabled(how can I capture the first button press and the second without disabling them?). And that after the comparison I don't know how to reset the board to go again. (for a set number of loops.)
Thank you for the help!
The following code will print the label of the button which has been pressed. I hope, you should be able to proceed from here. Feel free to let me know if you face any further issue.
ActionListener actionListener = new ActionListener(){
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(actionEvent.getActionCommand());
}
};
There are several options.
Store the buttons in a map<Integer, String>. the integer would be a count for keeping track of pushes. The string would be the actionCommand of the button pressed.
Store the button actionCommands in a list or array.
In either of the above you can provide appropriate logic to compare the buttons and then reset the arrays or map and count.
Note: The actionCommand defaults to the button label unless it explicitly set.
I'm making a simple calculator in GUI and I have all the code typed and ready, but I'm having trouble with making it so that when the user presses a number button, the respective number appears in the text box above. Do I need to use a radio button? Thanks in advance
I've tried action listeners but they didn't work (or I'm probably using them incorrectly). I've put the code for the 1 button.
JButton num1 = new JButton("1");
num1 = b1;
num1.setSize(50,50);
num1.setLocation(20,200);
num1.setBackground(Color.WHITE);
num1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Just append the number to the JTextField(using setText(String) and getText()) in the actionPerformed function.
I have a group of loop generated buttons made with this code
this.panelCuerpo.setLayout(new GridLayout(4,5));
for(int i = 1; i<=20; i++){
final JToggleButton b = new JToggleButton(new ImageIcon("/images/available.png"));
panelCuerpo.add(b);
b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/available1.png")));
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt){
if(b.isSelected()){
b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/busy1.png")));
cantidadBoletas++;
}else{
b.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/available1.png")));
cantidadBoletas--;
}
System.out.println(cantidadBoletas);
}
});
}
The problem here is that I can't use setText() to compare later cause there's no property to hide that text. How can I compare it later?
PS. Each button has a consecutive number, it's easy to assign that number. The real problem lies in where to put it.
You could:
Use the Action API, which lets you trigger the selected state of the associated button. This allows you to de-couple the button from the underlying "action" it should take. Take a look at How to Use ActionsHow to Use Actions for more details
Use the actionCommand property of the JButton. This allows you to have some kind of "identifier" associated with the button which is independent of the text
Use an array or List to maintain a reference to the buttons
You can maintain a List<JToggleButton> of JToggleButton and fetch element later by the index. Apart from that instead of adding ActionListener in loop you can implement ActionListener which can be used for all buttons and you just need to write b.addActionListener(this); in loop.
NOTE : better to start from i = 0 instead of 1
I'm trying to a make a reference to the last button that is clicked. When this button is clicked i want to store it and then be able to manipulated it. So then what ever the user clicks I can go back and change it. Is it possible to change the properties of a Jbutton through the use of references?
Something like;
JButton original = new JButton(" This is text");
JButton lastbuttonclicked;
lastbuttonclicked = original;
lastbuttonclicked.setText("This new text is this");
System.out.println(original.getText());
DESIRED OUTPUT:
"The new text is this"
I feel like the issue is that I am just creating a new JButton, but i never substantiate the new object.
How would I change a button using references?
Warning: pseudocode.
private Button[] lastButton = new Button[1];
public void actionPerformed(ActionEvent e) {
// the button that has been clicked
lastButton[0] = (Button) e.getSource();
}
Then just lastButton[0] whenever you want to access it. The array should store the reference and not an object, as best I know (for complete assurance, use an ArrayList).
I am a beginner in java, I want to know is there any possible way to control a loop by clicking a button? I am creating a GUI, and it's supposed to run 10 times in the loop. Is there a way that I could have a button on the screen so that when the user presses, then it goes to the next iteration? Because currently everything just runs and executes once.
In your java class, you should define an attribute and each time you click on the button you add 1 to this attribute and do the action.
define an attribute in your class;
public int i = 0;
and create a button to be clicked on:
private void clickMeButtonActionPerformed(java.awt.event.ActionEvent evt) {
// code your action here:
this.i++;
}
You could have the loop wait for the button click, and then once it loops 10 times break the loop.
You can use javaFX it's gonna replace javaswing very soon anyways plus it's cooler.
import javafx.scene.control.button
Button button = new Button("control");
int i = 0;
button.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
i++;
label.setText("i increased");
}
});