2

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 drag & 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 !!!!!

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.setSize(new Dimension(1010, 445));
    internalFrame.add(panel);
    internalFrame.setFocusTraversalKeysEnabled(false);
    internalFrame.setFocusTraversalPolicyProvider(false);


    desktopPane.getDesktopManager();
    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();
            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();
            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]);
        } else {
            vals.add(val[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 paint(Graphics g) {
                super.paint(g);
            }
        };
        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;
}


}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

1 Answers1

1
  1. Don't use setIgnoreRepaint, this can produce unwanted paint artificates or fail to update components when required
  2. desktopPane.setDesktopManager(null) is asking trouble.
  3. Instead of using a javax.swing.Timer for each instance of TestTableScreen, you should have a single Timer which then provides notifications to multiple recipients.
  4. Don't call fireTableDataChanged, this will cause the entire table to re-validate, including the columns and all the rows, this is very, very expensive.
  5. Perfer the use appropriate listeners to monitor the changes to components, rather than performing anonymous method overriding.

This is, essentially, a butchered version demonstrating the use of a single "master" Timer and changes to the table updating process.

It also employs a WindowListener and InternalFrameListener to deregister the views from the Timer when their parent windows are closed, so we're not updating components that are no longer on the screen.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

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;

    private Timer masterTimer;

    public TestMDI() {
        init();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                TestMDI frame = new TestMDI();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public void init() {
        masterTimer = new Timer(200, null);
        masterTimer.start();
        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(400, 400));
//        this.setSize(new Dimension(1000, 800));
        this.pack();
        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));
                    masterTimer.addActionListener(screen);
                    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));
                    masterTimer.addActionListener(screen);
                    if (windowMode == MDI_MODE) {
                        JInternalFrame frame = createInternalFram(screen);
                        frame.addInternalFrameListener(new InternalFrameHandler(screen, masterTimer));
                        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);
                        dialog.addWindowListener(new WindowHandler(screen, masterTimer));
                        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.setSize(new Dimension(1010, 445));
        internalFrame.add(panel);
        internalFrame.setFocusTraversalKeysEnabled(false);
        internalFrame.setFocusTraversalPolicyProvider(false);

        desktopPane.getDesktopManager();
//        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();
                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));
                    dialog.addWindowListener(new WindowHandler((ActionListener)allScreens.get(i), masterTimer));
                    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();
                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));
                    frame.addInternalFrameListener(new InternalFrameHandler((ActionListener)allScreens.get(i), masterTimer));
                    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 InternalFrameHandler extends InternalFrameAdapter {

        private ActionListener listener;
        private Timer timer;

        public InternalFrameHandler(ActionListener listener, Timer timer) {
            this.listener = listener;
            this.timer = timer;
        }

        @Override
        public void internalFrameClosing(InternalFrameEvent e) {
            timer.removeActionListener(listener);
        }

    }

    public class WindowHandler extends WindowAdapter {

        private ActionListener listener;
        private Timer timer;

        public WindowHandler(ActionListener listener, Timer timer) {
            this.listener = listener;
            this.timer = timer;
        }

        @Override
        public void windowClosing(WindowEvent e) {
            timer.removeActionListener(listener);
        }

    }

    public class TestTableScreen extends TestPanel implements ActionListener {

        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);
//                TestTableScreen.this.update(index + "|" + val);
////                SwingUtilities.invokeLater(new Runnable() {
////                    public void run() {
//////                        notRepaint = false;
////                    }
////                });
//            }
//        });

        public TestTableScreen(String title) {
            this.title = title;
            init();
            tableModel1.setTabelName(title);
        }

        @Override
        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);
            update(index + "|" + val);
        }

//        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]);

                if (tableModel1.getRowCount() > index) {
                    tableModel1.setValueAt(val[1], index, 0);
                } else {
                    tableModel1.add(val[1]);
                }

//                List vals = tableModel1.getVector();
//                if (vals.size() > index) {
//                    vals.set(index, val[1]);
//                } else {
//                    vals.add(val[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 AbstractTableModel {

            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 add(String value) {
                list.add(value);
            }

            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;
                        }
                }
            }

            @Override
            public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
                if (aValue instanceof String) {
                    list.set(rowIndex, aValue);
                    fireTableRowsUpdated(rowIndex, rowIndex);
                }
            }
        }

//        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 implements ActionListener {

        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();
        }

        @Override
        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);
            update(index + "|" + val);
        }

//        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 paint(Graphics g) {
                        super.paint(g);
                    }
                };
                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 (JLabel allLable : allLables) {
                this.add(allLable);
            }
        }
    }

    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;
        }

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks for your suggestions, we have some improvement in our application after using your suggestions. However it seems MDI model will repaint all overlapping windows when one of the window repaint. Can we have any method to "turn off" such behavior? – user3584799 May 05 '14 at 03:44
  • That's not surprising – MadProgrammer May 05 '14 at 03:45
  • If there are any method to "turn off" such behavior or separate to different drawing thread? – user3584799 May 05 '14 at 04:06
  • Why? Paint is, generally, pretty efficent – MadProgrammer May 05 '14 at 04:07
  • Actually our application has 2 models, one is free-float(JDialog with JFrame), second is MDI. At free float mode, our application run smooth even open lot of windows with overlapping and update. However , when we switch to MDI mode, the performance become poor, so i think it may caused by java repaint so many overlapping windows when update. – user3584799 May 05 '14 at 06:44