• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

My own AbstractButton

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class InvisibleButton extends JButton implements ActionListener {
String text;
Font inactiveFont;
Font activeFont;
Border activeBorder;
Border inactiveBorder;


public InvisibleButton(String Text, Color Background) {
super.setText(Text);
super.setBorder(new EmptyBorder(0, 0, 0, 0));

setOpaque(false);

if (Background.getRGB() == -1118482) {
Background = Color.white;

}
this.setBackground(Background);
cont();
}

public InvisibleButton(String Text, String ToolTipText, Color Background) {
this.setText(Text);
this.setToolTipText(ToolTipText);
this.setBorder(new EmptyBorder(0, 0, 0, 0));
setOpaque(false);

if (Background.getRGB() == -1118482) {
Background = Color.white;

}
this.setBackground(Background);
cont();
}

public void cont() {
this.addActionListener(this);

System.out.println();

inactiveFont = new Font(getFont().getFontName(), getFont().getStyle(),
getFont().getSize()
);
activeFont = new Font(getFont().getFontName(), Font.BOLD | Font.ITALIC,
getFont().getSize());
activeBorder = new EmptyBorder(0, 0, 2, 0);

inactiveBorder = new EmptyBorder(0, 0, 0, 0);
}

protected void processMouseEvent(MouseEvent evt) {
switch (evt.getID()) {
case MouseEvent.MOUSE_ENTERED:

this.setBorder(activeBorder);
this.setFont(activeFont);
this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
break;
case MouseEvent.MOUSE_EXITED:

this.setBorder(inactiveBorder);
this.setFont(inactiveFont);
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
break;

}

}

public void actionPerformed(ActionEvent e) {
System.exit(0);
}

public static void main(String a[]) {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER));
JFrame frame = new JFrame();
p.setBackground(new Color(232, 222, 122));
System.out.println(p.getBackground().getRGB());
InvisibleButton b = new InvisibleButton("Do me a favour and press me",
p.getBackground());

frame.setSize(dim.width / 3, dim.height / 3);
frame.setLocation(dim.width / 3, dim.height / 3);

frame.setResizable(true);

p.add(b);
frame.getContentPane().add(p);
frame.getContentPane().add(p);

//p.setBackground(Color.black);
frame.setVisible(true);

}

}

This is my attempt to create a custom Button
When i create objects of this class(InvisibleButton )
it does evrything it's supposed to except handle events.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
protected void processMouseEvent(MouseEvent evt) {
super.processMouseEvent(evt);//<---------------------
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!