Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Oct 29, 1998

Creating Hidden Buttons with Swing

Using Swing, you can easily create hidden buttons similar to the Back button used in Netscape Communicator 4. Hidden buttons look like normal graphic elements on the screen until you float over them with the mouse. This code was tested using JDK1.1.6 and Swing 1.0.3 under Win95:
 
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;

public class SwingHiddenButton01 extends JFrame {

	public static void main(String args[]) {
		SwingHiddenButton01 demoFrame = 
				new SwingHiddenButton01();
	}//end main()
	//-----------------------------------------------------//
  
	SwingHiddenButton01() {//constructor
		setTitle("Hidden Buttons");
		getContentPane().setLayout(new FlowLayout());

		//Create two hidden buttons with no action listeners
		JButton myJButton = new JButton("myButton");
		myJButton.setBorderPainted(false);
		myJButton.addMouseListener(new MyMouseListener());
		getContentPane().add(myJButton);
    
		JButton urJButton = new JButton("urButton");
		urJButton.setBorderPainted(false);
		urJButton.addMouseListener(new MyMouseListener());
		getContentPane().add(urJButton);
 
		//Set to windows look & feel    
		String plafClassName = 
			 "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
		try{
			UIManager.setLookAndFeel(plafClassName);
		}catch(Exception ex){System.out.println(ex);}
		SwingUtilities.updateComponentTreeUI(this);

		setSize(300,100);
		setVisible(true);
    
		// Inner class WindowAdapter to terminate the
		// program when the JFrame is closed.
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);}});//end WindowListener
    
	}//end constructor
	//=====================================================//

	//Inner mouse listener class to show and hide buttons 
	class MyMouseListener extends MouseAdapter{
		public void mouseEntered(MouseEvent e){
			//Display border on the button
			((JButton)e.getSource()).setBorderPainted(true);
		}//end mouseEntered()

		public void mouseExited(MouseEvent e){
			//Hide border on the button
			((JButton)e.getSource()).setBorderPainted(false);
		}//end mouseEntered()
	}//end inner class myMouseListener
     
}//end class SwingHiddenButton01
Richard G.
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date