scrollable panel in swing

心不动则不痛 提交于 2019-12-11 23:00:03

问题


I have a problem when adding a scroll pane to my app. My app works like this: 1. At start panel1(yellow one) is initialized 2. After I press "potvrdit" panel2(red one is initialized) 3. Set of panels(green one) are added to panel2

The problem is that I cant add a scrollbar when the number of set(green panels) is too big for screen. It just added thin line on the right side as you can see.

This is part of my source code:

JFrame jframe = new JFrame("Etiket print.");
JPanel panel0 = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JScrollPane scrollFrame = new JScrollPane(panel2);
jframe.setMinimumSize(new Dimension(1280, 1000));
GridBagConstraints c = new GridBagConstraints();        
panel0.setLayout(new GridBagLayout());
panel1.setLayout(new GridBagLayout());
panel2.setLayout(new GridBagLayout());
panel2.setAutoscrolls(true);
panel3.setLayout(new GridBagLayout());
panel4.setLayout(new GridBagLayout());
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JPanel container1 = new JPanel();
JPanel container2 = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));
container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));
container1.add(panel0);
container1.add(panel1);
container2.add(container1);
container2.add(panel3);
container2.add(panel4);
container.add(container2);
container.add(panel2);
jframe.add(scrollFrame);
jframe.add(container, BorderLayout.NORTH);
jframe.pack();
jframe.setVisible(true);
if (e.getActionCommand().equals("Potvrd")||e.getActionCommand().equals("myTextField")){

                temporaryBlockedSubharnesses.clear();
                panel2.removeAll();

                harnessPanelList.clear();


            GridBagConstraints c = new GridBagConstraints();

            for (int i=0; i<20; i++){
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0.5;
            c.gridx = 0;
            c.gridy = i;
            JPanel hp= new JPanel();
                hp.setMinimumSize(new Dimension(80, 70));
                hp.setMaximumSize(new Dimension(80, 70));
                hp.setPreferredSize(new Dimension(80, 70));
                if(i%2==0) {
                hp.setBackground(myBlue1Color);
            }
            else {
                    hp.setBackground(myBlue2Color);
                 }


            panel2.add(hp, c);

               hp.repaint();
              hp.validate();
            }
            panel2.repaint();
            panel2.validate();
            jframe.repaint();

        }

回答1:


Instead of JPanel try JXPanel from swingx library. It implements Scrollable so it support scrolling.



来源:https://stackoverflow.com/questions/18381857/scrollable-panel-in-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!