开发者

How do you position objects in JPanel with BorderLayout?

I have the following class which implements 3 JPanels. 1 Panel has a label, next are the buttons and the third is a table as described in my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import ja开发者_StackOverflowva.util.*;
import javax.swing.event.*;

class  netTable implements ActionListener, TableModelListener
{
JFrame frame;
JTable table;
Vector rows,columns;
DefaultTableModel tabModel;
JScrollPane scrollPane;
JLabel lblMessage;
JButton cmdLookup, cmdUpdatePlan;
JPanel topPanel,mainPanel,buttonPanel;

public static void main(String[] args) 
    {
    netTable t=new netTable();
    }

netTable()
    {
    rows=new Vector();
    columns= new Vector();
    String[] columnNames = 
    { 
        "ID", 
        "Client",
        "Plan",
        "Amount"
    };

addColumns(columnNames);

tabModel=new DefaultTableModel();
tabModel.setDataVector(rows,columns);

table = new JTable(tabModel);
scrollPane= new JScrollPane(table);//ScrollPane

table.setRowSelectionAllowed(false);

table.getModel().addTableModelListener(this);

topPanel = new JPanel(); 
lblMessage=new JLabel("Invoices to Update"); 
topPanel.add(lblMessage); 

buttonPanel=new JPanel();

cmdLookup=new JButton("Lookup"); 
cmdUpdatePlan = new JButton("Update Plan");

buttonPanel.add(cmdLookup);
buttonPanel.add(cmdUpdatePlan);

cmdLookup.addActionListener(this);
cmdUpdatePlan.addActionListener(this);

mainPanel=new JPanel();
frame=new JFrame("Update Table");
frame.setSize(500,500);
frame.setExtendedState(JFrame.ICONIFIED);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(topPanel,BorderLayout.NORTH);
mainPanel.add(buttonPanel,BorderLayout.CENTER);
mainPanel.add(scrollPane,BorderLayout.SOUTH);

topPanel.setBackground(Color.gray);
mainPanel.setBackground(Color.white);
buttonPanel.setBackground(Color.white);
table.getParent().setBackground(Color.black);
frame.getContentPane().add(mainPanel);
frame.addWindowListener(new WindowCloser());
frame.setVisible(true);
}

}

When I compile this, it displays the buttonPanel on top, a space, and then the scrollPane below it, leaving out the label where topPanel is supposed to display first at the top. Any ideas? I'm thinking the BorderLayout position is wrong.


There isn't room for those panels so the top panel is squished into about 3 pixels. Try

frame.setSize(800,800);

instead.


The problem seems to be this line:

frame.setExtendedState(JFrame.ICONIFIED);

For some reason, if this is run before the components are added to your frame, topPanel will disappear.

You can do one of these:

  1. Chuck your entire constructor in a try block, with the extended state being set in finally.

  2. (recommended) Set the extended state after everything has been constructed (using another method or directly in main). You should also move things like frame.setVisible(true); to such a method as well.


Center panel will eat as much of the available space as possible. If you want to have a scroll pane "maximized" with buttons between the top label and the scroll pane, create another JPanel with border layout, add buttons to the north and scroll pane to the center, and add the whole panel to the center of your mainPanel

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜