1

I want to resize JTable inside JTabbedPane, but i have no clue how to do that. currently my JTab only display JTable on default size.

enter image description here

currently my code is like this

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBounds(0, 0, 434, 262);
    frame.getContentPane().add(tabbedPane);

    JPanel panel = new JPanel();
    panel.setLayout(null);

    Object rowData[][] = {
            { "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
            { "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
            { "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
            { "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
            { "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
            { "Row2-Column1", "Row2-Column2", "Row2-Column3", "", "", "",
                    "", "" } };
    Object columnNames[] = { "Column One", "Column Two", "Column Three",
            "", "", "", "" };
    JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    tabbedPane.addTab("table", null, scrollPane, null);

here is my full code = My Code

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ridzuan Adris
  • 1,192
  • 2
  • 13
  • 32

2 Answers2

3

The fixed size is because the default layout of JPanel is FlowLayout, which uses the table's preferred size. Instead, use something like GridLayout, which fills the space available.

JPanel panel = new JPanel(new GridLayout());
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • 1
    Alos consider overridding [`getPreferredScrollableViewportSize()`](http://stackoverflow.com/a/14429388/230513). – trashgod Sep 26 '14 at 16:04
2

This code can help you to set row height of your table .

table.setRowHeight(anInteger);

In addition you know that with this code you can have your pleasant height of your jtabel:

int scrollPaneHeight = scrollPane.getHeight();

So you want every row has this height :

int spaceForRows = scrollPaneHeight -heightOfheaderOfTabel;

float rowH =(float) spaceForRows / (rowData.length );  

But as you know when you re-size your jframe and consequently your scrollPane and your table can not be full screen twice. So you most use a componentListener . So when your component is re-sizing your code calculate rowH twice. complete code is

public static void main(String[] args) {
    // TODO Auto-generated method stub
    final JFrame frame = new JFrame();
    final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    frame.setContentPane(tabbedPane);
    final Object rowData[][] = {
            { "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
            { "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
            { "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
            { "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
            { "Row1-Column1", "Row1-Column2", "", "", "", "", "" },
            { "Row2-Column1", "Row2-Column2", "Row2-Column3", "", "", "",
                    "", "" } };
    Object columnNames[] = { "Column One", "Column Two", "Column Three",
            "", "", "", "" };
    final JTable table = new JTable(rowData, columnNames);
    final JScrollPane scrollPane = new JScrollPane(table);

    tabbedPane.addTab("table", null, scrollPane, null);
    frame.setSize(new Dimension(500, 500));

    tabbedPane.addComponentListener(new ComponentListener() {

        @Override
        public void componentShown(ComponentEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void componentResized(ComponentEvent arg0) {
            // TODO Auto-generated method stub

            int scrollPaneHeight = scrollPane.getHeight();
            JTableHeader header = table.getTableHeader();
            int heightOfheaderOfTabel = header.getHeight();
            int spaceForRows = scrollPaneHeight - heightOfheaderOfTabel;
            float rowH = (float) spaceForRows / (rowData.length);
            table.setRowHeight((int) rowH);

        }

        @Override
        public void componentMoved(ComponentEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void componentHidden(ComponentEvent arg0) {
            // TODO Auto-generated method stub

        }
    });

    frame.setVisible(true);

}
Shomeis
  • 99
  • 5
  • 1
    It might be easier to override [`getPreferredScrollableViewportSize()`](http://stackoverflow.com/a/14429388/230513). – trashgod Sep 26 '14 at 16:03