是否可以在JList中获取所选元素的位置?我想让它将JFrame放在点击选项下方。
答案 0 :(得分:6)
您需要使用JList#getCellBounds
int selectedIndex = list.getSelectedIndex();
Rectangle bounds = list.getSelectedBounds(selectedIndex, selectedIndex);
这将为您提供JList
上下文中所选项目的位置,您需要将其转换为屏幕空间...
Point p = bounds.getLocation();
SwingUtilities.convertPointToScreen(p, list);
您可能还想查看The Use of Multiple JFrames: Good or Bad Practice?
例如......
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class List {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new List();
}
public List() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
final JFrame selected = new JFrame("Selected");
selected.add(new JLabel("Here I am"));
selected.pack();
DefaultListModel model = new DefaultListModel();
for (int index = 0; index < 1000; index++) {
model.addElement("#" + index);
}
final JList list = new JList(model);
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int index = list.getSelectedIndex();
Rectangle bounds = list.getCellBounds(index, index);
Point p = bounds.getLocation();
p.y += bounds.height;
SwingUtilities.convertPointToScreen(p, list);
p.x -= selected.getWidth();
selected.setLocation(p);
selected.setVisible(true);
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(list));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
答案 1 :(得分:3)
您可以使用JList
的{{3}}方法,例如:
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class TestFrame extends JFrame {
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
pack();
setVisible(true);
}
private void init() {
final JPopupMenu menu = new JPopupMenu("test");
menu.add(new JMenuItem("1"));
final JList<String> list = new JList<String>(new String[]{"1","2","3"});
list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int selectedIndex = list.getSelectedIndex();
if(selectedIndex != -1){
Point indexToLocation = list.indexToLocation(selectedIndex);
Rectangle cellBounds = list.getCellBounds(selectedIndex, selectedIndex);
menu.show(list, indexToLocation.x, indexToLocation.y+cellBounds.height);
}
}
});
add(list);
}
public static void main(String... strings) {
new TestFrame();
}
}