出于纯粹的兴趣(有点无聊),我创建了一个工作示例:
public class Main { private JTable table; private JList list; private Jframe frame; private final String[] data; 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(); }}真正的魔力是在custom中完成的
AdjustmentListener,我们每次都将当前的“滚动位置”增加一个单节。如示例所示,它可以上下移动并具有不同的行大小。
就像 注释中 提到的 @kleopatra一样 ,您也可以使用a
MouseWheelListener来重新定义鼠标滚轮的行为。
请参阅此处的官方教程。



