Java – JPanel in jtabbar is not scrollable

Enter the code here. I have the following questions

I added two jpanels to a panel and finally added it to my tabbedpane However, I want it to scroll, so I just make the panel scrollable, just add my scroll bar in the middle of the bar

Here is my example:

public class Test extends JFrame {

    private static final long serialVersionUID = -4682396888922360841L;    
    private JMenuBar menuBar;    
    private JMenu mAbout;    
    private JMenu mMain;    
    private JTabbedPane tabbedPane;    
    public SettingsTab settings = new SettingsTab();

    private void addMenuBar() {    
        menuBar = new JMenuBar();    
        mMain = new JMenu("Main");
        mAbout = new JMenu("About");    
        menuBar.add(mMain);
        menuBar.add(mAbout);    
        setJMenuBar(menuBar);    
    }

    public void createTabBar() {    
        tabbedPane = new JTabbedPane(JTabbedPane.TOP);    
        JComponent panel2 = new JPanel();
        tabbedPane.addTab("Test1",null,panel2,"Displays the results");    
        tabbedPane.addTab("Settings",settings.createLayout());    
        add(tabbedPane);    
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);    
    }

    private void makeLayout() {    
        setTitle("Test");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(1000,500));    
        addMenuBar();
        createTabBar();
        setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);    
    }    

    public void start() {
        javax.swing.SwingUtilities.invokelater(new Runnable() {
            public void run() {
                makeLayout();   
            }
        });
    }

    public static void main(String[] args) {    
        Test gui = new test();
        gui.start();    
    }

    public class SettingsTab extends JPanel {        
        public JPanel createLayout() {
            JPanel panel = new JPanel();    
            panel.setLayout(new @R_184_2419@Layout(panel,@R_184_2419@Layout.Y_AXIS));    
            panel.add(table1());
            panel.add(@R_184_2419@.createRigidArea(new Dimension(0,10)));
            panel.add(table2());
            panel.add(@R_184_2419@.createRigidArea(new Dimension(0,10)));                
            panel.add(new JScrollBar());    
            return panel;
        }

        public JPanel table1() {
            JPanel panel1 = new JPanel();    
            String[] columnNames = {"First Name","Last Name"};    
            Object[][] data = {{"Kathy","Smith"},{"John","Doe"},{"Sue","Black"},{"Jane","White"},{"Joe","Brown"}
            };    
            final JTable table = new JTable(data,columnNames);    
            panel1.add(table);    
            panel1.setLayout(new @R_184_2419@Layout(panel1,@R_184_2419@Layout.Y_AXIS));    
            return panel1;
        }

        public JPanel table2() {
            JPanel panel1 = new JPanel();    
            String[] columnNames = {"First Name","Last Name"};    
            Object[][] data = { {"Kathy",columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500,70));
            table.setFillsViewportHeight(true);    
            panel1.add(table);    
            panel1.setLayout(new @R_184_2419@Layout(panel1,@R_184_2419@Layout.Y_AXIS));    
            return panel1;
        }
    }
}

What do I suggest? Did I do wrong?

Thank you for your reply!

UPDATE

I tried it with JScrollPane, but it still didn't work:

public class Test extends JFrame {

    private static final long serialVersionUID = -4682396888922360841L;

    private JMenuBar menuBar;

    private JMenu mAbout;

    private JMenu mMain;

    private JTabbedPane tabbedPane;

    public SettingsTab settings = new SettingsTab();

    private void addMenuBar() {

        menuBar = new JMenuBar();

        mMain = new JMenu("Main");
        mAbout = new JMenu("About");

        menuBar.add(mMain);
        menuBar.add(mAbout);

        setJMenuBar(menuBar);

    }

    public void createTabBar() {

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);

        JComponent panel2 = new JPanel();
        tabbedPane.addTab("Test1","Displays the results");

        tabbedPane.addTab("Settings",settings.createLayout());

        add(tabbedPane);

        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

    }

    private void makeLayout() {

        setTitle("Test");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(1000,500));

        addMenuBar();
        createTabBar();
        setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }


    public void start() {
        javax.swing.SwingUtilities.invokelater(new Runnable() {
            public void run() {
                makeLayout();   
            }
        });
    }

    public static void main(String[] args) {

        Test gui = new test();
        gui.start();

    }

    public class SettingsTab extends JScrollPane {


        public JPanel createLayout() {
            JPanel panel = new JPanel();

            panel.setLayout(new @R_184_2419@Layout(panel,@R_184_2419@Layout.Y_AXIS));

            panel.add(table1());
            panel.add(@R_184_2419@.createRigidArea(new Dimension(0,10)));
            panel.add(table3());
            panel.add(@R_184_2419@.createRigidArea(new Dimension(0,10)));

            add(new JScrollPane());

            return panel;
        }

        public JPanel table1() {
            JPanel panel1 = new JPanel();

            String[] columnNames = {"First Name","Last Name"};

            Object[][] data = {
                    {"Kathy","Brown"},"Brown"}
            };

            final JTable table = new JTable(data,columnNames);

            panel1.add(table);

            panel1.setLayout(new @R_184_2419@Layout(panel1,@R_184_2419@Layout.Y_AXIS));

            return panel1;
        }

        public JPanel table2() {
            JPanel panel1 = new JPanel();

            String[] columnNames = {"First Name",70));
            table.setFillsViewportHeight(true);

            panel1.add(table);

            panel1.setLayout(new @R_184_2419@Layout(panel1,@R_184_2419@Layout.Y_AXIS));

            return panel1;
        }

        public JPanel table3() {
            JPanel panel1 = new JPanel();

            String[] columnNames = {"First Name",@R_184_2419@Layout.Y_AXIS));

            return panel1;
        }
    }
}

Solution

If this is what you need

Then you can do something like this:

public JScrollPane createLayout() {

    JPanel panel = new JPanel();
    JScrollPane sp = new JScrollPane(panel);
    sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                    // or ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED

    panel.setLayout(new @R_184_2419@Layout(panel,@R_184_2419@Layout.Y_AXIS));

    panel.add(table1());
    panel.add(@R_184_2419@.createRigidArea(new Dimension(0,10)));
    panel.add(table2());
    panel.add(@R_184_2419@.createRigidArea(new Dimension(0,10)));


    return sp;
}
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
分享
二维码
< <上一篇
下一篇>>