0 Replies Latest reply: Apr 29, 2014 2:12 AM by 682a9017-06b5-481f-b807-654a9e700a1c RSS

    MDI JTable Overlap Area Repaint Problem

    682a9017-06b5-481f-b807-654a9e700a1c

      Hi all,

       

      I have a problem for my application in MDI mode.

      I open many windows (JInternalFrame contain JTable) under JDesktopPane. Some of the windows are overlapping and when they receive update in the table, it seems repaint all of the overlapping windows, not only itself. This make my application performance become poor, slow respond for drap & drop an existing window or open a new window.

      To prove this, i make a simple example for open many simple table and have a thread to update the table's value for every 200 mill second. After i open about 20 windows, the performance become poor again.

      If anyone face the same problem with me and any suggestions to solve the problem ?

       

      Please help !!!!!

       

      Following are my sources:

       

       

      public class TestMDI extends JFrame {

          private static final long serialVersionUID = 1L;

       

       

          private JPanel contentPanel;

          private JDesktopPane desktopPane;

          private JMenuBar menuBar;

          private List<TestPanel> allScreens = new ArrayList<TestPanel>();

          private List<JDialog> freeFloatDialogs = new ArrayList<JDialog>();

          private List<JInternalFrame> mdiInternalFrm = new ArrayList<JInternalFrame>();

          int x = 0;

          int y = 0;

          int index = 0;

       

       

          private static int MDI_MODE = 0;

          private static int FREE_FLOAT_MODE = 1;

          private int windowMode = MDI_MODE;

       

       

          public TestMDI() {

              init();

          }

       

       

          public static void main(String[] args) {

              new TestMDI().show();

          }

       

       

          public void init() {

              contentPanel = new JPanel();

              desktopPane = new JDesktopPane();

              desktopPane.setDragMode(JDesktopPane.LIVE_DRAG_MODE);

              desktopPane.setFocusTraversalKeysEnabled(false);

              desktopPane.setFocusTraversalPolicyProvider(false);

              desktopPane.setBorder(null);

              desktopPane.setIgnoreRepaint(true);

              desktopPane.setPreferredSize(new Dimension(1000, 800));

              this.setSize(new Dimension(1000, 800));

              menuBar = new JMenuBar();

              JMenu menu1 = new JMenu("Test");

              JMenuItem menuItem1 = new JMenuItem("Open Lable Screen");

              menuItem1.addActionListener(new ActionListener() {

                  public void actionPerformed(ActionEvent e) {

                      for (int i = 1; i < 4; i++) {

                          final TestJLableScreen screen = new TestJLableScreen("Screen  " + (allScreens.size() + 1));

                          screen.startTime();

                          if (windowMode == MDI_MODE) {

                              JInternalFrame frame = createInternalFram(screen);

                              desktopPane.add(frame);

                              mdiInternalFrm.add(frame);

                              if (allScreens.size() * 60 + 100 < 1000) {

                                  x = allScreens.size() * 60;

                                  y = 60;

                              } else {

                                  x = 60 * index;

                                  y = 120;

                                  index++;

                              }

                              frame.setLocation(x, y);

                              frame.setVisible(true);

                          } else {

                              JDialog dialog = createJDialog(screen);

                              freeFloatDialogs.add(dialog);

                              if (i * 60 + 100 < 1000) {

                                  x = i * 60;

                                  y = 60;

                              } else {

                                  x = 60 * index;

                                  y = 120;

                                  index++;

                              }

                              dialog.setLocation(x, y);

                              dialog.setVisible(true);

                          }

       

       

                          allScreens.add(screen);

                      }

                  }

              });

       

       

              JMenuItem menuItem2 = new JMenuItem("Open Table Screen");

              menuItem2.addActionListener(new ActionListener() {

                  public void actionPerformed(ActionEvent e) {

                      for (int i = 1; i < 4; i++) {

                          TestTableScreen screen = new TestTableScreen("Screen  " + (allScreens.size() + 1));

                          screen.startTime();

                          if (windowMode == MDI_MODE) {

                              JInternalFrame frame = createInternalFram(screen);

                              desktopPane.add(frame);

                              mdiInternalFrm.add(frame);

                              if (allScreens.size() * 60 + 100 < 1000) {

                                  x = allScreens.size() * 60;

                                  y = 60;

                              } else {

                                  x = 60 * index;

                                  y = 120;

                                  index++;

                              }

                              frame.setLocation(x, y);

                              frame.setVisible(true);

                          } else {

                              JDialog dialog = createJDialog(screen);

                              freeFloatDialogs.add(dialog);

                              if (i * 60 + 100 < 1000) {

                                  x = i * 60;

                                  y = 60;

                              } else {

                                  x = 60 * index;

                                  y = 120;

                                  index++;

                              }

                              dialog.setLocation(x, y);

                              dialog.setVisible(true);

                          }

                          allScreens.add(screen);

                      }

                  }

              });

              menu1.add(menuItem1);

              menu1.add(menuItem2);

              this.setJMenuBar(menuBar);

              this.getJMenuBar().add(menu1);

              this.getJMenuBar().add(createSwitchMenu());

              this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              this.add(desktopPane);

              desktopPane.setDesktopManager(null);

          }

       

       

          public JInternalFrame createInternalFram(final TestPanel panel) {

              final CustomeInternalFrame internalFrame = new CustomeInternalFrame(panel.getTitle(), true, true, true, true) {

                  public void doDefaultCloseAction() {

                      super.doDefaultCloseAction();

                      allScreens.remove(panel);

                  }

              };

              internalFrame.setPanel(panel);

              // internalFrame.setOpaque(false);

              internalFrame.setSize(new Dimension(1010, 445));

              internalFrame.add(panel);

              internalFrame.setFocusTraversalKeysEnabled(false);

              internalFrame.setFocusTraversalPolicyProvider(false);

       

       

              desktopPane.getDesktopManager();

              // internalFrame.setFocusTraversalKeysEnabled(false);

              internalFrame.setIgnoreRepaint(true);

              return internalFrame;

          }

       

       

          public JDialog createJDialog(final TestPanel panel) {

              JDialog dialog = new JDialog(this, panel.getTitle());

              dialog.setSize(new Dimension(1010, 445));

              dialog.add(panel);

              dialog.addWindowListener(new WindowAdapter() {

       

       

                  public void windowClosing(WindowEvent e) {

                      allScreens.remove(panel);

                  }

              });

              return dialog;

          }

       

       

          public JMenu createSwitchMenu() {

              JMenu menu = new JMenu("Test2");

              JMenuItem menuItem1 = new JMenuItem("Switch FreeFloat");

              menuItem1.addActionListener(new ActionListener() {

                  public void actionPerformed(ActionEvent e) {

                      windowMode = FREE_FLOAT_MODE;

                      for (JInternalFrame frm : mdiInternalFrm) {

                          frm.setVisible(false);

                          frm.dispose();

                          frm = null;

                      }

                      mdiInternalFrm.clear();

                      remove(desktopPane);

                      desktopPane.removeAll();

      //                revalidate();

                      repaint();

                      add(contentPanel);

                      index = 0;

       

       

                      for (JDialog dialog : freeFloatDialogs) {

                          dialog.setVisible(false);

                          dialog.dispose();

                          dialog = null;

                      }

       

       

                      freeFloatDialogs.clear();

                      for (int i = 0; i < allScreens.size(); i++) {

                          JDialog dialog = createJDialog(allScreens.get(i));

                          freeFloatDialogs.add(dialog);

                          if (i * 60 + 100 < 1000) {

                              x = i * 60;

                              y = 60;

                          } else {

                              x = 60 * index;

                              y = 120;

                              index++;

                          }

                          dialog.setLocation(x, y);

                          dialog.setVisible(true);

                      }

                  }

              });

              JMenuItem menuItem2 = new JMenuItem("Switch MDI");

              menuItem2.addActionListener(new ActionListener() {

                  public void actionPerformed(ActionEvent e) {

                      windowMode = MDI_MODE;

                      remove(contentPanel);

                      add(desktopPane);

                      for (int i = 0; i < freeFloatDialogs.size(); i++) {

                          freeFloatDialogs.get(i).setVisible(false);

                          freeFloatDialogs.get(i).dispose();

                      }

                      freeFloatDialogs.clear();

      //                revalidate();

                      repaint();

                      for (JInternalFrame frm : mdiInternalFrm) {

                          frm.setVisible(false);

                          frm.dispose();

                          frm = null;

                      }

                      mdiInternalFrm.clear();

                      index = 0;

                      for (int i = 0; i < allScreens.size(); i++) {

                          JInternalFrame frame = createInternalFram(allScreens.get(i));

                          desktopPane.add(frame);

                          mdiInternalFrm.add(frame);

                          if (i * 60 + 100 < 1000) {

                              x = i * 60;

                              y = 60;

                          } else {

                              x = 60 * index;

                              y = 120;

                              index++;

                          }

                          frame.setLocation(x, y);

                          frame.setVisible(true);

                      }

                  }

              });

              menu.add(menuItem1);

              menu.add(menuItem2);

              return menu;

          }

      }

       

      ######################################################################################################

       

      public class TestTableScreen extends TestPanel {

          private static final long serialVersionUID = 1L;

          JTable testTable = new JTable();

          MyTableModel tableModel1 = new MyTableModel(1);

          private boolean notRepaint = false;

          int start = 0;

          JScrollPane scrollPane = new JScrollPane();

          private Timer timmer = new Timer(200, new ActionListener() {

              public void actionPerformed(ActionEvent e) {

                  Random indexRandom = new Random();

                  final int index = indexRandom.nextInt(50);

                  Random valRandom = new Random();

                  final int val = valRandom.nextInt(600);

                  SwingUtilities.invokeLater(new Runnable() {

                      public void run() {

                          notRepaint = false;

                          TestTableScreen.this.update(index + "|" + val);

                      }

                  });

              }

          });

       

       

          public TestTableScreen(String title) {

              this.title = title;

              init();

              tableModel1.setTabelName(title);

          }

       

       

          public void startTime() {

              timmer.start();

          }

       

       

          public String getTitle() {

              return title;

          }

       

       

          public void update(String updateStr) {

              String[] val = updateStr.split("\\|");

              if (val.length == 2) {

                  int index = Integer.valueOf(val[0]);

                  List vals = tableModel1.getVector();

                  if (vals.size() > index) {

                      vals.set(index, val[1]);

      //                 tableModel1.fireTableRowsUpdated(index, index);

                  } else {

                      vals.add(val[1]);

      //                 tableModel1.fireTableRowsUpdated(vals.size() - 1, vals.size() - 1);

                  }

                  tableModel1.fireTableDataChanged();

              }

          }

       

       

          public TableModel getTableModel() {

              return tableModel1;

          }

       

       

          public void init() {

              testTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

              testTable.setRowSelectionAllowed(true);

              this.testTable.setModel(tableModel1);

              int[] width = { 160, 80, 45, 98, 60, 88, 87, 88, 80, 70, 88, 80, 75, 87, 87, 41, 88, 82, 75, 68, 69 };

              TableColumnModel columnModel = testTable.getColumnModel();

              for (int i = 0; i < width.length; i++) {

                  columnModel.getColumn(i).setPreferredWidth(width[i]);

              }

              testTable.setRowHeight(20);

              tableModel1.fireTableDataChanged();

              this.setLayout(new BorderLayout());

       

       

              TableColumnModel columnMode2 = testTable.getColumnModel();

              int[] width2 = { 200 };

              for (int i = 0; i < width2.length; i++) {

                  columnMode2.getColumn(i).setPreferredWidth(width2[i]);

              }

              scrollPane.getViewport().add(testTable);

              scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

              this.add(scrollPane, BorderLayout.CENTER);

          }

       

       

          class MyTableModel extends DefaultTableModel {

              public List list = new ArrayList();

              String titles[] = new String[] { "袨怓1", "袨怓2", "袨怓3", "袨怓4", "袨怓5", "袨怓6", "袨怓7", "袨怓8", "袨怓9", "袨怓10", "袨怓11",

                      "袨怓12", "袨怓13", "袨怓14", "袨怓15", "袨怓16", "袨怓17", "袨怓18", "袨怓19", "袨怓20", "袨怓21" };

              String tabelName = "";

       

       

              int type_head = 0;

              int type_data = 1;

              int type = 1;

       

       

              public MyTableModel(int type) {

                  super();

                  this.type = type;

                  for (int i = 0; i < 50; i++) {

                      list.add(i);

                  }

              }

       

       

              public void setTabelName(String name) {

                  this.tabelName = name;

              }

       

       

              public int getRowCount() {

                  if (list != null) {

                      return list.size();

                  }

                  return 0;

              }

       

       

              public List getVector() {

                  return list;

              }

       

       

              public int getColumnCount() {

                  if (type == 0) {

                      return 1;

                  } else {

                      return titles.length;

                  }

              }

       

       

              public String getColumnName(int c) {

                  if (type == 0) {

                      return "head";

                  } else {

                      return titles[c];

                  }

              }

       

       

              public boolean isCellEditable(int nRow, int nCol) {

                  return false;

              }

       

       

              public Object getValueAt(int r, int c) {

                  if (list.size() == 0) {

                      return null;

                  }

       

       

                  switch (c) {

                  default:

                      if (type == 0) {

                          return r + " " + c + "  test ";

                      } else {

                          return list.get(r) + "   " + c;

                      }

                  }

              }

          }

       

       

          public boolean isNotRepaint() {

              return notRepaint;

          }

       

       

          public void setNotRepaint(boolean notRepaint) {

              this.notRepaint = notRepaint;

          }

      }

       

      ######################################################################################################

       

       

      public class TestPanel extends JPanel {

          protected String title = "";

          protected boolean needRepaint = false;

          protected boolean isFirstOpen = true;

       

       

          public String getTitle() {

              return title;

          }

       

       

          public void setNeedRepaint(boolean flag) {

              this.needRepaint = flag;

          }

       

       

          public boolean isNeedRepaint() {

              return needRepaint;

          }

       

       

          public boolean isFirstOpen() {

              return isFirstOpen;

          }

       

       

          public void setFirstOpen(boolean isFirstOpen) {

              this.isFirstOpen = isFirstOpen;

          }

      }

       

      ######################################################################################################

       

      public class TestJLableScreen extends TestPanel {

          private static final long serialVersionUID = 1L;

          private JLabel[] allLables = new JLabel[20];

          private Timer timmer = new Timer(20, new ActionListener() {

              public void actionPerformed(ActionEvent e) {

                  Random indexRandom = new Random();

                  final int index = indexRandom.nextInt(10);

                  Random valRandom = new Random();

                  final int val = valRandom.nextInt(600);

                  SwingUtilities.invokeLater(new Runnable() {

                      public void run() {

                          TestJLableScreen.this.setNeedRepaint(true);

                          TestJLableScreen.this.update(index + "|" + val);

                      }

                  });

              }

          });

       

       

          public TestJLableScreen(String title) {

              this.title = title;

              init();

          }

       

       

          public void startTime() {

              timmer.start();

          }

       

       

          public String getTitle() {

              return title;

          }

       

       

          public void update(String updateStr) {

              String[] val = updateStr.split("\\|");

              if (val.length == 2) {

                  int index = Integer.valueOf(val[0]);

                  allLables[index * 2 + 1].setText(val[1]);

              }

          }

       

       

          public void init() {

              this.setLayout(new GridLayout(10, 2));

              boolean flag = true;

              for (int i = 0; i < allLables.length; i++) {

                  allLables[i] = new JLabel() {

                      // public void setText(String text) {

                      // super.setText(text);

                      // // System.out.println("  setText " + getTitle() + "   ; " + this.getName());

                      // }

       

       

                      public void paint(Graphics g) {

                          super.paint(g);

                          // System.out.println("  paint " + getTitle() + "   ; " + this.getName());

                      }

       

       

                      // public void repaint() {

                      // super.repaint();

                      // System.out.println("  repaint " + getTitle() + "   ; " + this.getName());

                      // }

                  };

                  allLables[i].setName("" + i);

                  if (i % 2 == 0) {

                      allLables[i].setText("Name " + i + "  : ");

                  } else {

                      allLables[i].setOpaque(true);

                      if (flag) {

                          allLables[i].setBackground(Color.YELLOW);

                          flag = false;

                      } else {

                          allLables[i].setBackground(Color.CYAN);

                          flag = true;

                      }

                      allLables[i].setText(i * 8 + "");

                  }

              }

              for (int i = 0; i < allLables.length; i++) {

                  this.add(allLables[i]);

              }

          }

      }

       

      ######################################################################################################

       

      public class CustomeInternalFrame extends JInternalFrame {

          protected TestPanel panel;

       

       

          public CustomeInternalFrame() {

              this("", false, false, false, false);

          }

       

       

          public CustomeInternalFrame(String title) {

              this(title, false, false, false, false);

          }

       

       

          public CustomeInternalFrame(String title, boolean resizable) {

              this(title, resizable, false, false, false);

          }

       

       

          public CustomeInternalFrame(String title, boolean resizable, boolean closable) {

              this(title, resizable, closable, false, false);

          }

       

       

          public CustomeInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable) {

              this(title, resizable, closable, maximizable, false);

          }

       

       

          public CustomeInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable,

                  boolean iconifiable) {

              super(title, resizable, closable, maximizable, iconifiable);

          }

       

       

          public TestPanel getPanel() {

              return panel;

          }

       

       

          public void setPanel(TestPanel panel) {

              this.panel = panel;

          }

       

       

      }