Author
Custom Button Problem
Kapil Keskar
Ranch Hand
Joined: Dec 04, 2001
Posts: 47
Hello Guys, I am doing one custom button, which when pressed, a popup menu will be generated.Here Is the code which i have tried. But still got one error of -show()- method which i am unable to understand. Can anybody tell me this error, Please? import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import javax.swing.plaf.basic.ComboPopup ; public class CustomButton extends JButton { public CustomButton(String str) { super(str); init(); } JButton jb; public void init() { //jp=new JPanel (); new JButton (); this.setBackground(Color.cyan); this.setForeground(Color.black); //this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); addActionListener(new Action()); /*this.addMouseListener(new MouseAdapter (){ public void mouseClicked(MouseEvent evt) { if(evt.isPopupTrigger()) popup.show(evt.getComponent(),evt.getX(),evt.getY()); }});*/ //jp.add(jb); } public static void main(String ar[]) { //CustomButton cb=new CustomButton(); JFrame f = new JFrame (); Container c = f.getContentPane(); c.setLayout(new FlowLayout ()); c.add(new JLabel ("Button :")); JButton jb = new CustomButton("Example") ; c.add(jb); f.setSize(300,400); f.setVisible(true); f.show(); } class Action implements ActionListener , ComboPopup , MouseListener , MouseMotionListener , KeyListener { JPopupMenu popup; protected JButton button; public Action() { popup=new JPopupMenu (); JMenuItem mi1=new JMenuItem ("One"); JMenuItem mi2=new JMenuItem ("Two"); popup.add(mi1); popup.add(mi2); } public void actionPerformed(ActionEvent evt) { System.out.println("Clicked"); //popup.show(evt.getComponent(),evt.getX(),evt.getY()); popup.show(this,27,27); } public void show() { try { // if setSelectedItem() was called with a valid date, adjust the calendar //calendar.setTime( dateFormat.parse( comboBox.getSelectedItem().toString() ) ); } catch (Exception e) {} //updatePopup(); } public void hide() { popup.setVisible(false); } protected JList list = new JList (); public JList getList() { return list; } public MouseListener getMouseListener() { return this; } public MouseMotionListener getMouseMotionListener() { return this; } public KeyListener getKeyListener() { return this; } public boolean isVisible() { return popup.isVisible(); } public void uninstallingUI() { //popup.removePopupMenuListener(this); } // // end ComboPopup method implementations //====================================== public void mousePressed( MouseEvent e ) {} public void mouseReleased( MouseEvent e ) {} // something else registered for MousePressed public void mouseClicked(MouseEvent e) { /*if ( !SwingUtilities.isLeftMouseButton(e) ) return;*/ /*if ( !comboBox.isEnabled() ) return; if ( comboBox.isEditable() ) { //comboBox.getEditor().getEditorComponent().requestFocus(); } else { //comboBox.requestFocus(); } //togglePopup();*/ } protected boolean mouseInside = false; public void mouseEntered(MouseEvent e) { mouseInside = true; } public void mouseExited(MouseEvent e) { mouseInside = false; } // MouseMotionListener public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} // KeyListener public void keyPressed(KeyEvent e) {} public void keyTyped(KeyEvent e) {} public void keyReleased( KeyEvent e ) { if ( e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER ) { //togglePopup(); } }} }
Manfred Leonhardt
Ranch Hand
Joined: Jan 09, 2001
Posts: 1492
posted Jul 29, 2002 06:31:00
0
Hi Kapil, Try the following code to see how it can be done. Regards, Manfred.
subject: Custom Button Problem