我有一个JScrollPane,里面有很多巨大的JTable。
当我通过鼠标滚动向下滚动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
}
答案 0 :(得分:2)
这可能是因为每个表都有自己的JScrollPane(至少是表C的情况)。当鼠标位于表格内时,最里面的滚动窗格是对鼠标滚轮作出反应的窗格。我认为你不应该修改它:这是正常行为。
已编辑:
这很丑陋,但我不确定还有其他办法。我已经能够通过其UI删除添加到滚动窗格的侦听器(即使禁用滚轮滚动)来使您的示例工作:
for (MouseWheelListener listener : jScrollPane2.getMouseWheelListeners()) {
jScrollPane2.removeMouseWheelListener(listener);
}
// same for jScrollPane3
它并不优雅,但在scrollabl面板中有几个可滚动的表也不是。
答案 1 :(得分:1)
我有一个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();
}
});
}
}
答案 2 :(得分:0)
我只是使用Eclipse Visual Editor来完成它。 VE项目刚刚去世。我喜欢只编写重要的Swing内容,而不是每一个JLabel ......