2 Replies - 4007 Views - Last Post: 18 November 2009 - 05:19 PM Rate Topic: -----

#1 crochunter  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 29-May 09

Layouts and JTable Refresh

Post icon  Posted 18 November 2009 - 03:55 AM

I have got a problem with the layouts. I am creating a Grouplayout for my JLabels / Buttons etc in a window .. Then I am adding a Jtable using the JFileChooser for which I am specifying GridBagLayout and then adding the layout to Jframe again after JFilechooser creates a JTable calling the Table creation function. Also one more problem is that for once the JfileChooser creates the JTable fine but when the I choose another file with file chooser the First JTable doesn't refresh and the second JTable just distorts the format. How do I refresh the JTable after I select the other file ?

All formats work fine the way I am doing now but when I click to maximize the window the whole JTable Layout is gone or lost somewhere ! Sometimes something mistereous like JTable appears when I click on the window.

I have a separate GroupLayout for the rest of the JButtons / JLabels / Jcomboboxes... in my window which I add to the Jframe (fr) here. I create a GroupLayout like below for the rest of stuff like this:-
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(panel); // rest of the stuff in my window except Jtable.

I just keep a space in my window to accomodate JTable and then set the layout using GridBagLayout in my JFileChooser code.

Then I add separate GridBagLayout for my JTable.

Here is my JFileChooser code where I am creating a layout for my JTable.

	jButton.addActionListener(
		 new ActionListener() {
		   public void actionPerformed(ActionEvent e) {
			   JFileChooser fileChooser = new JFileChooser(".");
			   int status = fileChooser.showOpenDialog(fr);
			 if (status == JFileChooser.APPROVE_OPTION) {
				File file = fileChooser.getSelectedFile();
				constructTable(file);
				//JTable table = new JTable();
				scroll = new JScrollPane(getTable());
				//setScrollPane(scroll);

			   fr.setLayout(new GridBagLayout());
				GridBagConstraints c = new GridBagConstraints();
				boolean shouldFill = true;
				if (shouldFill) {
		//natural height, maximum width
		c.fill = GridBagConstraints.HORIZONTAL;
				}
				c.fill = GridBagConstraints.HORIZONTAL;
				c.ipady = -305;	  //make this component tall
				c.weightx = 0.0;
				c.gridwidth = 0;
				c.gridx = 0;
				c.gridy = 0;
				fr.add(scroll, c);

				//fr.add(scroll);
				//fr.setSize(400,200);
				fr.setVisible(true);
				fr.pack();
				//SwingUtilities.updateComponentTreeUI(scroll);
			 } else if (status == JFileChooser.CANCEL_OPTION) {
			   System.out.println("canceled");
			 }
		   }
		}
	   );


I know the problem is happening because I am mixing the GridBaglayout with the GroupLayout. The problem is that I am not able to figure out I can create a layout for my JTable which I create using JFile Chooser ?

Is there any way I can deal with this problem so that my layouts work even if I minimize or maximize my windows

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Layouts and JTable Refresh

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3489
  • View blog
  • Posts: 15,769
  • Joined: 20-September 08

Re: Layouts and JTable Refresh

Posted 18 November 2009 - 10:28 AM

You need to be as undestructive as possible to your gui. If you're filling and refilling a JTable, dont tear down the gui or create a new JTable, simply empty and refill the TableModel, trying to keep dimensions intact
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Layouts and JTable Refresh

Posted 18 November 2009 - 05:19 PM

View Postg00se, on 18 Nov, 2009 - 09:28 AM, said:

You need to be as undestructive as possible to your gui. If you're filling and refilling a JTable, dont tear down the gui or create a new JTable, simply empty and refill the TableModel, trying to keep dimensions intact

99% true
if you actually change the dimensions... just jTable.repaint()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1