0

I have a JButton which generates a JPanel that contains a set of JLabel components. As you can see, the amount of labels can be a bit too much for 600x600 frame to hold. I tried adding JScrollPane, but nothing works for some reason. Changing layout manager to something other than null and adding scroll pane completely breaks the panel and frame just appears completely blank.

createTable.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            ReadFile file = new ReadFile(path);
            String[] aryLines = file.OpenFile();

            JFrame frame2 = new JFrame("Table");
            JPanel panel = new JPanel();

           // JScrollPane scrollPane = new JScrollPane(panel);

            frame2.setSize(600, 300);
            panel.setBounds(0, 0, 600, 600);

            panel.setLayout(null);
            //scrollPane.setPreferredSize(new Dimension(600,300));

            for (counter = aryLines.length; counter > 0; counter-=6) {
                JLabel jLabel1 = new JLabel("Имя: "+ aryLines[counter-6]);
                JLabel jLabel2 = new JLabel("Деталь: " +aryLines[counter-5]);
                JLabel jLabel3 = new JLabel("Дата прибытия заказа: " + aryLines[counter-3]);
                JLabel jLabel4 = new JLabel("Дата заказа детали: " + aryLines[counter-4]);

                JCheckBox jCheckBox = new JCheckBox();
                JTextArea area = new JTextArea(Integer.toString(counter));

                jLabel1.setBounds(15, 5 + (counter - 6)*30, 300, 20);
                jLabel2.setBounds(15,35 + (counter - 6)*30, 300, 20);
                jLabel3.setBounds(15, 70 + (counter - 6)*30, 300, 20);
                jLabel4.setBounds(15, 100 + (counter - 6)*30,300,20);

                //jLabel.setBackground(Color.BLACK);
                panel.add(jLabel1); panel.add(jLabel2); panel.add(jLabel3); panel.add(jLabel4);

                //panel.add(jCheckBox);
            }
           // scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            //scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
           // frame2.getContentPane().add(scrollPane);
            frame2.add(panel);
            //panel.add(scrollPane);
            frame2.setLayout(null);
            frame2.setVisible(true);
        } catch (IOException et){System.out.println("Error in CreateTable doc parser");}
    }
});
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ZandD
  • 1
  • On a different note, I want to ask why Null layout is considered bad? It is the most logical and intuitive layout. – ZandD Mar 02 '21 at 11:48
  • Null layouts are brittle and frankly harder to get correct once you understand [Swing layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). Using a simple FlowLayout is easier than trying to remember where you put all the different Swing components. – Gilbert Le Blanc Mar 02 '21 at 12:08
  • Add an empty `JTable` when the GUI is initially created. When it needs updating, add data to the model. **Use layouts.** 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). Provide ASCII art or a simple drawing of the *intended* layout of the GUI. – Andrew Thompson Mar 02 '21 at 12:32
  • For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data for the table, or get it from built-in sources (e.g. fonts, locales, system properties, Unicode characters..). – Andrew Thompson Mar 02 '21 at 12:39
  • 1
    *It is the most logical and intuitive layout.* - no it isn't. Each Swing component is responsible for determining its own preferred size. Using a null layout implies a random guess as to what the size should be. A JScrollPane will only work when its child panel uses layout managers. – camickr Mar 02 '21 at 15:27

0 Answers0