我有一个表(JTable)值,每10秒实时更新一次(每行更新4个中的最后2列)。我希望这个表能够连续向上滚动,所以我使用了以下示例:
http://wiki.byte-welt.net/wiki/MarqueePanelV
滚动环绕,第一行显示在最后一行之后,除了一件事情以外工作正常:
当我更新DefaultTableModel(我正在使用setValueAt()函数)时,值将打印在其原始位置(就像表格不滚动一样)。仅刷新具有2列的部分,在视图中创建一些断裂。视图在下一个滚动(100毫秒)中得到纠正,但在屏幕上仍然可见。更多,因为我在鼠标悬停在桌面上时停止滚动,更新将在表格视图中创建一个可见的断裂。
有关如何解决此问题的任何想法?
以下是显示问题的代码示例:
package scrollingtable;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.table.DefaultTableModel;
class MarqueePanelV extends JPanel
implements ActionListener, AncestorListener, WindowListener {
private boolean paintChildren;
private boolean scrollingPaused;
private int scrollOffset;
private int wrapOffset;
private int preferredHeight = -1;
private int scrollAmount;
private int scrollFrequency;
private boolean wrap = false;
private int wrapAmount = 0;
private boolean scrollWhenFocused = true;
private Timer timer = new Timer(100, this);
/**
* Convenience constructor that sets both the scroll frequency and
* scroll amount to a value of 5.
*/
public MarqueePanelV() {
this(20, 1);
}
/**
*
* @param scrollFrequency
* @param scrollAmount
*/
@SuppressWarnings("LeakingThisInConstructor")
public MarqueePanelV(int scrollFrequency, int scrollAmount) {
setScrollFrequency(scrollFrequency);
setScrollAmount(scrollAmount);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
addAncestorListener(this);
}
/*
* Translate the location of the children before they are painted so it
* appears they are scrolling bottom to top
*/
@Override
public void paintChildren(Graphics g) {
// Need this so we don't see a flicker of the text before scrolling
if (!paintChildren) {
return;
}
int x = super.getPreferredSize().height;
// Normal painting as the components scroll bottom to top
Graphics2D g2d = (Graphics2D) g;
g2d.translate(0, -scrollOffset);
super.paintChildren(g);
g2d.translate(0, scrollOffset);
// Repaint the start of the components on the bottom edge of the panel once
// all the components are completely visible on the panel.
// (Its like the components are in two places at the same time)
if (isWrap()) {
//wrapOffset = scrollOffset - super.getPreferredSize().height - wrapAmount;
wrapOffset = scrollOffset - x - wrapAmount;
g2d.translate(0, -wrapOffset);
super.paintChildren(g);
g2d.translate(0, wrapOffset);
}
}
/*
* The default preferred size will be half the size of the components added to
* the panel. This will allow room for components to be scrolled on and off
* the panel.
*
* The default height can be overriden by using the setPreferredHeight() method.
*/
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.height = (preferredHeight == -1) ? d.height / 2 : preferredHeight;
return d;
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
public int getPreferredHeight() {
return preferredHeight;
}
/**
* Specify the preferred height on the panel. A value of -1 will cause the
* default preferred with size calculation to be used.
*
* @param preferredHeight preferred height of the panel in pixels
*/
public void setPreferredHeight(int preferredHeight) {
this.preferredHeight = preferredHeight;
revalidate();
}
/**
* Get the scroll amount.
*
* @return the scroll amount in pixels
*/
public int getScrollAmount() {
return scrollAmount;
}
/**
* Specify the scroll amount. The number of pixels to scroll every time
* scrolling is done.
*
* @param scrollAmount scroll amount in pixels
*/
public void setScrollAmount(int scrollAmount) {
this.scrollAmount = scrollAmount;
}
/**
* Get the scroll frequency.
*
* @return the scroll frequency
*/
public int getScrollFrequency() {
return scrollFrequency;
}
/**
* Specify the scroll frequency. That is the number of times scrolling
* should be performed every second.
*
* @param scrollFrequency scroll frequency
*/
public void setScrollFrequency(int scrollFrequency) {
this.scrollFrequency = scrollFrequency;
int delay = 1000 / scrollFrequency;
timer.setInitialDelay(delay);
timer.setDelay(delay);
}
/**
* Get the scroll only when visible property.
*
* @return the scroll only when visible value
*/
public boolean isScrollWhenFocused() {
return scrollWhenFocused;
}
/**
* Specify the scrolling property for unfocused windows.
*
* @param scrollWhenVisible when true scrolling pauses when the window
* loses focus. Scrolling will continue when
* the window regains focus. When false
* scrolling is continuous unless the window
* is iconified.
*/
public void setScrollWhenFocused(boolean scrollWhenFocused) {
this.scrollWhenFocused = scrollWhenFocused;
}
/**
* Get the wrap property.
*
* @return the wrap value
*/
public boolean isWrap() {
return wrap;
}
/**
* Specify the wrapping property. Normal scrolling is such that all the text
* will scroll from bottom to top. When the last part of the text scrolls off
* the bottom edge scrolling will start again from the bottom edge. Therefore
* there is a time when the component is blank as nothing is displayed.
* Wrapping implies that as the end of the text scrolls off the top edge
* the beginning of the text will scroll in from the bottom edge. So the end
* and the start of the text is displayed at the same time.
*
* @param wrap when true the start of the text will scroll in from the bottom
* edge while the end of the text is still scrolling off the top
* edge. Otherwise the panel must be clear of text before it
* will begin again from the bottom edge.
*/
public void setWrap(boolean wrap) {
this.wrap = wrap;
}
/**
* Get the wrap amount.
*
* @return the wrap amount value
*/
public int getWrapAmount() {
return wrapAmount;
}
/**
* Specify the wrapping amount. This specifies the space between the end of the
* text on the top edge and the start of the text from the bottom edge when
* wrapping is turned on.
*
* @param wrapAmount the amount in pixels
*/
public void setWrapAmount(int wrapAmount) {
this.wrapAmount = wrapAmount;
}
/**
* Start scrolling the components on the panel. Components will start
* scrolling from the bottom edge towards the top edge.
*/
public void startScrolling() {
paintChildren = true;
scrollOffset = -getSize().height;
timer.start();
}
/**
* Stop scrolling the components on the panel. The conponents will be
* cleared from the view of the panel
*/
public void stopScrolling() {
timer.stop();
paintChildren = false;
repaint();
}
/**
* The components will stop scrolling but will remain visible
*/
public void pauseScrolling() {
if (timer.isRunning()) {
timer.stop();
scrollingPaused = true;
}
}
/**
* The components will resume scrolling from where scrolling was stopped.
*/
public void resumeScrolling() {
if (scrollingPaused) {
timer.restart();
scrollingPaused = false;
}
}
/**
* Adjust the offset of the components on the panel so it appears that
* they are scrolling from bottom to top.
*/
@Override
public void actionPerformed(ActionEvent e) {
scrollOffset += scrollAmount;
int height = super.getPreferredSize().height;
if (scrollOffset > height) {
scrollOffset = isWrap() ? wrapOffset + scrollAmount : -getSize().height;
}
//System.out.println("scroll offset: " + scrollOffset);
repaint();
}
/**
* Get notified when the panel is added to a Window so we can use a
* WindowListener to automatically start the scrolling of the components.
*/
@Override
public void ancestorAdded(AncestorEvent event) {
SwingUtilities.windowForComponent(this).addWindowListener(this);
}
@Override
public void ancestorRemoved(AncestorEvent event) {
}
@Override
public void ancestorMoved(AncestorEvent event) {
}
// Implement WindowListener
@Override
public void windowOpened(WindowEvent e) {
startScrolling();
}
@Override
public void windowClosing(WindowEvent e) {
stopScrolling();
}
@Override
public void windowClosed(WindowEvent e) {
stopScrolling();
}
@Override
public void windowIconified(WindowEvent e) {
pauseScrolling(); }
@Override
public void windowDeiconified(WindowEvent e) {
resumeScrolling(); }
@Override
public void windowActivated(WindowEvent e) {
if (isScrollWhenFocused()) {
resumeScrolling();
}
}
@Override
public void windowDeactivated(WindowEvent e) {
if (isScrollWhenFocused()) {
pauseScrolling();
}
}
}
public class Main extends JDialog implements ActionListener {
private JTable table;
private DefaultTableModel model;
MarqueePanelV mpv = new MarqueePanelV();
private Timer timer2;
public static void main(String[] args) {
// TODO code application logic here
new Main();
}
Main() {
setSize(600, 400);
this.setLocation(300, 300);
table = new JTable();
model = new DefaultTableModel(20, 2);
table.setModel(model);
table.getColumnModel().getColumn(0).setPreferredWidth(280);
table.getColumnModel().getColumn(1).setPreferredWidth(280);
for(int i = 0; i < 20; i++) {
model.setValueAt(i, i, 0);
model.setValueAt(100 + i, i, 1);
}
mpv.add(table);
add(mpv);
mpv.setWrap(true);
mpv.setWrapAmount(0);
mpv.startScrolling();
table.addMouseListener(new MouseListener() {
// Implement MouseListener
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {
mpv.pauseScrolling();
}
@Override
public void mouseExited(MouseEvent e) {
mpv.resumeScrolling();
}
});
setVisible(true);
timer2 = new Timer(2000, this);
timer2.setInitialDelay(1000);
timer2.setDelay(2000);
timer2.start();
}
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < 20; i++) {
model.setValueAt(100 + i, i, 1);
}
}
}
答案 0 :(得分:0)
问题通过调用mpv.repaint()解决;在timer2动作中:
package scrollingtable;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.table.DefaultTableModel;
class MarqueePanelV extends JPanel
implements ActionListener, AncestorListener, WindowListener {
private boolean paintChildren;
private boolean scrollingPaused;
private int scrollOffset;
private int wrapOffset;
private int preferredHeight = -1;
private int scrollAmount;
private int scrollFrequency;
private boolean wrap = false;
private int wrapAmount = 0;
private boolean scrollWhenFocused = true;
private Timer timer = new Timer(100, this);
/**
* Convenience constructor that sets both the scroll frequency and
* scroll amount to a value of 5.
*/
public MarqueePanelV() {
this(20, 1);
}
/**
*
* @param scrollFrequency
* @param scrollAmount
*/
@SuppressWarnings("LeakingThisInConstructor")
public MarqueePanelV(int scrollFrequency, int scrollAmount) {
setScrollFrequency(scrollFrequency);
setScrollAmount(scrollAmount);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
addAncestorListener(this);
}
/*
* Translate the location of the children before they are painted so it
* appears they are scrolling bottom to top
*/
@Override
public void paintChildren(Graphics g) {
// Need this so we don't see a flicker of the text before scrolling
if (!paintChildren) {
return;
}
int x = super.getPreferredSize().height;
// Normal painting as the components scroll bottom to top
Graphics2D g2d = (Graphics2D) g;
g2d.translate(0, -scrollOffset);
super.paintChildren(g);
g2d.translate(0, scrollOffset);
// Repaint the start of the components on the bottom edge of the panel once
// all the components are completely visible on the panel.
// (Its like the components are in two places at the same time)
if (isWrap()) {
//wrapOffset = scrollOffset - super.getPreferredSize().height - wrapAmount;
wrapOffset = scrollOffset - x - wrapAmount;
g2d.translate(0, -wrapOffset);
super.paintChildren(g);
g2d.translate(0, wrapOffset);
}
}
/*
* The default preferred size will be half the size of the components added to
* the panel. This will allow room for components to be scrolled on and off
* the panel.
*
* The default height can be overriden by using the setPreferredHeight() method.
*/
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.height = (preferredHeight == -1) ? d.height / 2 : preferredHeight;
return d;
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
public int getPreferredHeight() {
return preferredHeight;
}
/**
* Specify the preferred height on the panel. A value of -1 will cause the
* default preferred with size calculation to be used.
*
* @param preferredHeight preferred height of the panel in pixels
*/
public void setPreferredHeight(int preferredHeight) {
this.preferredHeight = preferredHeight;
revalidate();
}
/**
* Get the scroll amount.
*
* @return the scroll amount in pixels
*/
public int getScrollAmount() {
return scrollAmount;
}
/**
* Specify the scroll amount. The number of pixels to scroll every time
* scrolling is done.
*
* @param scrollAmount scroll amount in pixels
*/
public void setScrollAmount(int scrollAmount) {
this.scrollAmount = scrollAmount;
}
/**
* Get the scroll frequency.
*
* @return the scroll frequency
*/
public int getScrollFrequency() {
return scrollFrequency;
}
/**
* Specify the scroll frequency. That is the number of times scrolling
* should be performed every second.
*
* @param scrollFrequency scroll frequency
*/
public void setScrollFrequency(int scrollFrequency) {
this.scrollFrequency = scrollFrequency;
int delay = 1000 / scrollFrequency;
timer.setInitialDelay(delay);
timer.setDelay(delay);
}
/**
* Get the scroll only when visible property.
*
* @return the scroll only when visible value
*/
public boolean isScrollWhenFocused() {
return scrollWhenFocused;
}
/**
* Specify the scrolling property for unfocused windows.
*
* @param scrollWhenVisible when true scrolling pauses when the window
* loses focus. Scrolling will continue when
* the window regains focus. When false
* scrolling is continuous unless the window
* is iconified.
*/
public void setScrollWhenFocused(boolean scrollWhenFocused) {
this.scrollWhenFocused = scrollWhenFocused;
}
/**
* Get the wrap property.
*
* @return the wrap value
*/
public boolean isWrap() {
return wrap;
}
/**
* Specify the wrapping property. Normal scrolling is such that all the text
* will scroll from bottom to top. When the last part of the text scrolls off
* the bottom edge scrolling will start again from the bottom edge. Therefore
* there is a time when the component is blank as nothing is displayed.
* Wrapping implies that as the end of the text scrolls off the top edge
* the beginning of the text will scroll in from the bottom edge. So the end
* and the start of the text is displayed at the same time.
*
* @param wrap when true the start of the text will scroll in from the bottom
* edge while the end of the text is still scrolling off the top
* edge. Otherwise the panel must be clear of text before it
* will begin again from the bottom edge.
*/
public void setWrap(boolean wrap) {
this.wrap = wrap;
}
/**
* Get the wrap amount.
*
* @return the wrap amount value
*/
public int getWrapAmount() {
return wrapAmount;
}
/**
* Specify the wrapping amount. This specifies the space between the end of the
* text on the top edge and the start of the text from the bottom edge when
* wrapping is turned on.
*
* @param wrapAmount the amount in pixels
*/
public void setWrapAmount(int wrapAmount) {
this.wrapAmount = wrapAmount;
}
/**
* Start scrolling the components on the panel. Components will start
* scrolling from the bottom edge towards the top edge.
*/
public void startScrolling() {
paintChildren = true;
scrollOffset = -getSize().height;
timer.start();
}
/**
* Stop scrolling the components on the panel. The conponents will be
* cleared from the view of the panel
*/
public void stopScrolling() {
timer.stop();
paintChildren = false;
repaint();
}
/**
* The components will stop scrolling but will remain visible
*/
public void pauseScrolling() {
if (timer.isRunning()) {
timer.stop();
scrollingPaused = true;
}
}
/**
* The components will resume scrolling from where scrolling was stopped.
*/
public void resumeScrolling() {
if (scrollingPaused) {
timer.restart();
scrollingPaused = false;
}
}
// Implement ActionListener
/**
* Adjust the offset of the components on the panel so it appears that
* they are scrolling from bottom to top.
*/
@Override
public void actionPerformed(ActionEvent e) {
scrollOffset += scrollAmount;
int height = super.getPreferredSize().height;
if (scrollOffset > height) {
scrollOffset = isWrap() ? wrapOffset + scrollAmount : -getSize().height;
}
repaint();
}
// Implement AncestorListener
/**
* Get notified when the panel is added to a Window so we can use a
* WindowListener to automatically start the scrolling of the components.
*/
@Override
public void ancestorAdded(AncestorEvent event) {
SwingUtilities.windowForComponent(this).addWindowListener(this);
}
@Override
public void ancestorRemoved(AncestorEvent event) {
}
@Override
public void ancestorMoved(AncestorEvent event) {
}
// Implement WindowListener
@Override
public void windowOpened(WindowEvent e) {
startScrolling();
}
@Override
public void windowClosing(WindowEvent e) {
stopScrolling();
}
@Override
public void windowClosed(WindowEvent e) {
stopScrolling();
}
@Override
public void windowIconified(WindowEvent e) {
pauseScrolling(); }
@Override
public void windowDeiconified(WindowEvent e) {
resumeScrolling(); }
@Override
public void windowActivated(WindowEvent e) {
if (isScrollWhenFocused()) {
resumeScrolling();
}
}
@Override
public void windowDeactivated(WindowEvent e) {
if (isScrollWhenFocused()) {
pauseScrolling();
}
}
}
public class Main extends JDialog implements ActionListener {
private JTable table;
private DefaultTableModel model;
MarqueePanelV mpv = new MarqueePanelV();
private Timer timer2;
public static void main(String[] args) {
// TODO code application logic here
new Main();
}
Main() {
setSize(600, 400);
this.setLocation(300, 300);
table = new JTable();
model = new DefaultTableModel(20, 2);
table.setModel(model);
table.getColumnModel().getColumn(0).setPreferredWidth(280);
table.getColumnModel().getColumn(1).setPreferredWidth(280);
for(int i = 0; i < 20; i++) {
model.setValueAt(i, i, 0);
model.setValueAt(100 + i, i, 1);
}
mpv.add(table);
add(mpv);
mpv.setWrap(true);
mpv.setWrapAmount(0);
mpv.startScrolling();
table.addMouseListener(new MouseListener() {
// Implement MouseListener
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {
mpv.pauseScrolling();
}
@Override
public void mouseExited(MouseEvent e) {
mpv.resumeScrolling();
}
});
setVisible(true);
timer2 = new Timer(2000, this);
timer2.setInitialDelay(1000);
timer2.setDelay(2000);
timer2.start();
}
int k = 0;
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < 20; i++) {
model.setValueAt(100 + i + k, i, 1);
}
k++;
mpv.repaint();
}
}