1 Reply Latest reply: Nov 30, 2012 5:19 AM by 800268 RSS

    How to keep focus unchanged in popup window while refreshing Parent window

    976869
      I have a parent window (frame) and there are 3 JTables on it. The 3 JTables are binded with model so that when the data in model changes, the JTable will be refreshed automatically. The problem is on the gui, it can pop-up a dialog window. While the popup window is open, the JTables on the parent window could be refreshed by incoming data through other threads. In the case the parent window is refreshed, the cursor focus on the pop-up window is lost, which is quite annoying. Is there any way to keep the focus not changed?
        • 1. Re: How to keep focus unchanged in popup window while refreshing Parent window
          800268
          Updating a table should not change the focus, so you are probably updating it incorrectly (not on the EDT or not firing proper table model events) or calling requestFocus() somewhere.
          import java.awt.EventQueue;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          
          import javax.swing.JFrame;
          import javax.swing.JOptionPane;
          import javax.swing.JScrollPane;
          import javax.swing.JTable;
          import javax.swing.Timer;
          import javax.swing.table.DefaultTableModel;
          
          public class TestFocusWithTableRefresh {
              public static void main(String[] args) {
                  EventQueue.invokeLater(new Runnable() {
                      @Override
                      public void run() {
                          final DefaultTableModel model = new DefaultTableModel(0, 1);
                          Timer timer = new Timer(100, new ActionListener() {
                              @Override
                              public void actionPerformed(ActionEvent e) {
                                  model.insertRow(0, new Object[] { System.currentTimeMillis() });
                              }
                          });
                          JFrame frame = new JFrame("Test");
                          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                          frame.getContentPane().add(new JScrollPane(new JTable(model)));
                          frame.pack();
                          frame.setLocationRelativeTo(null);
                          frame.setVisible(true);
          
                          timer.start();
                          JOptionPane.showInputDialog("See the table change?");
                          timer.stop();
                      }
                  });
              }
          }