The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > Java


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 06-17-2007, 05:13 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Tutorial: Adding Components To Your GUI

Hello again with my Java tutorial~! **as you see I'm always bored..**
In this tutorial, I'll teach you on how to add more components to your GUI. Not only frame as you learned before. If you haven't see my previous post about your First GUI, go to this LINK

Let's write some codes (explanation on comments):

PHP Code:
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JEditorPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Color;

import java.io.IOException;

public class Browser extends JFrame implements HyperlinkListener
ActionListener{
    
private static final long serialVersionUID 1;
    
private JTextField urlField;
    
private JEditorPane htmlPane;
    
private String initialURL;

    
public Browser(String initialURL) {
        
super("My Web Browser");
        
this.initialURL initialURL;
        
// add a listener to the window for closing init
        
setDefaultCloseOperation(EXIT_ON_CLOSE);
        
// add a top panel
        
JPanel topPanel = new JPanel();
        
topPanel.setBackground(Color.green);
        
// create a Jlabel and JTextField for gettingURL
        
JLabel urlLabel = new JLabel("URL:");
        
urlField = new JTextField(30);
        
urlField.setText(initialURL);
        
// add a listener to JTextField
        
urlField.addActionListener(this);
        
        
// add url label and field to toppanel
        
topPanel.add(urlLabel);
        
topPanel.add(urlField);
        
        
//add top panel at the top and html text at the center
        
getContentPane().add(topPanelBorderLayout.NORTH);
        
try {
                
htmlPane = new JEditorPane(initialURL);
                
htmlPane.setEditable(false);
                
htmlPane.addHyperlinkListener(this);
                
JScrollPane scrollPane = new JScrollPane(htmlPane);
                
getContentPane().add(scrollPaneBorderLayout.CENTER);
            } 
catch(IOException ioe) {
               
System.out.println("Can't build HTML pane for " initialURL);
            }
            
Dimension screenSize getToolkit().getScreenSize();
            
int width screenSize.width 10;
            
int height screenSize.height 10;
            
setBounds(width/8height/8widthheight);
            
setVisible(true);
          }
          
// event handling for changing the URL
          
public void actionPerformed(ActionEvent event) {
            
String url;
            
url urlField.getText();
            
try {
              
htmlPane.setPage(new URL(url));
              
urlField.setText(url);
            } 
catch(IOException ioe) {
              
System.out.println("Can't follow link to " url );
            }
          }
          
// event handling when you click on a link
          
public void hyperlinkUpdate(HyperlinkEvent event) {
            if (
event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
              
try {
                
htmlPane.setPage(event.getURL());
                
urlField.setText(event.getURL().toExternalForm());
              } 
catch(IOException ioe) {
                
System.out.println("Can't follow link to " 
                         
event.getURL().toExternalForm() );
              }
            }
          }
            
public static void main(String[] args) {
                
// take the initial web page from the command line argument
                
if (args.length == 0)
                  new 
Browser("http://www.programmerstalk.net/");
                else
                  new 
Browser(args[0]);
            }
 } 
Guess what!!! You're now creating your own simple web browser with Java!

Follow this LINK to see my tutorial about Action Listener if you want review about action Listener. In this code, I'm using the other way of using Action Listener. It's basically the same thing, you can change it either way

** Tested and worked with JVM 1.6 ** but, rendered crappy lol..

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 06-17-2007 at 05:20 PM.
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 06:37 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50