-3

I want to generate multiple panels (with title, desc and detail button), this is the general structure:

enter image description here

But when I put this code in a for loop it always adds the last item.

Panel panel_1 = new Panel();
panel_1.setBounds(23, 134, 378, 208);

JLabel lblNewLabel_2 = new JLabel("desc");
lblNewLabel_2.setBounds(0, 69, 189, 69);

JButton btnNewButton_2 = new JButton("Details");
btnNewButton_2.setBounds(104, 139, 189, 69);
panel_1.setLayout(null);

JLabel lblPrv = new JLabel("title");
lblPrv.setBounds(104, 0, 189, 69);
panel_1.add(lblPrv);
panel_1.add(lblNewLabel_2);

JLabel label_1 = new JLabel("");
label_1.setBounds(0, 69, 189, 69);
panel_1.add(label_1);

panel_1.add(btnNewButton_2);

Any suggestion?

user1803551
  • 12,965
  • 5
  • 47
  • 74
Fr4ncx
  • 356
  • 6
  • 22
  • 1
    Please don't add tags into the title. I removed them for you. – Tom Dec 22 '15 at 01:51
  • 3
    Use a combination of layout managers, see [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) for more details – MadProgrammer Dec 22 '15 at 01:53
  • 2
    Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Dec 22 '15 at 05:05
  • 1
    *"I want to generate multiple panels (with title desc and detail button)"* Use a `JList` with an appropriate `ListCellRenderer` .. – Andrew Thompson Dec 22 '15 at 05:07

1 Answers1

1

Here is an example of how to place multiple panels next to each other:

enter image description here

public class Example extends JFrame {

    Example() {

        setLayout(new FlowLayout());
        for (int i = 0; i < 4; i++)
            add(new MyPanel());

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    class MyPanel extends JPanel {

        MyPanel() {

            setLayout(new BorderLayout());
            add(new JLabel("title", SwingConstants.CENTER), BorderLayout.PAGE_START);
            add(new JLabel("<html>WWWWWW WWWWWWW<p>WWWWWWWWW<p>RRRRRRR RRRRR RRRRRRR"));
            add(new JButton("Details"), BorderLayout.PAGE_END);
        }
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new Example());
    }
}

You should pick the layout managers best suited for your situation.

user1803551
  • 12,965
  • 5
  • 47
  • 74