0

i want to make a gui with swing. In my ContentPane i want to have 3 panels. So far, so good. In the middle one, i want to show a dynamic number of panels, where every one has on checkbox an a textfield in it. With fixed sizes it looks ok. But if the number of those panels increase, they wont be shown up. So i need to have something scrollable...i tried with JScrollPane for hours now, but dont get it running. Please, you surely have a suggestion!

My code so far...

    frame = new JFrame("Caller GUI");
    frame.getContentPane().setLayout(null);
    frame.setPreferredSize(new Dimension(520, 300));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel_1 = new JPanel();
    panel_1.setBounds(0, 0, 503, 70);
    panel_1.setBorder(BorderFactory.createLineBorder(Color.black));
    frame.getContentPane().add(panel_1);
    panel_1.setLayout(null);

    panel_2 = new JPanel();
    frame.getContentPane().add(panel_2);
    panel_2.setBounds(0, 70, 503, 120);
    panel_2.setBorder(BorderFactory.createLineBorder(Color.black));
    panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    Dimension panelDim = new Dimension(500, 30);
    Dimension cbDim = new Dimension(60, 30);
    Dimension tfDim = new Dimension(150, 20);       

    for(int i = 0; i < fleets.size(); i++) {
        JPanel panel = new JPanel();
        panel.setPreferredSize(panelDim);

        fleets.get(i).getCheckBox().setPreferredSize(cbDim);
        panel.add(fleets.get(i).getCheckBox()); 
        fleets.get(i).getTextField().setPreferredSize(tfDim);
        panel.add(fleets.get(i).getTextField());            

        i++;

        fleets.get(i).getCheckBox().setPreferredSize(cbDim);
        panel.add(fleets.get(i).getCheckBox());
        fleets.get(i).getTextField().setPreferredSize(tfDim);
        panel.add(fleets.get(i).getTextField());

        panel_2.add(panel);
    }

    panel_3 = new JPanel();
    panel_3.setBounds(0, 190, 503, 71);
    panel_3.setBorder(BorderFactory.createLineBorder(Color.black));
    frame.getContentPane().add(panel_3);
    panel_3.setLayout(null);

thanks a lot!

D.Schall
  • 53
  • 6
  • First and foremost, ditch the `null` layouts, and learn and use the layout managers. – Hovercraft Full Of Eels May 23 '18 at 16:56
  • if i kick null Layout from frame, then panel_3 jumps over panel_1. When i am writing here, then i want to learn from your answers, because i dont understand the things i found by searching in the internet. – D.Schall May 23 '18 at 18:11
  • This is not a teaching site but rather a question and answer site. The answer is as per the duplicate, use decent layouts, especially in the JPanel that is receiving the sub-components and that is being displayed in the JScrollPane. – Hovercraft Full Of Eels May 23 '18 at 18:46

0 Answers0