Home Tutorial Java Swing Java Swing Create LinkButton

 
 

Share on Google+Share on Google+
Java Swing Create LinkButton
Posted on: July 14, 2010 at 12:00 AM
Advertisement
In this section, you will learn how to create a link button in java swing.

Java Swing Create LinkButton

You all are aware of JButtons, JRadioButtons, JToggleButtons and you have used these components in your applications. Here we are going to create a Link Button that will allow you to move to another page very easily. For this, we have used html code to display the text in a standard way.

Here is the code:

import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;

public class LinkButton {
	public static void main(String[] args) throws Exception {
		final URI uri = new URI("http://www.roseindia.net");
		JFrame frame = new JFrame("Link");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300, 100);
		Container container = frame.getContentPane();
		container.setLayout(new GridBagLayout());

		JButton button = new JButton();
		button.setText("www.roseindia.net");
		button.setHorizontalAlignment(SwingConstants.LEFT);
		button.setBorderPainted(false);
		button.setOpaque(false);
		button.setBackground(Color.lightGray);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (Desktop.isDesktopSupported()) {
					Desktop desktop = Desktop.getDesktop();
					try {
						desktop.browse(uri);
					} catch (Exception ex) {
					}
				} else {
				}
			}
		});
		container.add(button);
		frame.setVisible(true);
	}
}

Output:

Advertisement

Related Tags for Java Swing Create LinkButton:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 14, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com