如何使JScrollPane每个鼠标滚轮步长滚动1行? [英] How to make JScrollPane scroll 1 line per mouse wheel step?
问题描述
我有一个JScrollPane,其内容窗格是JXList.当我在列表上使用鼠标滚轮时,该列表一次可处理三(3)个项目.无论行高如何,这也适用于表格.我如何更改此设置,以便无论平台如何,列表和表格的滚动距离都恰好是1个项目?设置块增量不会削减它,因为表中的某些行具有不同的高度.
I have a JScrollPane whose content pane is a JXList. When I use the mouse wheel on the list, the list steps three (3) items at a time. This also works for a table, regardless of row height. How can I change this so that - regardless of platform - for both list and table the scroll distance is exactly 1 item? Setting block increment doesn't cut it because some rows in the table have a different height.
推荐答案
出于纯粹的兴趣(有点无聊),我创建了一个有效的示例:
Out of pure interest (and a little boredom) I created a working example:
/**
* Scrolls exactly one Item a time. Works for JTable and JList.
*
* @author Lukas Knuth
* @version 1.0
*/
public class Main {
private JTable table;
private JList list;
private JFrame frame;
private final String[] data;
/**
* This is where the magic with the "just one item per scroll" happens!
*/
private final AdjustmentListener singleItemScroll = new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
// The user scrolled the List (using the bar, mouse wheel or something else):
if (e.getAdjustmentType() == AdjustmentEvent.TRACK){
// Jump to the next "block" (which is a row".
e.getAdjustable().setBlockIncrement(1);
}
}
};
public Main(){
// Place some random data:
Random rnd = new Random();
data = new String[120];
for (int i = 0; i < data.length; i++)
data[i] = "Set "+i+" for: "+rnd.nextInt();
for (int i = 0; i < data.length; i+=10)
data[i] = "<html>"+data[i]+"<br>Spacer!</html>";
// Create the GUI:
setupGui();
// Show:
frame.pack();
frame.setVisible(true);
}
private void setupGui(){
frame = new JFrame("Single Scroll in Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
frame.add(split);
// Add Data to the table:
table = new JTable(new AbstractTableModel() {
@Override
public int getRowCount() {
return data.length;
}
@Override
public int getColumnCount() {
return 1;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex];
}
});
for (int i = 0; i < data.length; i+=10)
table.setRowHeight(i, 30);
JScrollPane scroll = new JScrollPane(table);
// Add out custom AdjustmentListener to jump only one row per scroll:
scroll.getVerticalScrollBar().addAdjustmentListener(singleItemScroll);
split.add(scroll);
list = new JList<String>(data);
scroll = new JScrollPane(list);
// Add out custom AdjustmentListener to jump only one row per scroll:
scroll.getVerticalScrollBar().addAdjustmentListener(singleItemScroll);
split.add(scroll);
}
public static void main(String[] agrs){
new Main();
}
}
真正的魔力是在自定义中完成的AdjustmentListener
,我们每次将当前的滚动位置"增加一个块.如示例所示,它可以上下移动,并具有不同的行大小.
The real magic is done in the custom AdjustmentListener
, where we go and increase the current "scroll-position" by one single block per time. This works up and down and with different row-sizes, as shown in the example.
As @kleopatra mentioned in the comments, you can also use a MouseWheelListener
to only redefine the behavior of the mouse-wheel.
请参见此处的官方教程.
这篇关于如何使JScrollPane每个鼠标滚轮步长滚动1行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!