Reputation: 15251
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));
Upvotes: 2
Views: 15793
Reputation: 205865
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);
Upvotes: 1
Reputation: 54924
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.
Upvotes: 1