Adax
Adax

Reputation: 17

I need to set the JButton color almost in this way, Help:

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

Answers (3)

deHaar
deHaar

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

gooamoko
gooamoko

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

AB D CHAMP
AB D CHAMP

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

Related Questions