禁用滚动到焦点单元格

发布于 2024-08-02 12:40:08 字数 157 浏览 6 评论 0原文

我在滚动窗格内有一个 jtable 。当部分不在视图范围内的单元格获得焦点时,如何阻止滚动窗格向上或向下滚动?

问题是,当用户将鼠标悬停在单元格上时,我将单元格设置为可编辑,因此当您将鼠标悬停在部分不可见的单元格上时,视图会突然发生变化。我不喜欢这种行为。关于如何改变它有什么想法吗?

I have a jtable inside of a scrollpane. how can i stop the scrollpane from scrolling up or down when a cell that is partly out of view gains focus?

the problem is i am setting the cells to editable when the user mouses over them, so when you mouse over a cell that's partly out of view, the view changes suddenly. I don't like this behaviour. any ideas on how to change it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-08-09 12:40:08

您可以尝试在桌子上调用“setAutoscrolls(false)”。

尽管 javadoc 讨论了处理鼠标拖动事件,但它也应该禁用行/单元格部分的滚动。

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TableTest extends JFrame {

    private JTable table;
    /**
     * Create a new TableTest Frame.
     * @param title The title of the frame.
     */
    public TableTest() {

        Object[][] tableData = new Object[][] {
                {1, "One"}, {2, "Two"}, {3, "Three"},
                {4, "Four"}, {5, "Five"}, {6, "Six"}};

        DefaultTableModel tableModel = new DefaultTableModel(tableData, new String[] {"A", "B"});
        table = new JTable(tableModel);
        table.setAutoscrolls(false);

        JScrollPane scrolly = new JScrollPane(table);
        setLayout(new BorderLayout());
        add(scrolly, BorderLayout.CENTER);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        TableTest frame = new TableTest();
        frame.pack();
        frame.setSize(200, 100);
        frame.setVisible(true);
    }
}

You could try calling "setAutoscrolls(false)" on your table.

Although the javadoc talks about handing mouse drag events, it should also disable scrolling on row/cell sections also.

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TableTest extends JFrame {

    private JTable table;
    /**
     * Create a new TableTest Frame.
     * @param title The title of the frame.
     */
    public TableTest() {

        Object[][] tableData = new Object[][] {
                {1, "One"}, {2, "Two"}, {3, "Three"},
                {4, "Four"}, {5, "Five"}, {6, "Six"}};

        DefaultTableModel tableModel = new DefaultTableModel(tableData, new String[] {"A", "B"});
        table = new JTable(tableModel);
        table.setAutoscrolls(false);

        JScrollPane scrolly = new JScrollPane(table);
        setLayout(new BorderLayout());
        add(scrolly, BorderLayout.CENTER);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        TableTest frame = new TableTest();
        frame.pack();
        frame.setSize(200, 100);
        frame.setVisible(true);
    }
}
日暮斜阳 2024-08-09 12:40:08

我花了几个小时的时间进行搜索,但我让它为我工作。

使用您的 JTable 子类覆盖此方法

public void scrollRectToVisible(java.awt.Rectangle aRect)
    {
       if(getAutoscrolls())
       super.scrollRectToVisible(aRect);
    }

现在 table.setAutoscrolls(false) 应该可以正常工作。

Took me hours of searching but I got it to work for me.

Override this method with your JTable subclass

public void scrollRectToVisible(java.awt.Rectangle aRect)
    {
       if(getAutoscrolls())
       super.scrollRectToVisible(aRect);
    }

Now table.setAutoscrolls(false) should work properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文