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;

}

}

[2633 byte] By [hemanthjavaa] at [2007-11-26 18:38:46]
# 1

> 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.

So why don't you use a JCheckBoxMenuItem? And customize that?

the12huntersa at 2007-7-9 6:12:49 > top of Java-index,Desktop,Core GUI APIs...
# 2

You don't even to customise any components - it's no more than a case of lobbing an image into the UI defaults table:

UIManager.put("CheckBoxMenuItem.checkIcon", yourIcon);

That's assuming you want to do this for all JCheckBoxMenuItems in your app. If you want to do it just for specific ones then you can extend BasicCheckBoxMenuItemUI to use the icon of your choice, then install those UIs on the relevant items.

But, you don't need a pile of code for custom components.

itchyscratchya at 2007-7-9 6:12:49 > top of Java-index,Desktop,Core GUI APIs...
# 3

No actually Scollable PopupMenu does not work with JCheckBoxMenuItems as its got some flickering issues. So I am putting JButtons on a panel which is put on a JScrollapane on a pop up menu.

Thats why the use of JButtons. But they dont look like simple menu items and hence I need your help in customizing the button close to a JCheckBoxMenuItem

hemanthjavaa at 2007-7-9 6:12:49 > top of Java-index,Desktop,Core GUI APIs...
# 4
Scollable PopupMenu does not work with JCheckBoxMenuItems as its got some flickering issuesSounds odd. What is "ScrollablePopupMenu?" I suspect the bug lies in there rather than in JCheckBoxMenuItem.
itchyscratchya at 2007-7-9 6:12:49 > top of Java-index,Desktop,Core GUI APIs...
# 5
I have made a scrollable Pop up menu implementation which is not working fine. Can I send you the code. Can I have your email ifd sir
hemanthjavaa at 2007-7-9 6:12:49 > top of Java-index,Desktop,Core GUI APIs...
# 6
You can post it up here in code tags if you like.
itchyscratchya at 2007-7-9 6:12:49 > top of Java-index,Desktop,Core GUI APIs...
# 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.

hemanthjavaa at 2007-7-9 6:12:49 > top of Java-index,Desktop,Core GUI APIs...