I have 20 Buttons and i want change the color of the all the buttons when i click on a special button, is there any way to do this with function(or without function) and do not using setBackground for 20 times
You can put the buttons in an array
JButton[] array = new JButton[20];
//then add the buttons to the array
Then:
for(JButton button : array){
button.setBackground(/*the color you want*/);
}
You should not do button1, button2, etc.
Instead, make a List<Button> buttons. You'll still have to call add() 20 times on that list, but then you can loop over them all.
for (Button b : buttons) {
b.setBackground(color);
}
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 have made a panel which contains two radio groups of buttons. You can find the result of JPanel in the image below. Then I used the below code to add this panel to a JOptionPane box:
OptionsForDisjunctionNodes optionsForDisjunctionNodes=new OptionsForDisjunctionNodes();
JPanel p=optionsForDisjunctionNodes.getPanel();
int option = JOptionPane.showConfirmDialog(null, p, "Decision on Disjunctive Nodes", JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
if (JOptionPane.OK_OPTION == option) {
// Print selected radio button in each group. How?
} else {
// Do something else.
}
Let's say the name of group boxes are buttonGroup1, and buttonGroup2
I need to when the user click on Ok button, I print the selected label in both groups
Let OptionsForDisjunctionNodes add a listener to the radio buttons. And when pressed have it store whatever option was pressed last.
Then also provide getters on OptionsForDisjunctionNodes to retrieve this information.
Alternatively, you still provide getters in OptionsForDisjunctionNodes but have them loop over the button group to find which option is selected.
Check this stackoverflow question for ideas on how to do that exactly.
You could work with action commands on the JRadioButtons like this:
ButtonGroup buttonGroup1=new ButtonGroup();
JRadioButton r1=new JRadioButton();
JRadioButton r2=new JRadioButton();
r1.setActionCommand("hello");
buttonGroup1.add(r1);
buttonGroup1.add(r2);
your if would then look like this:
if (JOptionPane.OK_OPTION == option) {
System.out.println(buttonGroup1.getSelection().getActionCommand());
System.out.println(buttonGroup2.getSelection().getActionCommand());
} else {
}
for buttonGroup1 it would print hello in this case if r1 is selected
I have a problem with buttons. I found that when i add buttons into ArrayList, I will can manange every added button. But i don't have idea how to use setEnabled after adding it into ArrayList
public ArrayList<Button> buttons = new ArrayList();
buttons.add(newButton)
If you want all of them to setEnabled(true) then iterate through the array list as #DannyDaglas stated.
for(Button button : buttonList)
button.setEnabled(true);
If you want to pick one of them in specific then you can enter the index of the button and setEnabled(true).
int index = 0;
buttonList.get(index).setEnabled(true);
If you are not sure about which index it is and you have objects of the buttons then you can do something like this
int i;
for (i=0;i<buttonList.size();i++){
if(buttonList.get(i).equals(closeButton))
buttonList.get(i).setEnabled(true);
}
Iterate over the array list with:
for(Button button : buttons)
button.setEnabled(true);
I am trying to make a simple memory game. but I don't know how to set up actionPerformed where when 1 button is pressed, then the program waits for another click and checks if the buttons are the same?
Right now all the code does it create random spots for but[x](set up in other method) and when the button is pressed the button changes to the image to correct image
public void actionPerformed(ActionEvent a) {
for(int x = 0; x < 16; x++) {
if(a.getSource() == but[x]) {
but[x].setIcon(imageicon[x / 2]); //Another method creates the imageicons
}
frame.repaint();
}
}
No need to call repaint() if all you're doing is changing a JLabel or JButton's Icon.
You will need your ActionListener to contain a variable, perhaps an Icon variable, that holds the value of the last button's Icon, and let's call it lastIcon. It will initially hold a value of null.
When the first button is pressed, its icon is displayed, it checks the value of lastIcon. If it is null, it knows that this is the first button, and the lastIcon variable is assigned its icon's value.
When the 2nd button is pressed, it displays its icon, checks it against the last one. If equal, it keeps both buttons displayed. If not equal, it starts a Swing Timer that resets the Icons back to default. Either way, it sets the lastIcon to null.
Note that you do not want to check if one button == another button. You want to either compare their Icons, or else have a separate model class that is distinct from your view, and check if the two buttons are equivalent in your model.
I have 25 jButtons and i want to change their texts from a loop. Here is my code for 1 button..
void changeText(){
jButton1.setText(jButton1.getText().toUpperCase());
}
I want to do the same for all other buttons without writing a method for each.
Is it possible to use something like this?
void changeText(){
for(int i=0;i<25;i++){
String x = "jButton"+i;
x.setText(x.getText().toUpperCase());
}
}
Surely this wont work. Please suggest me a method.
You can do this by adding the buttons to a collection.
Something like this:
// initialization of jbuttons:
List<JButton> buttons = new ArrayList<JButton>();
JButton jbutton1 = new JButton();
// .. set properties
buttons.add(jbutton1);
// add more jbuttons to the list
Later you can iterate over the list of buttons:
for (JButton button : buttons) {
button.setText(button.getText().toUpperCase());
}