1 Reply Latest reply on Nov 30, 2012 11:19 AM by walterln

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

      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
          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() {
                      public void run() {
                          final DefaultTableModel model = new DefaultTableModel(0, 1);
                          Timer timer = new Timer(100, new ActionListener() {
                              public void actionPerformed(ActionEvent e) {
                                  model.insertRow(0, new Object[] { System.currentTimeMillis() });
                          JFrame frame = new JFrame("Test");
                          frame.getContentPane().add(new JScrollPane(new JTable(model)));
                          JOptionPane.showInputDialog("See the table change?");