Question

I want to create popup menu exactly the same as in eclipse on Ctrl+F6.

It should have JScrollBar and list of Strings where each item has small border.

I have idea to use JDialog but how to make it not display border and close buttons but only JList with scroll bar?

Thank you!

Was it helpful?

Solution

The easiest and most straightforward way is to use the JPopupMenu class. You can add any JComponent to a JPopupMenu, not just JMenuItems.

Here's a button which when clicked, shows a JList in a popup without any close buttons, just with a scroll bar. Wherever the popup (the JList) loses focus, the popup will be automatically closed.

final JButton b = new JButton("Press me");
b.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        final DefaultListModel<String> model = new DefaultListModel<>();
        for (int i = 0; i < 100; i++)
            model.addElement(i + ".");
        final JList<String> l = new JList<>(model);

        final JPopupMenu pm = new JPopupMenu();
        final JScrollPane sp = new JScrollPane(l);
        // pm.setPreferredSize(new Dimension(100, 300));
        pm.add(sp);
        pm.show(b, 0, 0);
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top