Solved

gui clean up

Posted on 2004-09-11
5
187 Views
Last Modified: 2010-03-31
Can anyone tell me how to place my gui components on this win form in some sort of order?


package com.softwareConstruction;

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.*;

public class AssociateApplet extends Applet
   implements ActionListener {
   
    //Variable declarations
    private Frame win;
    private TextField mapInputText, domainInputText;
    private Label domainContent, mapContent;
    private static final String submitInput = "Submit input";
    private static final String bye  = " Exit ";
    private static final String addDomainValue  = " Add Value to Domain ";    
    private HashSet hashSet = new HashSet();
    private HashMap hashMap = new HashMap();
   
   public void actionPerformed ( ActionEvent e) {
      String cmd = e.getActionCommand();
      if ( cmd.equals( submitInput ) ) {
         
          //Use the associate class to add the object and key to the map
      }
      else if ( cmd.equals( addDomainValue ) ) {
          //Add the value to the domain
         
       }
      else if ( cmd.equals( bye ) ) {
         win.setVisible(false);
      }
   }

   public void init() {

      win = new Frame ( "Associative Application" );
     
      //Define a new TextField to handle the map value input
      this.mapInputText = new TextField("", 20);
      this.domainInputText = new TextField("", 20);
     
      //Add the text Field
      win.add(mapInputText);
      win.add(domainInputText);
     
      //Create a new window
      win.setLayout ( new FlowLayout() );
     
      //Add the submitInput and exit buttons to the window
      addButton ( submitInput ); addButton ( bye ); addButton(addDomainValue);
     
      //Add the label for the domain content
      domainContent = new Label( "Domain Content" );
      domainContent.setFont (new Font ( "Dialog", Font.BOLD, 14 ) );
      domainContent.setBackground ( Color.green );
      domainContent.setAlignment ( Label.CENTER );
     
      //Add the window that will show us what is in the domain
      win.add ( domainContent );
     
     
      //Add the label for the domain content
      mapContent = new Label( "Map Content" );
      mapContent.setFont (new Font ( "Dialog", Font.BOLD, 14 ) );
      mapContent.setBackground ( Color.green );
      mapContent.setAlignment ( Label.CENTER );
     
      //Add the window that will show us what is in the domain
      win.add ( mapContent );
     
     
      win.setSize ( 600, 500 );
   }

   public void start() {
         win.setVisible(true);
   }

   public void stop() {
         win.setVisible(false);
   }

   private void addButton ( String name ) {
      Button b = new Button ( name );
      b.setFont (new Font ( "Helvetica", Font.BOLD, 14 ) );
      b.addActionListener ( this );
      win.add ( b );
   }

   private void display(){
       
       
   }

}

0
Comment
Question by:jayrod
5 Comments
 
LVL 3

Assisted Solution

by:aviadbd
aviadbd earned 100 total points
ID: 12033707

The order of your gui components is determined by your LayoutManager. I see you're using the FlowLayout, so just read its docs here: http://java.sun.com/j2se/1.4.2/docs/api/index.html

You can also use a visual editor to place the gui components on your layout. If you use Eclipse (www.eclipse.org) you could use the VE plugin: http://www.eclipse.org/vep/

Good luck!
AviadBD.
0
 
LVL 8

Expert Comment

by:pjcrooks2000
ID: 12034004
Yep Layout managers check them out from the Sun site as aviadbd says :)

pjcrooks2000
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 400 total points
ID: 12035084
Dear jayrod,

Mmmm....so if I am not mistaken, you want to have some mechanism where you can place your GUI nicely. To do that, as aviadbd and pjcrooks2000 said, yes, you need to use Layout Manager. Let me explain you the concept.

Your win form (the applet that you have created) is a container where it can be divided freely by us. Well, the first easy and default LayoutManager is FlowLayout manager which is actually you used on your applet. LayoutManager has the use to organize your GUI components in the container (either your win form or Panel...we talked Panel later on). If you do not specify any layout manager then it takes FlowLayout as the default layout manager. FlowLayout will alwasy place the components (such as Button, TextField or etc) from the left to the right.

There are many types of LayoutManager such as GridLayout (row and column), BorderLayout (north, east, west and etc) and etc. Now, to create a sophisticated GUI arrangements, you need to have Panel which behaves exactly the same as your container (win form). Each Panel can have its own LayoutManager, it also can store the components such as buttons and etc. Now, what you need to do is to apply your win form with the LayoutManager and place each Panel (contains the GUI components) to your win form.

In below example, I show you how to use BorderLayout to modify your applet. Some components are placed on North, West and South of your win form.

/* Start here */
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.*;

public class MeowTest extends Applet
   implements ActionListener {
   
    //Variable declarations
    private Frame win;
    private Panel panel;
    private Panel panel2;
    private TextField mapInputText, domainInputText;
    private Label domainContent, mapContent;
    private static final String submitInput = "Submit input";
    private static final String bye  = " Exit ";
    private static final String addDomainValue  = " Add Value to Domain ";    
    private HashSet hashSet = new HashSet();
    private HashMap hashMap = new HashMap();
   
   public void actionPerformed ( ActionEvent e) {
      String cmd = e.getActionCommand();
      if ( cmd.equals( submitInput ) ) {
         
          //Use the associate class to add the object and key to the map
      }
      else if ( cmd.equals( addDomainValue ) ) {
          //Add the value to the domain
         
       }
      else if ( cmd.equals( bye ) ) {
         win.setVisible(false);
      }
   }

   public void init() {

      win = new Frame ( "Associative Application" );
      panel = new Panel();
      panel2 = new Panel();

      //Define a new TextField to handle the map value input
      this.mapInputText = new TextField("", 20);
      this.domainInputText = new TextField("", 20);
     
      //Add the text Field
      //win.add(mapInputText);
      //win.add(domainInputText);

      panel2.add (domainInputText);
      panel2.add (mapInputText);

      //Create a new window

      win.setLayout ( new BorderLayout() );
     
      //Add the submitInput and exit buttons to the window
      addButton ( submitInput );
      addButton ( bye );
      addButton(addDomainValue);
     
      win.add (panel, BorderLayout.WEST);
      win.add (panel2, BorderLayout.CENTER);
      //Add the label for the domain content
      domainContent = new Label( "Domain Content" );
      domainContent.setFont (new Font ( "Dialog", Font.BOLD, 14 ) );
      domainContent.setBackground ( Color.green );
      domainContent.setAlignment ( Label.CENTER );
     
      //Add the window that will show us what is in the domain
      win.add ( domainContent , BorderLayout.NORTH);
     
     
      //Add the label for the domain content
      mapContent = new Label( "Map Content" );
      mapContent.setFont (new Font ( "Dialog", Font.BOLD, 14 ) );
      mapContent.setBackground ( Color.green );
      mapContent.setAlignment ( Label.CENTER );
     
      //Add the window that will show us what is in the domain
      win.add ( mapContent , BorderLayout.SOUTH);
     
     
      win.setSize ( 600, 500 );
   }

   public void start() {
         win.setVisible(true);
   }

   public void stop() {
         win.setVisible(false);
   }

   private void addButton ( String name ) {
      Button b = new Button ( name );
      b.setFont (new Font ( "Helvetica", Font.BOLD, 14 ) );
      b.addActionListener ( this );
      panel.add ( b );
   }

   private void display(){
       
       
   }

}

/* End here */
0
 
LVL 3

Author Comment

by:jayrod
ID: 12036818
I've downloaded the vep and i'm using eclipse 3.0 and I can't get the stinking thing to work for me... Once I have it installed.. how do I know it's working? VEP that is?
0
 
LVL 3

Expert Comment

by:aviadbd
ID: 12037082

You should have a new perspective called VE, and you should have new objects under the "New" window (Where you have "Class", "Interface", "Project" etc, you should have GUI items now).

If you don't have those, you probably didn't install it right.

AviadBD.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now