简体   繁体   中英

Using instanceof to change color of JLabels

I am looking for away to change the text color of all my JLabel s with a function so I don´t have to use the setForegroundColor for each and every one of them.

I currently have a bunch of JLabel s in a panel called Main . I did a bit of research and came across the instanceof and getComponents method. So I've come this far:

main = new JPanel();
    main.setBackground(Color.red);
    tf_search = createTF();
    l_name1 = new JLabel("Name: "+ DB.findUser(1001).returnName());
    l_nick = new JLabel("Nick: " + DB.findUser(1001).returnNick());
    l_style = new JLabel("Style: ");
    l_styleshow = new JLabel(DB.findUser(1001).returnStyle());
    l_music = new JLabel("Favourite songs: ");
    l_musicshow1 = new JLabel(DB.findUser(1001).returnMusic1());
    l_musicshow2 = new JLabel(DB.findUser(1001).returnMusic2());
    l_musicshow3 = new JLabel(DB.findUser(1001).returnMusic3());
    l_blank = new JLabel("");
    l_blank2 = new JLabel("");
    l_inst = new JLabel("Instrument: ");
    l_instshow = new JLabel(DB.findUser(1001).returnInst());
    l_band = new JLabel("Band: ");
    l_bandshow = new JLabel(DB.findUser(1001).returnBand());
    b_search  = new JButton("Sök");
    b_musicchn = new JButton("Edit Profile");
    b_return = new JButton("Return to profile");


    main.setLayout(new GridLayout(9,2));
    main.add(l_name1);
    main.add(l_nick);
    main.add(l_style);
    main.add(l_styleshow);
    main.add(l_music);
    main.add(l_musicshow1);
    main.add(l_blank);
    main.add(l_musicshow2);
    main.add(l_blank2);
    main.add(l_musicshow3);
    main.add(l_band);
    main.add(l_bandshow);
    main.add(l_inst);
    main.add(l_instshow);
    main.add(b_search);
    b_search.addActionListener(new searchHandler());
    main.add(b_musicchn);
    b_musicchn.addActionListener(new editHandler());
    main.add(tf_search);
    main.add(b_return);
    b_return.addActionListener(new returnHandler());

And all the Panels and stuff are declared, or what to call it. ex "private JLabel l_nick, etc"

So I thought this might select all JLabels and turn the text to white, but my code doesn't work hehe. Is this a legit way of doing things and can you correct it, or does someone know another way. Thanks in advance!

Note: I am a student and this is for my final project in my first programming year, so I just want the code variety. If it is not possible with a massive, advanced block of code don't bother typing it out, even though your help is appreciated!

don't do this:

main.getComponents(l_label instanceof JLabel).setForegroundColor(Color.White);

instead define a List<JLabel> mylabels = ... populate the list:

myLabels.add(l_label);
myLabels.add(l_label2);
myLabels.add(l_label3);

and do a for enhanced

for(JLabel x:myLabels){

    x.setForegroundColor(Color.White);

}

You may retrieve all child components, check if they are JLabel , and set the color accordingly .

    for (Component component : panel.getComponents()) {

        if (component instanceof JLabel) {

            component.setForeground(Color.WHITE);
        }

    }

I don't know of a method getComponents that takes a filter argument. But you could use streams to filter. Something like this:

Stream.of(main.getComponents()).filter(component -> component instanceOf JLabel).forEach(label -> ((JLabel)label).setForegroundColor(Color.White));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM