How to set size of Jtable in tab of JtabbedPane and how to set the layout of tabs in JTabbedPane?

StackOverflow https://stackoverflow.com/questions/22974749

Вопрос

I'm trying to make my first swing application and there's a problem which I can not figure it out. I want the JTabbedPane and the JTable in it to be with the full width of the Jpanel in which they are placed. Also I want to make my tabs to be just in one row. Is there any method which is used to set the layout of the tabs? You can see the picture below.

enter image description here

That's the code which I'm using for the panel with the table:

public class GradesInfo extends JPanel {

    public GradesInfo () {
        Dimension size = getPreferredSize();
        size.width = 650;
        setPreferredSize(size);

        setBorder(BorderFactory.createTitledBorder("Grades info"));

        // creating components
        JTabbedPane tabbedPane = new JTabbedPane();

        GradesTableModel tableModel = new GradesTableModel(new String[][] {{"Stephen", "6"}, {"Michael", "5"}});

        JTable table1 = new JTable();
        table1.setModel(tableModel);
        JTable table2 = new JTable();
        table2.setModel(tableModel);
        JTable table3 = new JTable();
        table3.setModel(tableModel);
        JTable table4 = new JTable();
        table4.setModel(tableModel);
        JTable table5 = new JTable();
        table5.setModel(tableModel);
        JTable table6 = new JTable();
        table6.setModel(tableModel);
        JTable table7 = new JTable();
        table7.setModel(tableModel);
        JTable table8 = new JTable();
        table8.setModel(tableModel);

        JScrollPane scr1 = new JScrollPane(table1);
        JScrollPane scr2 = new JScrollPane(table2);
        JScrollPane scr3 = new JScrollPane(table3);
        JScrollPane scr4 = new JScrollPane(table4);
        JScrollPane scr5 = new JScrollPane(table5);
        JScrollPane scr6 = new JScrollPane(table6);
        JScrollPane scr7 = new JScrollPane(table7);
        JScrollPane scr8 = new JScrollPane(table8);


        // adding the components
        tabbedPane.addTab("Semester 1", scr1);
        tabbedPane.addTab("Semester 2", scr2);
        tabbedPane.addTab("Semester 3", scr3);
        tabbedPane.addTab("Semester 4", scr4);
        tabbedPane.addTab("Semester 5", scr5);
        tabbedPane.addTab("Semester 6", scr6);
        tabbedPane.addTab("Semester 7", scr7);
        tabbedPane.addTab("Semester 8", scr8);

        add(tabbedPane);


    }

}
Это было полезно?

Решение

The problem isn't with the JTabbedPane or the JScrollPane or the JTable, the problem is with your GradesInfo JPanel.

JPanel uses a FlowLayout by default, which honours the preferredSize of the components added to it.

Instead, set the layout of the GradesInfo panel to BorderLayout

To change the way that the tabs are laid out, see JTabbedPane#setTabLayoutPolicy

You'll probably want yo use JTabbedPane.SCROLL_TAB_LAYOUT

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top