JTable and Resizing

A

Abraham Khalil

When I resize, can't seem to get the JTable to be align North near the
close button. When I resize more the JTable moves closer to the bottom
of the frame, instead of going towards the close button at the top.
Attach is the code sample I'am trying to figure out. Hope someone can
figure why JTable is behaving like this:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestLayoutFrame extends JFrame implements ActionListener
{
private static final int VGAP = 4;

private JButton stopButton = null;
private JButton closeButton = null;


public TestLayoutFrame() {
getContentPane().setLayout(new BorderLayout());

JTabbedPane tabbedPane = new JTabbedPane();
JTable table = new JTable(new Object[][] {{"1", "2", "3"}, {"4",
"5", "6"}, {"7", "8", "9"}},
new Object[] {"a", "b", "c"});

JTextArea textArea = new JTextArea();
textArea.setRows(10);

JPanel tabPanelOne = new JPanel();
tabPanelOne.setLayout(new BorderLayout());
tabPanelOne.add(new JScrollPane(table));

JPanel tabPanelTwo = new JPanel();
tabPanelTwo.setLayout(new BorderLayout());
tabPanelTwo.add(textArea, BorderLayout.NORTH);

tabbedPane.add(tabPanelOne, "Tasks");
tabbedPane.addTab("Text area...", tabPanelTwo);

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());

JLabel statusLabel = new JLabel("Labe text sitting in top left
of the screen");

stopButton = new JButton("Stop");
stopButton.setMnemonic('S');
stopButton.addActionListener(this);

closeButton = new JButton("Close");
closeButton.setMnemonic('C');
closeButton.addActionListener(this);

JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(2, 1, 0, VGAP));

buttonsPanel.add(stopButton);
buttonsPanel.add(closeButton);

constrain( mainPanel, statusLabel,
GridBagConstraints.HORIZONTAL,
GridBagConstraints.NORTH,
0, 1, 1, 1, 1.0, 1.0, new Insets(5, 5, 0, 0));

constrain( mainPanel, buttonsPanel,
GridBagConstraints.NONE, GridBagConstraints.NORTH,
1, 1, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, new
Insets(5, 10, 0, 3));

constrain( mainPanel, tabbedPane,
GridBagConstraints.BOTH, GridBagConstraints.CENTER,
0, 2, GridBagConstraints.REMAINDER, 1, 1.0, 1.0);

addContentSpace(mainPanel, 3);

getContentPane().add(mainPanel, BorderLayout.CENTER);
}



public static void constrain( Container container, Component
component,
int fill, int anchor,
int gx, int gy, int gw, int gh,
double wx, double wy ) {
GridBagConstraints c = new GridBagConstraints();

c.fill = fill;
c.anchor = anchor;
c.gridx = gx;
c.gridy = gy;
c.gridwidth = gw;
c.gridheight = gh;
c.weightx = wx;
c.weighty = wy;

( (GridBagLayout) container.getLayout() ).setConstraints(
component, c );

container.add( component );
}



public static void constrain( Container container, Component
component,
int fill, int anchor,
int gx, int gy, int gw, int gh,
double wx, double wy, Insets inset )
{
GridBagConstraints c = new GridBagConstraints();

c.fill = fill;
c.anchor = anchor;
c.gridx = gx;
c.gridy = gy;
c.gridwidth = gw;
c.gridheight = gh;
c.weightx = wx;
c.weighty = wy;
c.insets = inset;

( (GridBagLayout) container.getLayout() ).setConstraints(
component, c );

container.add( component );
}



private void addContentSpace(Container container, int row) {
Component boxSpace = Box.createVerticalStrut(VGAP);

constrain( container, boxSpace,
GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,
0, row, GridBagConstraints.REMAINDER, 1, 1.0, 0.0);
}



public void actionPerformed(ActionEvent e) {
Component c = (Component) e.getSource();

if (c instanceof JButton) {
JButton button = (JButton) c;

if (button.equals(stopButton)) {
}


if (button.equals(closeButton)) {
quit();
}
}
}


private void quit() {
System.exit(0);
}



public static void main(String args[]) {
TestLayoutFrame testLayoutFrame = new TestLayoutFrame();
testLayoutFrame.pack();
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();

int locX = (screenSize.width - testLayoutFrame.getSize().width)
/ 2;
int locY = (screenSize.height -
testLayoutFrame.getSize().height) / 2;

testLayoutFrame.setLocation(locX, locY);

testLayoutFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

testLayoutFrame.setVisible(true);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top