Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTable与autoscroll一起使用,更新单元格时出现问题_Java_Swing_Jtable_Jpanel_Autoscroll - Fatal编程技术网

Java JTable与autoscroll一起使用,更新单元格时出现问题

Java JTable与autoscroll一起使用,更新单元格时出现问题,java,swing,jtable,jpanel,autoscroll,Java,Swing,Jtable,Jpanel,Autoscroll,我有一个值表JTable,它大约每10秒实时更新一次,每行更新4列中的最后2列。我希望此表能够连续向上滚动,因此我使用了以下示例: 滚动环绕,第一行显示在最后一行之后,工作正常,但有一点除外: 当我使用setValueAt函数更新DefaultTableModel时,这些值会打印在原始位置,就像表格不会滚动一样。仅刷新具有2列的部分,并在视图中创建一些断开。该视图在100毫秒后的下一次滚动时被更正,但在屏幕上仍然可见。此外,由于我在鼠标位于表格上方时停止滚动,更新将在表格视图中创建一个可见的断开

我有一个值表JTable,它大约每10秒实时更新一次,每行更新4列中的最后2列。我希望此表能够连续向上滚动,因此我使用了以下示例:

滚动环绕,第一行显示在最后一行之后,工作正常,但有一点除外:

当我使用setValueAt函数更新DefaultTableModel时,这些值会打印在原始位置,就像表格不会滚动一样。仅刷新具有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);
        }
    }
}

通过调用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();
    }
}

最有可能是来自您的侦听器的更新?为了更快地获得更好的帮助,请发布一个演示了的上午问题,简短、可运行、可编译,其中XxxTableModel.setValueAt的值应该从中计算Random@mKorbel我必须等8个小时才能在这里发布代码我必须等8个小时才能在这里发布代码什么?!?所以这并不是强加给你的。顺便说一句-345行代码,但仍然没有主字符串[]?查看@mkorbel的评论无法回答他们自己的问题否,但您应该分享| |。。这个密码使问题变得复杂。但为了更快地获得更好的帮助,请发布一个远低于413 LOC的电子邮件。