• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Custom Button Problem

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}}
}

 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kapil,
Try the following code to see how it can be done.

Regards,
Manfred.
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic