Question

enter image description here

Hi guys,

Above is a screenshot of my frame how it looks like at the moment. I want that the first label PlatformTest1 starts after the red stripe. The red stripe i painted in myself for clarity. I used a Borderlayout with 3 Janels, one at the West, North and at the Center. I fill the North and West Panels with Labels which I get from two lists. In the Center-Panel I wanted a grid with size list1.size() * list2.size(). But I just dont get the first label is added at the Position where the red stripe is. I tried adding a blank label, but that doesnt look great.

Here is my code so far:

public DisplayMapping() {

    final int x = list1.size();
    final int y = list2.size();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100,600, 500);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout(0, 0));

    JPanel panelApps = new JPanel();
    panelApps.setBorder(new LineBorder(new Color(0, 0, 0), 2));
    contentPane.add(panelApps, BorderLayout.WEST);
    panelApps.setLayout(new GridLayout(y, 1));


    JPanel panelHW = new JPanel();
    panelHW.setBorder(new LineBorder(new Color(0, 0, 0), 2));
    contentPane.add(panelHW, BorderLayout.NORTH);
    panelHW.setLayout(new GridLayout(1,x));

    JPanel panelGrid = new JPanel();
    contentPane.add(panelGrid, BorderLayout.CENTER);
    panelGrid.setLayout(new GridLayout(1, 0, 0, 0));

    for(String s : list1)
    {
        JLabel lbl = new JLabel();
        lbl.setText(s);
        panelApps.add(lbl);
        panelApps.revalidate();
        panelApps.repaint();
        contentPane.revalidate();
        contentPane.repaint();
    }

    for(String s : XMLParser.HardwareListGUI)
    {
        JLabel lbl = new JLabel();
        lbl.setText(s);
        panelHW.add(lbl);
        panelHW.revalidate();
        panelHW.repaint();
        contentPane.revalidate();
        contentPane.repaint();
    }

I also tried setting the Bounds of the PanelApps a bit more in x-direction, but didnt work out either. I hope somebody can help here with it.

Thanks in advance.

Was it helpful?

Solution

What you could do is put both your current North panel and your current Center panel together inside a panel that is in the Center. So you would have something like this:

 _____________________________________________________
|             | Center Panel (also BorderLayout)      |
|panelApps    |  ___________________________________  |
|             | | panelHW                           | |
|             | |___________________________________| |
|             | | panelGrid                         | |

That way your apps panel will go to the top of the content pane, and your HW panel will go to the top as well, but will be beside the apps panel.

The "border" of the Center Panel I have put around the HW and Grid panels won't actually show up since the panels will stretch to the edge of the "Center Panel". I have just drawn it like this to show the "hierarchy" of the panels.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top