3

enter code hereI am having the following problem.

I am adding a two JPanels to one panel and add this finally to my TabbedPane. However, I want to make it scrollable, therefore I just make the Panel scrollable which just adds my scroller 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 BoxLayout(panel, BoxLayout.Y_AXIS));    
            panel.add(table1());
            panel.add(Box.createRigidArea(new Dimension(0,10)));
            panel.add(table2());
            panel.add(Box.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 BoxLayout(panel1, BoxLayout.Y_AXIS));    
            return panel1;
        }

        public JPanel table2() {
            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);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);    
            panel1.add(table);    
            panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));    
            return panel1;
        }
    }
}

Any recommendations what I am doing wrong?

I appreciate your reply!

UPDATE

I tried it with a JScrollPane but it still does not 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", 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 JScrollPane {


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

            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

            panel.add(table1());
            panel.add(Box.createRigidArea(new Dimension(0,10)));
            panel.add(table2());
            panel.add(Box.createRigidArea(new Dimension(0,10)));
            panel.add(table3());
            panel.add(Box.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", "Smith"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"}
            };

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

            panel1.add(table);

            panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));

            return panel1;
        }

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

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

            Object[][] data = {
                    {"Kathy", "Smith"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"}
            };

            final JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);

            panel1.add(table);

            panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));

            return panel1;
        }

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

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

            Object[][] data = {
                    {"Kathy", "Smith"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"},
                    {"John", "Doe"},
                    {"Sue", "Black"},
                    {"Jane", "White"},
                    {"Joe", "Brown"}
            };

            final JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);

            panel1.add(table);

            panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));

            return panel1;
        }
    }
}
Carol.Kar
  • 4,581
  • 36
  • 131
  • 264

3 Answers3

3

You must add a JScrollPane on it! :)

I dont really know what you did there, but basically it goes like that:

JPanel myPanel = new JPanel(); // the main content is on the myPanel
myPanel.add(myContent);// add everything you want

new JFrame().add(new JScrollPane(myPanel));
romaneso
  • 1,097
  • 1
  • 10
  • 26
3

Add JTable inside JScrollPane and then add JScrollPane in the JPanel.

JScrollPane pane = new JScrollPane(table);
panel1.add(pane);

there is no need of below line:

panel.add(new JScrollBar());

See Swing Tutorial on How to Use Tables where you can find examples as well.


You can set the size of JPanel if needed (not always required) and then add it inside the JScrollPane

JPanel panel = new JPanel(){
    @Override
    public Dimension getSize(){
        return new Dimension(...,....);
    }
};

EDIT

You are not added it inside the JScrollPane. It should be

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

and remove below line:

add(new JScrollPane());

enter image description here

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Thx a lot for your answer! However, this only makes the JTable scrollable. Is it also possible to make the JPanel scrollable where the tables are inside? – Carol.Kar Jul 23 '14 at 08:04
  • 1
    why you need JPanel scrollable? what you want to scroll? – Braj Jul 23 '14 at 08:05
  • Thx for your asnwer! I only want to scroll the JPanel in the Tab. Therefore, there should be a scrollable element on the site of the JPanel. – Carol.Kar Jul 23 '14 at 08:07
  • I do not get your update. What do you mean by `then add it inside the JScrollPane`? – Carol.Kar Jul 23 '14 at 08:13
  • yes you can simply add JPanel inside the JScrollPane same as did for JTable. – Braj Jul 23 '14 at 08:15
  • Thx! I tried it like that, but it still does not work. Pls have a look at my update! – Carol.Kar Jul 23 '14 at 08:20
1

Thx a lot for your answer! However, this only makes the JTable scrollable. Is it also possible to make the JPanel scrollable where the tables are inside?

If this is what you need

enter image description here

then you could 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 BoxLayout(panel, BoxLayout.Y_AXIS));

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


    return sp;
}
A4L
  • 17,353
  • 6
  • 49
  • 70
  • @Vivien cool you're welcome! you might also want to consider Braj's last edit if you dont want to change the signature of `createlayout`. – A4L Jul 23 '14 at 08:27