Samer El Gendy
Samer El Gendy

Reputation: 1683

Show table on button click

Simply what I want to do is to show some data in JTable when user click a button and it works correctly, however something weird is happening. When I click button at first time nothing happens but when I maximize frame, the table appears!

ActionListener

b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                boolean state = external.isSelected();
                DefaultTableModel model = new DefaultTableModel(ManhattanTable(values), Headers(values));
                   JTable table = new JTable(model);
                   table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                   container.add(new JScrollPane(table));
                   table.setVisible(false);
                if(state) {
                    PrintStream out = null;
                    try {
                        out = new PrintStream(new FileOutputStream("output.txt"));
                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    System.setOut(out);
                    long start= System.currentTimeMillis();
                    Manhattan(values);
                    long end=System.currentTimeMillis();
                    out.println("time: "+(end-start)+" milliseconds");
                    out.println("Number of input data: "+values.size());    
                } else {
                     table.setVisible(true);
                }           
            }
        }); 

Anyone knows, why this strange behaviour?

Upvotes: 0

Views: 1793

Answers (2)

Kurtymckurt
Kurtymckurt

Reputation: 337

The problem is that you create the table and set the properties, but you didn't tell the window to redraw it's components. When you change the window size, you're forcing it to redraw.

Upvotes: 2

MByD
MByD

Reputation: 137362

After adding the table, call container.revalidate()

Upvotes: 6

Related Questions