Custom JButton for JPopupMenu
I need to make a custom PopupMenu Button . This button needs to be added to the custom JPopupmenu . This custom button must have the look and feel of a typical menu item. Basically it should have a typical features of a checkboxmenuitem where I have an check.jpg for check and uncheck of the button. How do I reproduce the look and feel of a menuitem. What changes do i make in the below code. Basically no border, button press should not be there. It should not look like we are adding JButtons to the user
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.border.EmptyBorder;
publicclass XCheckedButton
extends JButton{
privateboolean flag;
private ImageIcon checkedIcon;
public XCheckedButton(){
super();
}
public XCheckedButton(Action a){
this();
setAction(a);
}
public XCheckedButton(Icon icon){
super(icon);
}
public XCheckedButton(String text, Icon icon){
super(text, icon);
}
public XCheckedButton(String text){
super(text);
}
public ImageIcon getCheckedIcon(){
return checkedIcon;
}
publicvoid setCheckedIcon(boolean state){
this.checkedIcon = checkedIcon;
}
}
# 7
Thanks a lot. Here are my 2 java files and right below is the probelm I am facing when USing JMenuItems. Thats the reason why i switched over to JButtons.
JframePopupMenu.java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class JFramePopupMenu extends JFrame {
private static final long serialVersionUID = 1;
private JPanel jContentPane = null;
private JButton jbnPopup = null;
private JTextField jtfNumOfMenus = null;
private JLabel lblNumElem = null;
JTextArea output;
JScrollPane scrollPane;
String newline = "\n";
ScrollablePopupMenu scrollablePopupMenu = new ScrollablePopupMenu(JFramePopupMenu.this.getGraphicsConfiguration());
private JButton getBtnPopup() {
if (jbnPopup == null) {
jbnPopup = new JButton();
jbnPopup.setText("View Popup");
jbnPopup.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int n = Integer.parseInt(getTxtNumElem().getText());
JCheckBoxMenuItem cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
cbMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println( e );
scrollablePopupMenu.hidemenu();
}
});
cbMenuItem.setMnemonic(KeyEvent.VK_C);
scrollablePopupMenu.add(cbMenuItem);
for (int i=0;i<n;i++){
JMenuItem xx = new JMenuItem(" JMenuItem " + (i+1));
xx.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println( e );
scrollablePopupMenu.hidemenu();
}
});
//scrollablePopupMenu.add(new JButton(" JMenuItem " + (i+1)));
scrollablePopupMenu.add(xx);
}
scrollablePopupMenu.show(jbnPopup, jbnPopup.getWidth()*3, 0);
}
});
}
return jbnPopup;
}
private JTextField getTxtNumElem() {
if (jtfNumOfMenus == null) {
jtfNumOfMenus = new JTextField();
jtfNumOfMenus.setColumns(3);
jtfNumOfMenus.setText("60");
}
return jtfNumOfMenus;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFramePopupMenu thisClass = new JFramePopupMenu();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public JFramePopupMenu() {
super();
initialize();
}
private void initialize() {
this.setSize(274, 109);
this.setContentPane(getJContentPane());
this.setTitle("Scrollable JPopupMenu");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
lblNumElem = new JLabel();
//lblNumElem.setText("N鷐ero de elementos del Men?);
FlowLayout flowLayout = new FlowLayout();
flowLayout.setHgap(8);
flowLayout.setVgap(8);
jContentPane = new JPanel();
jContentPane.setLayout(flowLayout);
jContentPane.add(getBtnPopup(), null);
jContentPane.add(lblNumElem, null);
jContentPane.add(getTxtNumElem(), null);
}
return jContentPane;
}
}
ScrollablePopupMenu.java
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
public class ScrollablePopupMenu extends JPopupMenu {
private static final long serialVersionUID = 1;
private JPanel panelMenus = null;
private JScrollPane scroll = null;
public ScrollablePopupMenu(GraphicsConfiguration gc) {
super();
scroll = new JScrollPane();
panelMenus = new JPanel();
panelMenus.setLayout(new GridLayout(0,1));
scroll.setViewportView(panelMenus);
scroll.setBorder(null);
scroll.setMaximumSize(new Dimension(scroll.getMaximumSize().width,
this.getToolkit().getScreenSize().height -
this.getToolkit().getScreenInsets(gc).top -
this.getToolkit().getScreenInsets(gc).bottom - 4));
super.add(scroll);
}
public void show(Component invoker, int x, int y) {
this.pack();
panelMenus.validate();
int maxsize = scroll.getMaximumSize().height;
int realsize = panelMenus.getPreferredSize().height;
int sizescroll = 0;
if (maxsize >< realsize) {
sizescroll = scroll.getVerticalScrollBar().getPreferredSize().width;
}
scroll.setPreferredSize(new Dimension(
scroll.getPreferredSize().width + sizescroll,
scroll.getPreferredSize().height));
this.setLocation( x, y);
this.setVisible(true);
}
public void hidemenu(){
if(this.isVisible()){
this.setVisible(false);
}
}
public JMenuItem add(JMenuItem menuItem) {
panelMenus.add(menuItem);
return menuItem;
}
public void addSeparator() {
panelMenus.add(new JSeparator());
}
}
Problem 1: Not able to Scroll down when frame is Large
My application is a large frame and when I invoke the JPopupMenu on it I am not able to scroll.
In the example application also I found the same problem when I maximize the frame. I am able to see the JPopupMenu but not able to scroll on it.