10

私はこの答えを求めてインターネットを精査してきました。内部にアイテムを含む単純な JList があります。右クリックすると、「使用、ドロップ、キャンセル」などのメニューがポップアップ表示されます。しかし、私は困惑しています。

以下のコードは、内部にいくつかのアイテムを含む単純な JList を生成します。コードに右クリックを追加しようとしましたが、機能しません。ヘルプ?

これが私がこれまでに持っているものです:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import javax.swing.*;



public class inv extends JApplet implements MouseListener {

    JList listbox;



     public void init()
     {


    String  listData[] = { "Item 1","Item 2","Item 3","Item 4" };
listbox = new JList( listData );

     listbox.addMouseListener( new MouseAdapter()
     {
        public void mousePressed(MouseEvent e)
        {
            if ( SwingUtilities.isRightMouseButton(e) )
            {
                listbox.setSelectedIndex(getRow(e.getPoint()));
            }
        }
     });

    listbox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


        add(listbox);
                    listbox.setVisible(true);

            listbox.setFocusable(false);

     }



     private int getRow(Point point)
     {
        return listbox.locationToIndex(point);
}

      public void mousePressed(MouseEvent e) {
      }

      public void mouseReleased(MouseEvent e) {
      }

      public void mouseEntered(MouseEvent e) {
      }

      public void mouseExited(MouseEvent e) {
      }

      public void mouseClicked(MouseEvent e) {
      }


     public void stop()
     {
     }

     public void paint(Graphics g)
     {
     }

}
4

3 に答える 3

6

典型的な間違いの 1 つは、JPopupMenu.setVisible(true)何かが起こることを期待して電話をかけることです。このコンポーネントを起動するには別の方法が必要です。次の行に沿ってマウス リスナーを書き直します。

    listbox.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                JPopupMenu menu = new JPopupMenu();
                JMenuItem item = new JMenuItem("Say hello");
                item.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JOptionPane.showMessageDialog(inv.this, "Hello "
                                + listbox.getSelectedValue());
                    }
                });
                menu.add(item);
                menu.show(inv.this, 5, listbox.getCellBounds(
                        listbox.getSelectedIndex() + 1,
                        listbox.getSelectedIndex() + 1).y);
            }
        }
    });

例を短くするために、1 つの項目のみを追加しますが、さらに追加することもできます。私が使用する show メソッドでは、メニューを表示するコンポーネントの場所も指定する必要があります。この例に示すように、場所はリスト自体から取得できます。

于 2013-04-15T11:28:16.627 に答える
6

意味が分からない。これは、あなたが指定したように機能するように見えるコードですが、冗長またはバグのあるステートメントをいくつでも取り除くことを除けば、投稿したものとほぼ同じです。

/*
<applet code='inv' width='200' height='200'>
</applet>
*/
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.*;

public class inv extends JApplet { //implements MouseListener {

    JList listbox;

     public void init()
     {


    String  listData[] = { "Item 1","Item 2","Item 3","Item 4" };
listbox = new JList( listData );

     listbox.addMouseListener( new MouseAdapter()
     {
        public void mousePressed(MouseEvent e)
        {
            System.out.println(e);
            if ( SwingUtilities.isRightMouseButton(e) )
            {
                System.out.println("Row: " + getRow(e.getPoint()));
                listbox.setSelectedIndex(getRow(e.getPoint()));
            }
        }
     });

    listbox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        add(listbox);
                    // unnecessary
                    //listbox.setVisible(true);

            listbox.setFocusable(false);
     }



     private int getRow(Point point)
     {
        return listbox.locationToIndex(point);
}

/** Why implement MouseListener, while adding a MouseAdapter?
Do you have ANY idea what your doing?

      public void mousePressed(MouseEvent e) {
      }

      public void mouseReleased(MouseEvent e) {
      }

      public void mouseEntered(MouseEvent e) {
      }

      public void mouseExited(MouseEvent e) {
      }

      public void mouseClicked(MouseEvent e) {
      }
*/

    /** What is this nonsense supposed to achieve?
    Don't override empty methods with empty implementations!
     public void stop()
     {
     }
     */

    /** What is this nonsense supposed to achieve?
     public void paint(Graphics g)
     {
     }
     */
}

出力

java.awt.event.MouseEvent[MOUSE_PRESSED,(31,22),absolute(39,72),button=3,modifiers=Meta+Button3,extModifiers=Button3,clickCount=1] on javax.swing.JList[,0,0,200x200,alignmentX=0.0,alignmentY=0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],selectionForeground=sun.swing.PrintColorUIResource[r=51,g=51,b=51],visibleRowCount=8,layoutOrientation=0]
Row: 1
java.awt.event.MouseEvent[MOUSE_PRESSED,(29,39),absolute(37,89),button=3,modifiers=Meta+Button3,extModifiers=Button3,clickCount=1] on javax.swing.JList[,0,0,200x200,alignmentX=0.0,alignmentY=0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],selectionForeground=sun.swing.PrintColorUIResource[r=51,g=51,b=51],visibleRowCount=8,layoutOrientation=0]
Row: 2
java.awt.event.MouseEvent[MOUSE_PRESSED,(36,65),absolute(468,192),button=3,modifiers=Meta+Button3,extModifiers=Button3,clickCount=1] on javax.swing.JList[,0,0,200x200,alignmentX=0.0,alignmentY=0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],selectionForeground=sun.swing.PrintColorUIResource[r=51,g=51,b=51],visibleRowCount=8,layoutOrientation=0]
Row: 3
java.awt.event.MouseEvent[MOUSE_PRESSED,(45,11),absolute(477,138),button=3,modifiers=Meta+Button3,extModifiers=Button3,clickCount=1] on javax.swing.JList[,0,0,200x200,alignmentX=0.0,alignmentY=0.0,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,fixedCellHeight=-1,fixedCellWidth=-1,horizontalScrollIncrement=-1,selectionBackground=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],selectionForeground=sun.swing.PrintColorUIResource[r=51,g=51,b=51],visibleRowCount=8,layoutOrientation=0]
Row: 0

Tool completed successfully
于 2011-01-31T18:07:25.580 に答える
5

前の回答に基づいて、以下のコードはすぐに項目を選択し(右クリック)、マウス クリックの横にポップアップを表示します。

listbox.addMouseListener( new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
        if ( SwingUtilities.isRightMouseButton(e) ) {      
           listbox.setSelectedIndex(listbox.locationToIndex(e.getPoint()));

            JPopupMenu menu = new JPopupMenu();
            JMenuItem itemRemove = new JMenuItem("Remove");
            itemRemove.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    // This could probably be improved, but assuming you
                    // also keep the values in an ArrayList, you can 
                    // remove the element with this:
                    //array_list.remove(listbox.getSelectedValue());
                    //listbox.setListData((String[]) array_list.toArray(new String[array_list.size()]));

                    System.out.println("Remove the element in position " + listbox.getSelectedValue());

                }
            });
            menu.add(itemRemove);
            menu.show(listbox, e.getPoint().x, e.getPoint().y);            
        }
    }
});

アイテムを削除する可能な方法を示すコメント付きのセクションがあります。上の要素のコピーを含むArrayList(と呼ばれる)の存在を前提としています。をリフレッシュするためにメソッドを使用しました。もっと効率的な方法があるはずですが、リストが短い場合はそれで十分です。array_listJListto_arrayJList

于 2016-07-23T21:00:23.190 に答える