0

この JList にはカスタム コードが ActionMapAction を使用しています。カスタムの上下矢印ボタンを使用できるようになりましたが、マウスのスクロールを使用して jLIST を上下にスクロールすることはできません。

マウスのスクロールを実装して JList を上下にスクロールするにはどうすればよいですか?

jlist.setFont(new Font("Calibri",Font.BOLD,16));
jlist.setCellRenderer(new Renderer());
jlist.setFixedCellWidth(100);
jlist.setBorder(new EmptyBorder(10,10, 10, 10));
jlist.addMouseListener(new MouseListener(){

    public void mouseClicked(MouseEvent arg0) {

    }

    public void mouseEntered(MouseEvent arg0) {

    }

    public void mouseExited(MouseEvent arg0) {

    }

    public void mousePressed(MouseEvent e) {
        JList<String> jl = (JList<String>)e.getSource();
        if(e.getClickCount() == 2){
            jlist.setCellRenderer(new Renderer(true));
            int index  = jl.locationToIndex(e.getPoint());
            new OneToOneChat(jlist.getModel().getElementAt(index).toString());
            System.out.print(index);
        }
    }
    public void mouseReleased(MouseEvent e) {
        jlist.setCellRenderer(new Renderer(false));
    }

});


jsp = new JScrollPane(jlist);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

JScrollBar customScroll = jsp.getVerticalScrollBar();
add(jsp);
BasicArrowButton top = new BasicArrowButton(BasicArrowButton.TOP);
top.setAction(new ActionMapAction("", customScroll, "negativeUnitIncrement") );
add(top,BorderLayout.NORTH);

BasicArrowButton bottom = new BasicArrowButton(BasicArrowButton.SOUTH);
bottom.setAction(new ActionMapAction("", customScroll, "positiveUnitIncrement") );
add(bottom,BorderLayout.SOUTH);
}
4

2 に答える 2

2

MouseWheelEvent によって生成されるすべてのイベントをよく理解していないので、リスナーは思ったほど単純ではありませんでした。とにかく、基本的な考え方は、垂直スクロールバーからデフォルトの Action を使用してスクロールすることです:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MouseWheelScroller implements MouseWheelListener
{
    public void mouseWheelMoved(MouseWheelEvent e)
    {
        //  Ignore events generated with a rotation of 0
        //  (not sure why these events are generated)

        int rotation = e.getWheelRotation();

        if (rotation == 0)
            return;

        //  Get the appropriate Action key for the given rotation
        //  (unit/block scroll is system dependent)

        String key = null;

        if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
        {
            key = (rotation < 0) ? "negativeUnitIncrement" : "positiveUnitIncrement";
        }
        else
        {
            key = (rotation < 0) ? "negativeBlockIncrement" : "positiveBlockIncrement";
        }

        //  Get the Action from the scrollbar ActionMap for the given key

        JScrollPane scrollPane = (JScrollPane)e.getComponent();
        JScrollBar vertical = scrollPane.getVerticalScrollBar();

        ActionMap map = vertical.getActionMap();
        Action action = (Action)map.get( key );
        ActionEvent event = new ActionEvent(vertical, ActionEvent.ACTION_PERFORMED, "");

        //  Invoke the Action the appropriate number of times to simulate
        //  default mouse wheel scrolling

        int unitsToScroll = Math.abs( e.getUnitsToScroll() );

        for (int i = 0; i < unitsToScroll; i++)
            action.actionPerformed( event );
    }

    private static void createAndShowUI()
    {
        DefaultListModel<String> model = new DefaultListModel<String>();

        for (int i = 0; i < 200; i++)
        {
            model.addElement("" + i);
        }

        JList<String> list = new JList<String>(model);
        list.setVisibleRowCount(20);

        JScrollPane scrollPane = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.addMouseWheelListener( new MouseWheelScroller());

        JFrame frame = new JFrame("Mouse Wheel Scroller");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( scrollPane );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
于 2013-05-17T03:12:09.137 に答える
1

MouseWheelListener を実装し、JList に追加する必要があります。

http://docs.oracle.com/javase/tutorial/uiswing/events/mousewheellistener.htmlを参照してください。

于 2013-05-16T22:39:51.833 に答える