Rejeev Divakaran
Rejeev Divakaran

Reputation: 4504

Swing Layout issue - JTable and JTree

I am having trouble in using the layout in swing properly.

I am trying to achieve something like screenshot1 however I am getting something like screenshot2.

Please see my source code below.

What am I dong wrong? How can I achieve what I want (as in screenshot1).

Note: When I run the code multiple times, sometimes I get the alignment between table an tree getting correct. looks like a race condition here.

Screenshot1

Screenshot1

Screenshot2
Screenshot2

Source code:

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.util.Date;

public class SwingLayoutTest {
    private static final int screenWidth;
    private static final int screenHeight;

    static {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        screenWidth = screenSize.width;
        screenHeight = screenSize.height;
    }

    public static void main(String[] args) throws Exception {
        JFrame mainWindow = new JFrame();
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setSize(800, 600);
        mainWindow.setExtendedState(JFrame.MAXIMIZED_BOTH);
        Container contentPane = mainWindow.getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
        JTree tree = createTree();
        tree.setPreferredSize(new Dimension(screenWidth/2, screenHeight));
        JScrollPane scrollTree = new JScrollPane(tree);
        JTable table = createTable();
        table.setPreferredSize(new Dimension(screenWidth/2, screenHeight));
        table.getColumnModel().getColumn(1).setWidth(100);
        table.getColumnModel().getColumn(2).setWidth(100);
        JScrollPane scrollTable = new JScrollPane(table);
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollTree, scrollTable);
        contentPane.add(splitPane);
        mainWindow.setVisible(true);

    }

    private static JTree createTree(){
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("This is root Node");
        DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
        rootNode.add(child1);
        child1.add(new DefaultMutableTreeNode("Child 1 1"));
        child1.add(new DefaultMutableTreeNode("Child 1 2"));
        DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
        rootNode.add(child2);
        child2.add(new DefaultMutableTreeNode("Child 2 1"));
        child2.add(new DefaultMutableTreeNode("Child 2 2"));
        return new JTree(rootNode);
    }

    private static JTable createTable(){
        String[] columnNames = {"Name", "Size", "Modified Time"};
        Object[][] data = {{"This is row 1","1000", new Date()},
                {"This is row 2", "5455", new Date()}};
        return new JTable(data, columnNames);
    }
}

Upvotes: 1

Views: 576

Answers (1)

wolfcastle
wolfcastle

Reputation: 5930

From the Swing Tutorial:

To make your split pane work well, you often need to set the minimum sizes of components in the split pane, as well as the preferred size of either the split pane or its contained components. Choosing which sizes you should set is an art that requires understanding how a split pane's preferred size and divider location are determined.

Why are you setting the size of the window to something small, then setting the size of the components to be the size of the screen? If you want that much to be visible, why not also set the size of your window to be the size of the screen?

Try calling setDividerLocation and/or setResizeWeight and messing with the minimum sizes of the table/tree components. There is no single correct answer here.

Upvotes: 4

Related Questions