如何使每个鼠标滚轮步骤滚动1行JScr​​ollPane?

时间:2012-07-09 14:53:16

标签: java swing jscrollpane swingx

我有一个JScrollPane,其内容窗格是JXList。当我在列表上使用鼠标滚轮时,列表一次处理三(3)个项目。无论行高如何,这也适用于表格。我怎样才能改变这一点 - 无论平台如何 - 对于列表和表格,滚动距离恰好是1项?设置块增量不会削减它,因为表中的某些行具有不同的高度。

2 个答案:

答案 0 :(得分:7)

出于纯粹的兴趣(和一点点无聊),我创造了一个工作的例子:

/**
 * 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中完成,我们每次都会将当前的“滚动位置”增加一个块。这可以上下运行,具有不同的行大小,如示例所示。


如评论中提到的 @kleopatra ,您还可以使用MouseWheelListener来重新定义鼠标滚轮的行为。

请参阅官方教程here

答案 1 :(得分:0)

太多的代码无法简单说明:

//   **********  must use self = this ************** 
                // this reference vue-app.  must pass it to self, then pass into callback function (success call back)
                var self = this;  


                fetch(getUser_url).then(function (response) {
                                return response.json();
                        }).then(function (result) {




                                 //------------------------ properties to lowercase ----------------------
                                 // result is upper case, must convert all properties to lowercase, 
                                 // however, the value like password or number MUST remain no change. 

                                 // result = [{}, {}, {}....... {}]
                                    var result_lower_properties= [];

                                    var arrayLength = result.length;

                                    for (var i = 0; i < arrayLength; i++) {

                                        var obj = result[i];
                                        var obj_lower_properties = {};

                                        for (var prop in obj) {

                                                          //console.log(prop.toLowerCase())
                                                          //console.log(obj[prop])

                                                          obj_lower_properties[prop.toLowerCase()] = obj[prop]
                                        }// for

                                        result_lower_properties.push(obj_lower_properties)

                                    }// for

                                  //----------  ENd -------------- properties to lowercase ----------------------





                                 // must use self.user,  do not use this.user, 
                                 // because here, this's scope is just the function (result).   
                                 // we need this reference to vue-app, 
                                 self.user = result_lower_properties;  // [{}, {}, {}]  




    }); // fetch(){}