Changing the properties of a JButton through references - java

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).

Related

how to identify a button in a group of loop generated buttons?

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

How do I get input from buttons I created using a loop?

I'm trying to make a simple calculator in Java using Swing, and I've created my buttons the following way:
//Our number keypad
public static JPanel numbers(){
//our panel to return
JPanel panel = new JPanel();
//Create and add 3x4 grid layout to panel
GridLayout gl = new GridLayout(3, 4);
panel.setLayout(gl);
//For creating and adding buttons to panel
for(int i = 0; i < 10; i++){
//Create a new button where the name is the value of i
String name = "" + i + "";
JButton button = new JButton(name);
//add action listener
button.addActionListener(handler);
//Add button to panel
panel.add(button);
}
return panel;
}
My question is how do I reference each specific button in my event handler? I can't think of a way to do this without having to manually create each button rather than using a loop.
Thanks.
In your listener, call event.getSource(), and that will return the button which has been pressed. Get the text of the button, and you have its number.
Or create a different instance of your handler for every button, and pass the value of the button (i) to the constructor of the handler. This last solution is cleaner, IMO, because it doesn't depend on the text of the button. If you replaced the text by an image, for example, the first technique wouldn't work anymore.
You can distinguish created buttons by adding the following inside handler:
String buttonText = ((JButton) e.getSource()).getText();
if (<text1>.equals(buttonText)){
//do the stuff
} else if (<text2>.equals(buttonText)){
//do the stuff
} else {
//do the stuff
}
Method #1: go through the child components of the parent JPanel (quite tedious, has to be rebuilt every time you modify the contents of that JPanel). Make sure they're JButtons by using an if . . instanceof clause.
Method #2: as you create them in that loop, add them to a List (or even better, a Map). I prefer a Map personally as it lets me customise the key for that specific JComponent
i.e.
HashMap<String, JComponent> buttonList = new HashMap<String, JComponent>();
for(. .) {
buttonList.put("nameForEachButton", button);
}
I recommend generating the button name based off of the loop counter. Either use your existing name value, or just set it to "button" + i;
Declare your buttons using an array.
JButton[] button = new JButton[9]; //outside the for loop
for (int i = 0; i < 10; i++) {
//put your code
button[i] = new JButton(name);
//your code
}

Java set buttons names automatically

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.
}

How to rename set of JButtons?

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());
}

Array of buttons, make only one button change its text when clicked

I have an array I fill with buttons and I want an individual button to change its text when clicked.
for (int i = 0; i<4; i++)
{
button[i] = new JButton ("Add");
button[i].addActionListener(this);
box[i] = new JComboBox();
foodOptions.add(box[i]);
foodOptions.add(button[i]);
}
public void actionPerformed (ActionEvent e)
{
button[this].setText("I've been clicked!");
}
The current doesn't work because of incompatible types, what format is appropriate?
Yes, it makes no sense to pass an object, this, into an array index which expects an int, not your GUI object, so I'm not sure what you were trying to achieve with this.
Just get a reference to the JButton that's been clicked from the ActionEvent's getSource() method:
JButton btn = (JButton)e.getSource();
btn.setText("I've been clicked");
Edit:
Also you should avoid using this as your ActionListener since this means that you're likely having your GUI class implement an ActionListener which is asking the poor class to be too many things, to do too much. You're much better off either using anonymous inner classes or else even better use AbstractActions.

Categories

Resources