JScrollPane 中的滚动条不适用于 JTables

发布于 2024-11-17 17:16:03 字数 6792 浏览 1 评论 0原文

我有一个 JScrollPane,里面有许多巨大的 JTable。

image

当我通过鼠标滚动向下滚动 JScrollPane 时,当鼠标指针经过 JTable 时它就会停止。 (即滚动条仅在 B 区域有效,但在 A 区域无效。)

如何避免这种情况?谢谢。

这是代码:

/*
 * test.java
 *
 * Created on Jun 26, 2011, 7:39:43 PM
 */
package collector;

import javax.swing.JScrollPane;

/**
 *
 * @author Bhathiya
 */
public class test extends javax.swing.JFrame {

    /** Creates new form test */
    public test() {
        initComponents();
        jScrollPane2.setWheelScrollingEnabled( false );
        jScrollPane3.setWheelScrollingEnabled( false );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setAutoscrolls(false);
        jScrollPane2.setViewportView(jTable1);

        jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable2.setAutoscrolls(false);
        jTable2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jScrollPane3.setViewportView(jTable2);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 131, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(194, 194, 194))
        );

        jScrollPane1.setViewportView(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new test().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration
}

I have a JScrollPane with number of huge JTables in it.

image

While i'm scrolling down the JScrollPane by mouse scroll, it just stops when the mouse pointer passes a JTable. (i.e. Scrollbar works only on B area but not in A.)

How can I avoid this? Thanks.

Here is the code:

/*
 * test.java
 *
 * Created on Jun 26, 2011, 7:39:43 PM
 */
package collector;

import javax.swing.JScrollPane;

/**
 *
 * @author Bhathiya
 */
public class test extends javax.swing.JFrame {

    /** Creates new form test */
    public test() {
        initComponents();
        jScrollPane2.setWheelScrollingEnabled( false );
        jScrollPane3.setWheelScrollingEnabled( false );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setAutoscrolls(false);
        jScrollPane2.setViewportView(jTable1);

        jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable2.setAutoscrolls(false);
        jTable2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jScrollPane3.setViewportView(jTable2);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 131, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(194, 194, 194))
        );

        jScrollPane1.setViewportView(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new test().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration
}

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

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

发布评论

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

评论(3

眼泪也成诗 2024-11-24 17:16:03

这可能是因为每个表都有自己的 JScrollPane (至少表 C 是这样)。当鼠标位于表格内时,最里面的滚动窗格是对鼠标滚轮做出反应的滚动窗格。我认为你不应该修改它:这是正常行为。

编辑:

这很丑陋,但我不确定是否还有其他方法可以做到这一点。我已经能够通过删除由 UI 添加到滚动窗格的侦听器来使您的示例正常工作(即使禁用滚轮滚动):

for (MouseWheelListener listener : jScrollPane2.getMouseWheelListeners()) {
    jScrollPane2.removeMouseWheelListener(listener);
}
// same for jScrollPane3

这并不优雅,但在可滚动面板内有多个可滚动表也不是。

It's probably caused by the fact that that each table has its own JScrollPane (that's at least the case of your table C). When the mouse is inside a table, the inner-most scroll pane is the one that reacts to the mouse wheel. I don't think you should modify that: it's normal behaviour.

EDITED :

It's ugly, but I'm not sure there is another way of doing it. I've been able to make your example work by removing the listener added to the scroll pane by its UI (even if wheel scrolling is disabled) :

for (MouseWheelListener listener : jScrollPane2.getMouseWheelListeners()) {
    jScrollPane2.removeMouseWheelListener(listener);
}
// same for jScrollPane3

It's not elegant, but having a several scrollable tables inside a scrollabl panel isn't either.

眼泪淡了忧伤 2024-11-24 17:16:03

我有一个 JScrollPane,里面有许多巨大的 JTable。

我假设每个子表也都添加到 JScrollPane 中。如果是这样,则滚动事件将传递到这些滚动窗格而不是向外滚动窗格。因此,您需要

scrollPane.setWheelScrollingEnabled( false );

在主滚动窗格中包含的每个滚动窗格上使用:。

更新:

问题是,即使滚轮已被禁用,滚动窗格也会拦截所有 mouseScrolled 事件,并且它们不会传递到父滚动窗格。所以解决方案是不要将表添加到滚动窗格中。而是使用带有 BorderLayout 的 JPanel。像这样的东西:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableMultiple extends JPanel
{
    public TableMultiple()
    {
        setLayout( new BorderLayout() );
        Box box = Box.createVerticalBox();
        box.add(Box.createVerticalStrut(50));

        JPanel panel = new JPanel( new BorderLayout() );
        JTable table = new JTable(10, 5);
        panel.add(table.getTableHeader(), BorderLayout.NORTH);
        panel.add(table);
        box.add( panel );

        box.add(Box.createVerticalStrut(50));

        JTable table2 = new JTable(100, 5);
        table2.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane2 = new JScrollPane( table2 );
        box.add( scrollPane2 );

        box.add(Box.createVerticalStrut(50));

        JTable table3 = new JTable(10, 5);
        box.add( table3 );

        box.add(Box.createVerticalStrut(50));

        add( new JScrollPane(box) );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("TableMultiple");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TableMultiple() );
        frame.setSize(400, 500);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

I have a JScrollPane with number of huge JTables in it.

I assume each of the child tables are also added to a JScrollPane. If so then the scroll event is being passed to these scroll panes and not the out scroll pane. So you need to use:

scrollPane.setWheelScrollingEnabled( false );

on each of the scrollpanes contained within the main scrollpane.

Update:

The problem is that even though the scroll wheel has been disable, the scrollpane intercepts all the mouseScrolled events and they don't get passed to the parent scrollpane. So the solution is to not add the table to a scrollpane. Instead use a JPanel with a BorderLayout. Something like:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableMultiple extends JPanel
{
    public TableMultiple()
    {
        setLayout( new BorderLayout() );
        Box box = Box.createVerticalBox();
        box.add(Box.createVerticalStrut(50));

        JPanel panel = new JPanel( new BorderLayout() );
        JTable table = new JTable(10, 5);
        panel.add(table.getTableHeader(), BorderLayout.NORTH);
        panel.add(table);
        box.add( panel );

        box.add(Box.createVerticalStrut(50));

        JTable table2 = new JTable(100, 5);
        table2.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane2 = new JScrollPane( table2 );
        box.add( scrollPane2 );

        box.add(Box.createVerticalStrut(50));

        JTable table3 = new JTable(10, 5);
        box.add( table3 );

        box.add(Box.createVerticalStrut(50));

        add( new JScrollPane(box) );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("TableMultiple");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TableMultiple() );
        frame.setSize(400, 500);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
旧时光的容颜 2024-11-24 17:16:03

我只需使用 Eclipse 可视化编辑器即可完成此操作。不过 VE 项目刚刚结束。我喜欢只编写重要的 Swing 内容,而不是每个 JLabel...

I'd just do it using the Eclipse Visual Editor. The VE project has just died though. I like to code only the important Swing stuff, not every single JLabel...

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