Boas
Necessito de criar um botão que ao clicar apareça um popmenu com uma scrollbar a controlar, consegui isso com o auxilio de uma jscrollpane.
Mas estou com problemas a livrar-me do popup, pois gostaria que ele desaparecesse caso se clicasse fora do mesmo.
Também não estou conseguindo fazer aparecer uma cor diferente sempre que o rato estiver sobre um menuitem do popmenu.
Em baixo está o meu código.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JPanel {
JPopupMenu popmenu = new JPopupMenu();
public test() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(800, 600));
JButton bbb= new JButton("PRESS TO POP-UP");
bbb.addMouseListener(new MouseListPop());
panel.add(bbb);
this.add(panel);
}
private class MouseListPop implements MouseListener {
public MouseListPop() {
}
@Override
public void mouseClicked(MouseEvent evt) {
popmenu.removeAll();
popmenu.setPopupSize(new Dimension(100,100));
JMenuItem menuA;
JPanel panel2 = new JPanel(new GridLayout(30,1));
for (int i=0; i < 30; i++){
menuA = new JMenuItem("item "+i);
panel2.add(menuA,"wrap");
}
JScrollPane jspane = new JScrollPane(panel2);
popmenu.add(jspane);
popmenu.setLocation(evt.getXOnScreen(),evt.getYOnScreen());
//PopMenuAlarms.show(evt.getComponent(),evt.getX(),evt.getY());
popmenu.setVisible(true);
//PopMenuAlarms.addMouseMotionListener(new mousemotionpopup());
}
@Override
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseEntered(MouseEvent e) {
//System.out.println("Enter");
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
//System.out.println("EXIT");
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("CustomComboBoxDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
test newContentPane = new test();
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
});
}
}
/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test {
JMenu menu3;
JPopupMenu popup;
private JMenuBar getMenuBar() {
JMenuBar menuBar = new JMenuBar();
menuBar.add(new JMenu("menu 1"));
menuBar.add(getMenu2());
menuBar.add(getMenu3());
return menuBar;
}
private JMenu getMenu2() {
JMenu menu = new JMenu("menu 2");
menu.getPopupMenu().setLayout(new GridLayout(0,9));
for(int j = 0; j < 100; j++)
menu.add(new JMenuItem("item " + (j+1)));
return menu;
}
private JMenu getMenu3() {
menu3 = new JMenu("menu 3");
popup = new JPopupMenu();
popup.setLayout(new BorderLayout());
popup.setPopupSize(500, 200);
popup.setInvoker(menu3);
menu3.addMouseListener(menuMouseListener);
popup.addFocusListener(popupFocusListener);
popup.addMouseListener(popupMouseListener);
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
popup.setVisible(false);
}
};
JPanel panel = new JPanel(new GridLayout(100,1));
for(int j = 0; j < 100; j++) {
JMenuItem item = new JMenuItem("item " + (j+1));
item.addActionListener(l);
panel.add(item);
}
JScrollPane scrollPane = new JScrollPane(panel);
popup.add(scrollPane);
return menu3;
}
private void showMenu() {
Rectangle r = menu3.getBounds();
Point p = new Point(r.x, r.y+r.height);
SwingUtilities.convertPointToScreen(p, menu3.getParent());
popup.setLocation(p.x, p.y);
popup.setVisible(true);
}
private MouseListener menuMouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(!popup.isVisible())
showMenu();
else
popup.setVisible(false);
}
public void mouseEntered(MouseEvent e) {
if(!popup.isVisible() && menu3.isSelected())
showMenu();
}
public void mouseExited(MouseEvent e) {
if(popup.isVisible() && !popup.contains(e.getPoint())) {
popup.setVisible(false);
}
}
};
private MouseListener popupMouseListener = new MouseAdapter() {
public void mouseExited(MouseEvent e) {
if(!menu3.isSelected())
popup.setVisible(false);
}
};
private FocusListener popupFocusListener = new FocusAdapter() {
public void focusLost(FocusEvent e) {
if(popup.isVisible() && !menu3.isSelected())
popup.setVisible(false);
}
};
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setJMenuBar(new test().getMenuBar());
f.setSize(200,100);
f.setLocation(200,200);
f.setVisible(true);
}
}
*/
Desde já obrigado pela ajuda prestada