0

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

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ezzaral
0

You don't need to re-create all of those things when you pick another file for the table. Just create a new table model from that file and use setModel() to set the JTable to the new model. There is no reason to discard the GUI components themselves.

Edited by Ezzaral: added link

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.