Multi-colored lists

一笑奈何 提交于 2020-01-06 03:12:12

问题


I have to create 5 Jlists in succession to make their backgrounds look in different colors. My existing code creates these 5 lists without the colors (the default JList). I understand that I can only customize the inside/border of a jlist and not the margin/padding around it? Or Am I wrong and is there a way to it?

Btw, The space above the list is a Jlabel (not shown in the pic above) and the layout managers in use are GroupLayout and GridBagLayout.

UPDATE

AT, as per your suggestion, here is a comparison to how the lists look like when surrounded by a jpanel. The lists in the back are the ones with the Jpanel surrounded with a minimal empty border of size 1.

The issue with creating a JPanel with preferredsize overridden is that the jlists are in horizontal jpanel and above them is another jpanel with labels. So, wrapping jlist in a jpanel is not going to do it.


回答1:


Please do have a look at this code example. Is it closer to what you wanted, else you define the changes that needs to be done. I be on them as soon as I get the reply from your side :-)

Here is the outcome :

import java.awt.*;
import javax.swing.*;

public class JListExample
{
    private JList<String> list1;
    private JList<String> list2;
    private JList<String> list3;
    private JList<String> list4;
    private JList<String> list5;
    private CustomPanel panel1;
    private CustomPanel panel2;
    private CustomPanel panel3;
    private CustomPanel panel4;
    private CustomPanel panel5;
    private String[] data = {"one", "two", "three", "four"};
    private int width = 110;
    private int height = 300;

    private void displayGUI()
    {
        JFrame frame = new JFrame("JList Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 5, 2, 2));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 0.9;

        panel1 = new CustomPanel(width, height, Color.GRAY, "List 1");
        list1 = new JList<String>(data);
        panel1.add(list1, gbc);
        panel2 = new CustomPanel(width, height, 
                 Color.GREEN.brighter().brighter(), "List 2");
        list2 = new JList<String>(data);         
        panel2.add(list2, gbc);
        panel3 = new CustomPanel(width, height, 
                          Color.ORANGE.brighter(), "List 3");
        list3 = new JList<String>(data);                  
        panel3.add(list3, gbc);
        panel4 = new CustomPanel(width, height, 
                            Color.BLUE.brighter(), "List 4");
        list4 = new JList<String>(data);                    
        panel4.add(list4, gbc);
        panel5 = new CustomPanel(width, height, Color.RED, "List 5");
        list5 = new JList<String>(data);
        panel5.add(list5, gbc);

        contentPane.add(panel1);
        contentPane.add(panel2);
        contentPane.add(panel3);
        contentPane.add(panel4);
        contentPane.add(panel5);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new JListExample().displayGUI();
            }
        });
    }
}

class CustomPanel extends JPanel
{
    private final int GAP = 5;
    private int width;
    private int height;
    private Color backgroundColour;
    private JLabel titleLabel;

    public CustomPanel(int w, int h, Color c, String title)
    {
        width = w;
        height = h;
        backgroundColour = c;
        titleLabel = new JLabel(title, JLabel.CENTER);
        setBackground(backgroundColour);
        setBorder(
            BorderFactory.createEmptyBorder(
                                GAP, GAP, GAP, GAP));                   
        setLayout(new GridBagLayout()); 
        titleLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 0.1;
        add(titleLabel, gbc);
    }   

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(width, height));
    }
}

**LATEST EDIT : **

As rightly pointed by @kleopatra (not something that is new for me :-)), too good judgement call. Done the edit related to that below :

import java.awt.*;
import javax.swing.*;

public class JListExample
{
    private final int GAP = 5;
    private JList<String> list1;
    private JList<String> list2;
    private JList<String> list3;
    private JList<String> list4;
    private JList<String> list5;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JPanel panel5;
    private String[] data = {"one", "two", "three", "four"};
    private int width = 110;
    private int height = 300;
    private GridBagConstraints gbc = new GridBagConstraints();

    private void displayGUI()
    {
        JFrame frame = new JFrame("JList Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 5, 2, 2));      

        panel1 = getPanel(Color.GRAY, "List 1");
        list1 = new JList<String>(data);

        panel2 = getPanel(Color.GREEN.brighter().brighter(), "List 2");
        list2 = new JList<String>(data);         

        panel3 = getPanel(Color.ORANGE.brighter(), "List 3");
        list3 = new JList<String>(data);                  

        panel4 = getPanel(Color.BLUE.brighter(), "List 4");
        list4 = new JList<String>(data);                    

        panel5 = getPanel(Color.RED, "List 5");
        list5 = new JList<String>(data);        

        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 0.9;

        panel1.add(list1, gbc);
        panel2.add(list2, gbc);
        panel3.add(list3, gbc);
        panel4.add(list4, gbc);
        panel5.add(list5, gbc);

        contentPane.add(panel1);
        contentPane.add(panel2);
        contentPane.add(panel3);
        contentPane.add(panel4);
        contentPane.add(panel5);

        frame.setContentPane(contentPane);
        frame.setSize(610, 300);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel getPanel(Color c, String title)
    {
        JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBorder(
            BorderFactory.createEmptyBorder(
                                GAP, GAP, GAP, GAP));       
        panel.setBackground(c);
        panel.setLayout(new GridBagLayout());   
        JLabel label = new JLabel(title, JLabel.CENTER);
        label.setAlignmentX(JLabel.CENTER_ALIGNMENT);

        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 0.1;

        panel.add(label, gbc);

        return panel;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new JListExample().displayGUI();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/11963928/multi-colored-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!