Java – JScrollPane ‘lazy’ scrolling, containing many components

I have a 3045 form! Components (1015 labels, 1015 text fields, 1015 combo boxes) All of these are in JPanel and JPanel in JScrollPane The problem is that rolling is "lagging" I have 4GB ram on my PC, so I don't think that's the problem What's up?

My code:

final JScrollPane scrollPane_1 = new JScrollPane();
final JPanel panel = new JPanel();
panel.setLayout(null);
scrollPane_1.setViewportView(panel);
int y =0;
for(int i=0; i<1015;i++)
    {

            JLabel length = new JLabel();
            length.setBounds(10,y,350,20);
            length.setFont(new Font("Tahoma",Font.BOLD,11));
            length.setEnabled(false);
            panel.add(length);
            panel.revalidate();

            JCombo@R_160_2419@ combo = new JCombo@R_160_2419@();
            combo.setModel(new DefaultCombo@R_160_2419@Model(new String[] {"=","!="}));
            combo.setBounds(10,y + 20,70,20);
            panel.add(combo);
            panel.revalidate();

            JTextField text = new JTextField();
            text.setBounds(10 + 80,200,20);
            panel.add(text);
            panel.revalidate();
}

Editor: I've done a lot of tests. I realize that there is a delay only when I use combo boxes. If I use text fields instead of combo boxes, scrolling is normal

Solution

Your code doesn't seem so dull to me when using layout manager Please test:

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.*;

public class Foo2 {
   protected static final int PREF_W = 400;
   protected static final int PREF_H = 400;

   public static void main(String[] args) {
      final JScrollPane scrollPane_1 = new JScrollPane() {
         @Override
         public Dimension getPreferredSize() {
            return new Dimension(PREF_W,PREF_H);
         }
      };
      final JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(0,1));
      scrollPane_1.setViewportView(panel);
      for (int i = 0; i < 1015; i++) {
         JPanel innerPanel = new JPanel();
         innerPanel.setLayout(new @R_160_2419@Layout(innerPanel,@R_160_2419@Layout.LINE_AXIS));

         JLabel length = new JLabel("foo");
         length.setFont(new Font("Tahoma",11));
         length.setEnabled(false);
         innerPanel.add(length);
         innerPanel.add(@R_160_2419@.createHorizontalGlue());

         JCombo@R_160_2419@<String> combo = new JCombo@R_160_2419@<String>();
         combo.setPrototypeDisplayValue("              ");
         combo.setModel(new DefaultCombo@R_160_2419@Model<String>(new String[] { "=","!=" }));
         combo.setMaximumSize(combo.getPreferredSize());
         innerPanel.add(combo);

         JTextField text = new JTextField(10);
         JPanel textwrapper = new JPanel();
         textwrapper.add(text);
         innerPanel.add(textwrapper);

         panel.add(innerPanel);
      }
      JFrame frame = new JFrame();
      frame.add(scrollPane_1);
      frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}

edit

Instead, how about using JTable?

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class Foo3 {
   protected static final int PREF_W = 400;
   protected static final int PREF_H = 400;

   public static void main(String[] args) {
      final JScrollPane scrollPane_1 = new JScrollPane() {
         @Override
         public Dimension getPreferredSize() {
            return new Dimension(PREF_W,PREF_H);
         }
      };
      String[] columnNames = {"Foo","Bar","Baz"};
      String[][] data = new String[1015][3];
      for (int i = 0; i < data.length; i++) {
         data[i] = new String[]{"foo","==",""};
      }
      DefaultTableModel model = new DefaultTableModel(data,columnNames){
         @Override
         public boolean isCellEditable(int row,int column) {
            return (column != 0);
         }
      };
      JTable table = new JTable(model);
      table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(
            new JCombo@R_160_2419@<String>(new String[]{"==","!="})));
      final JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(0,1));
      scrollPane_1.setViewportView(table);

      JFrame frame = new JFrame();
      frame.add(scrollPane_1);
      frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>