Reputation: 17
I am very new and I have no idea how to set the color in this same line of code, I need it to be this line because it is covered by a loop while or if there are other ways, I would appreciate it if you told me, thanks.
add(new JButton(new PersonAction(new Person(miResultSet.getString("name"), miResultSet.getString("identification"))),setBackground(Color.yellow)));
Upvotes: 0
Views: 40
Reputation: 18568
If you want to do it in an uncluttered way, use more lines than one and create every object needed in a single statement:
while (someConditionIsTrue) {
// create a Person passing some parameters
Person p = new Person(miResultSet.getString("name"),
miResultSet.getString("identification"));
// create a PersonAction with the recently created person as parameter
PersonAction pa = new PersonAction(p);
// create the JButton passing the PersonAction as parameter
JButton jb = new JButton(pa);
// set the background of the JButton
jb.setBackground(Color.YELLOW)));
// add it to wherever it is to be added
someThing.add(jb);
}
This will make it much easier to read and debug…
Upvotes: 1
Reputation: 658
Or you can try this...
while (miResultSet.next()) {
Person person = new Person(miResultSet.getString("name"), miResultSet.getString("identification"));
PersonAction action = new PersonAction(person);
JButton actionButton = new JButton(action);
actionButton.setBackground(Color.yellow);
// set other properties if you need
add(actionButton);
}
P.S. I guess condition in while loop. Really - I don't know exacly.
Upvotes: 0
Reputation: 488
Try this
add(new JButton(new PersonAction(new Person(miResultSet.getString("name"), miResultSet.getString("identification")))).setBackground(Color.yellow));
You have not constructed jbutton properly, more over you have used comma to access property instead of dot operator.
Upvotes: 0