我有JList选择的问题。当我单击JList中的任何对象时,它将被选中(这很好)。
但是当我点击所有对象(空白处)下方时,将选择JList中的最后一项。 我怎么能把它关掉?单击下面的所有对象(在空白处)后,我不想在JList中选择最后一个对象。
在几条评论之后,我在这里添加代码示例。
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class TestOfJList extends JFrame{
private static JList playList;
private static DefaultListModel<String> modelPlayList;
private static JPanel mainPanel;
private static MyCellRenderer myRendererForJlist;
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}catch (Exception e){
e.printStackTrace();
}
JFrame frame = new JFrame("JListTest!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(700, 250, 300, 400);
frame.setResizable(false);
mainPanel = new JPanel();
modelPlayList = new DefaultListModel<String>();
modelPlayList.addElement("Hello world!");
modelPlayList.addElement("Hello heaven!");
modelPlayList.addElement("Hello hell!");
playList = new JList(modelPlayList);
playList.setPreferredSize(new Dimension(270,500));
playList.setVisible(true);
playList.setBackground(Color.LIGHT_GRAY);
playList.setSelectionBackground(Color.YELLOW);
playList.setSelectedIndex(0);
playList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
playList.setDragEnabled(true);
playList.setDropMode(javax.swing.DropMode.ON_OR_INSERT);
playList.setFixedCellHeight(20);
JScrollPane scrollPane = new JScrollPane(playList);
scrollPane.setPreferredSize(new Dimension(300,400));
mainPanel.add(scrollPane);
myRendererForJlist = new MyCellRenderer();
playList.setCellRenderer(myRendererForJlist);
frame.add(mainPanel);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
MCVE 2:
import java.awt.event.MouseListener;
import javax.swing.*;
public class TestList {
public static final String[] ITEMS = { "One", "Two", "Three", "Four", "Five" };
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JList<String> myList = new JList<>(ITEMS);
myList.setVisibleRowCount(10);
JScrollPane sPane = new JScrollPane(myList);
// just to see what listeners are involved
MouseListener[] mouseListeners = myList.getListeners(MouseListener.class);
System.out.println(mouseListeners.length);
MouseMotionListener[] mouseMotionListeners = myList.getListeners(MouseMotionListener.class);
System.out.println(mouseMotionListeners.length);
JOptionPane.showMessageDialog(null, sPane);
}
});
}
}
答案 0 :(得分:-1)
您需要更改滚动窗格的首选大小,因为它会在完整的主面板上展开,当您单击最后一个元素(在空白区域中)时,它会被选中
更改: -
scrollPane.setPreferredSize(new Dimension(300,400));
到
scrollPane.setPreferredSize(new Dimension(300,100));
现在点击下面的列表不会改变选择。
它将如下所示