tags:

views:

36

answers:

2

I have a createUI() function which loads JPanel into NORTH, SOUTH, EAST.

private void createUI() {
    add(createToolBar(), BorderLayout.NORTH);
    add(createDataView(), BorderLayout.SOUTH);
    add(createHistoryView(), BorderLayout.EAST);

    createMenuBar();

    setTitle("My Application");

}

Each components display in the correct location, however, they are all in fixed sized JPanel. I would like to know if it's possible for the JPanel to completely fill the BorderLayout region.

For example, the createDataView() is a simple Jpanel with tables. I would like it to expand horizontally, and have a fixed height.

private JPanel createDataView(){
        JPanel panel = new JPanel();

        String data[][] = {{"001","vinod","Bihar","India","Biology","65","First"},
                           {"002","Raju","ABC","Kanada","Geography","58","second"},
                           {"003","Aman","Delhi","India","computer","98","Dictontion"},
                           {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"},
                           {"004","Ranjan","Bangloor","India","chemestry","90","Dictontion"}};
        String col[] = {"Roll","Name","State","country","Math","Marks","Grade"};
        JTable table = new JTable(data,col);
        table.setPreferredScrollableViewportSize(new Dimension(900,100));
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.yellow);
        JScrollPane pane = new JScrollPane(table);
        table.setAutoResizeMode(JTable.WIDTH);
        table.doLayout();
        panel.add(pane);
        return panel;
}                      

Right now I am relying on manually setting the dimension. However, I would a way to get the current window width, and set it as below.

table.setPreferredScrollableViewportSize(new Dimension(900,100));
+1  A: 

The default layout of a new JPanel is FlowLayout; a GridLayout may be an alternative for your data view. You can compare the possibilities in A Visual Guide to Layout Managers.

JPanel panel = new JPanel(new GridLayout(1, 0));

Addendum: Your setAutoResizeMode() looks suspicious; JTable.WIDTH is unlikely to be only coincidentally a valid value. GridLayout works well with the default, JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS, but you might experiment with other settings.

table.setAutoResizeMode(JTable.WIDTH);
trashgod
`WIDTH` is inherited from `ImageObserver` (implemented all the way up by `Component`), which happens to be the same as `JTable#AUTO_RESIZE_NEXT_COLUMN`.
Geoffrey Zheng
@Geoffrey Zheng: Both 1! I should have looked. Thanks for clarifying this.
trashgod
+1  A: 

With the borderlayout, the NORTH, SOUTH, EAST and WEST borders all take on the default size of the containing panel, but the CENTER expands to the take up all remaining space. I would suggest putting your createDataView in this section of the layout.

Codemwnci
do you meant by putting a JPanel container in CENTER, and placing the component in here ?
Kim Jong Woo
yeah, change add(createDataView(), BorderLayout.SOUTH); to add(createDataView(), BorderLayout.CENTER);
Codemwnci
this seems to completely overlap the existing component in CENTER already. anyway to make it side by side ? What about if I need something in SOUTH, CENTER ?
Kim Jong Woo
oh, sorry you did not mention you already had components in the CENTER. In which case, you probably want to look at minSize, preferredSize and maxSize methods on the JTable.
Codemwnci