我在MDI
模式下的应用程序出现问题。
我在JInternalFrame
下打开了许多窗口(JTable
包含JDesktopPane
)。有些窗口是重叠的,当它们在表格中接收到更新时,它似乎重新绘制了所有重叠的窗口,而不仅仅是它自己。这使我的应用程序性能变差,拖慢响应速度慢。删除现有窗口或打开一个新窗口。
为了证明这一点,我举了一个简单的例子来打开许多简单的表,并有一个线程来每200毫秒更新一次表的值。打开大约20个窗口后,性能再次变差。
如果有人遇到与我相同的问题以及解决问题的任何建议?
请帮忙!!!!!
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;
}
}
答案 0 :(得分:1)
setIgnoreRepaint
,这可能会产生不需要的油漆艺术品,或者在需要时无法更新组件desktopPane.setDesktopManager(null)
正在惹麻烦。javax.swing.Timer
,然后向多个收件人提供通知,而不是对TestTableScreen
的每个实例使用Timer
。fireTableDataChanged
,这会导致整个表重新验证,包括列和所有行,这非常非常昂贵。这实质上是一个屠宰版本,展示了使用单个&#34; master&#34; Timer
并更改表更新过程。
它还使用WindowListener
和InternalFrameListener
在父级窗口关闭时从Timer
取消注册视图,因此我们不会更新不再开启的组件屏幕。
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;
}
}
}