简体   繁体   中英

Java Swing GridBagLayout adding components inbetween other components

What i want to achieve : I want to be able to add components to a scrollpane inbetween components already added. In other words, i somehow want to move the index up and plop some new components where the old ones used to be. I created a short program to illustrate my problem, it replace the components it doesnt add them. can someone help?

Why i want to achieve it (to answer the inevitable question related to this, this is long only read if interested): Ive got a scrollpane in which i need to display lots of components, approx 150000 JTables. This takes too much memory and processing time. So what ive done is to load the scrollpane with 150000 TextFields and only displaying 100 of the tables at a time. If you scroll to textfield 200 it loads the next 100 tables, replacing the previously created text fields with the tables. If you move to textfield 300 it loads the next 100 tables, and removes the first 100, again replacing it with textfields. My thinking here is that the textfields are going to be much less resource intensive than the tables, and so far this thinking seems to be correct. So this all seems to work pretty well (on my smaller test file - about 5000 records). The problem is that to create the 150000 textfields also takes lots of time. I thought of a solution for this. To not create a textfield for every table, but to rather create 1 textfield for every 100 tables. The idea is that when you scroll to textfield 2 (instead of 200), it loads the next 100 tables, replacing textfield nr 2 with tables nr 200 - 299. So somehow i need to move textfield 3 and all those below it down in order to insert all the tables. Phew, hope this makes sense

So looking at the example below what i effectively want to achieve is to replace say textfield 20 with 5 new textfields, but not repalce textfields 21 - 25.

    import javax.swing.*;
    import java.awt.*;

    public class Example {
    public static void main(String args[]) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ExampleScreen ex = null;
                    ex = new ExampleScreen();
                    ex.setVisible(true);
            }
        });
    }

    private static class ExampleScreen extends JFrame{

        GridBagConstraints gc = new GridBagConstraints();
        JPanel panel = new JPanel(new GridBagLayout());
        JScrollPane scrollPane = new JScrollPane(panel);

        public ExampleScreen() {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.add(scrollPane, BorderLayout.CENTER);
            setupFields();
            createExtraFieldsInbetween(20);
            this.pack();
            scrollPane.getVerticalScrollBar().setUnitIncrement(16);
            scrollPane.getViewport().setScrollMode(JViewport.BLIT_SCROLL_MODE);
        }    

        //after this happens i need to add components somewhere in the middle, like after textfield 20 for example
        private void setupFields() {    
            gc.gridy = 0;
            gc.gridx = 0;
            for(int k = 0; k < 50; k++) {
                JTextField textField = new JTextField();
                textField.setPreferredSize(new Dimension(1500,36));
                textField.setText(new Integer(k).toString());
                panel.add(textField, gc, k);
                gc.gridy++;
            }
        }

        //this is to create extra fields inbetween those already created
        //this does not work, it overwrites the existing components, doesnt move them up
        private void createExtraFieldsInbetween(int i) {    
            gc.gridy = i;    
            //create 5 extra
            for(int k = i; k < i + 5; k++) {
                JTextField textField = new JTextField();
                textField.setPreferredSize(new Dimension(1500,36));
                textField.setText("Extra Field : " + new Integer(k).toString());
                panel.add(textField, gc, k);
                gc.gridy++;
            }
        }
    }
}

Anytime I see a number like 150,000 I think the user can't be bothered to scroll through all those components so you need a better UI.

Anyway if you have a problem with a GridBagLayout, then don't use a GridBagLayout.

It looks to me like you are just displaying the components vertically so try a BoxLayout . Then you can just use the add(...) method to specify the index where you want to remove/add components.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM