aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes My own AbstractButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "My own AbstractButton" Watch "My own AbstractButton" New topic
Author

My own AbstractButton

luis Ferreira
Greenhorn

Joined: Sep 08, 2006
Posts: 1
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.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
protected void processMouseEvent(MouseEvent evt) {
super.processMouseEvent(evt);//<---------------------
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: My own AbstractButton