简体   繁体   English

单击按钮显示表格

[英]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. 我只想做的就是当用户单击一个按钮并且可以正常工作时在JTable显示一些数据,但是正在发生一些奇怪的事情。 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? 谁知道,为什么这种奇怪的行为?

添加表后,调用container.revalidate()

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. 更改窗口大小时,将强制其重新绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM