Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Resizing JTable when window has been resized.

843805Apr 25 2006 — edited Apr 25 2006
Right now I have a rather long program so I won't post the whole code. What I'm trying to accomplish is to have a JFrame which will have a bunch of tabbed panes. In the one tab pane there is a JTable. I what it to be a certain size in relation to the window. When it first loads up everything is the right size but if I resize the window the JTable stays the same size. I've tried revalidate, repaint, invalidate/validate. Below is some pieces of code that I have that pretains to this questions.

This is the code that renses the resize of the window
addComponentListener(new ComponentAdapter()
		{
		  	public void componentResized(ComponentEvent e)
		  	{
			  	Width=getWidth();
			  	Height=getHeight();
			  	revalidate();
		  	}
		} );
This is the code that monitors the tabs to see if the tabs were selected. I need this to startup threads for dynamic elements
		JTabbedPane TabList = new JTabbedPane();
		
		ImageIcon icon = new ImageIcon("images/Md.gif");
		
		TabList.addChangeListener(new ChangeListener() 
		{
      		public void stateChanged(ChangeEvent changeEvent) 
      		{
	        	JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
	        	int index = sourceTabbedPane.getSelectedIndex();
	        	//System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));
	        	TabListener(index);
        	}
      	});
This is some code that pretains to the tab panel that has the JTable
PortPanel = new JPanel();
		
		PortLayout = new GridBagLayout();
		PortPanel.setLayout(PortLayout);
		
		Portconstraints = new GridBagConstraints();
		
		JTable Porttable = new JTable(portcellData, portcolumnNames);

		// Disables auto resizing to make the table horizontal scrollable
		Porttable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		Porttable.setPreferredScrollableViewportSize(new Dimension(Width-40,Height/3));

		TableColumn col[]=new TableColumn[12];
		int tablewidth[] = {150,50,50,70,60,70,80,70,80,60,60,60};

		for(int cnt=0;cnt<12;cnt++)
		{
			col[cnt]=Porttable.getColumnModel().getColumn(cnt);
			col[cnt].setMinWidth(tablewidth[cnt]);
			col[cnt].setMaxWidth(tablewidth[cnt]);
			col[cnt].setPreferredWidth(tablewidth[cnt]);
		}

		// Makes the table vertically scrollable
		JScrollPane scrollPane = new JScrollPane(Porttable);
		
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
All of this is running in one funtion. And is setup like below
public class DisplayTable extends JFrame implements ActionListener
{
       public DisplayTable()
       {
//All my code for the display
        }

         public static void main (String args[])
	{
		JFrame application = new DisplayTable();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 23 2006
Added on Apr 25 2006
2 comments
1,002 views