Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2005

Tip: Looking for answers? Try searching our database.

The String "Result" on a new line

Thread view: 
geeksatlarge - 12 Nov 2005 20:12 GMT
How would I get The string "Result", to display on a new line along
with its accompanying JTextField, given the following code?

TIA all,
geeksatlarge

/*Linear search of an array
*/

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

public class LinearSearch extends JApplet implements ActionListener
{
   JLabel enterLabel, resultLabel;
   JTextField enterField, resultField;
   int array[];

   // set up applet's GUI
   public void init()
   {
       // get content pane and set its layout to FlowLayout
       Container container = getContentPane();
       container.setLayout( new FlowLayout() );

       // set up JLabel and JTextField for user input
       enterLabel = new JLabel( "Enter integer search key" );
       container.add( enterLabel );

       enterField = new JTextField( 10 );
       container.add( enterField );

       // register this applet as enterField's action listener
       enterField.addActionListener( this );

       // set up JLabel and JTextField for displaying results
       resultLabel = new JLabel( "Result" );    <============= Hmmm!
       container.add( resultLabel );

       resultField = new JTextField( 20 );
       resultField.setEditable( false );
       container.add( resultField );

       // create array and populate with even integers from 0 to 198
       array = new int[ 100 ];

       for ( int counter = 0; counter < array.length; counter++ )
           array[ counter ] = 2 * counter;

   }   // end method init

   // search array for specified key value
   public int linearSearch( int array2[], int key )
   {
       // loop through array elements
       for ( int counter = 0; counter < array2.length; counter++ )

           // if array element equals key value, return location
           if ( array2[ counter ] == key )
               return counter;

       return -1;  // key not found

   }   // end method linearSearch

   // obtain user input and call method linearSearch
   public void actionPerformed( ActionEvent actionEvent )
   {
       // input can also be obtained with enterField.getText()
       String searchKey = actionEvent.getActionCommand();

       // pass array reference to linearSearch; normally a reference
to an
       // array is passed to a method to search corresponding array
object
       int element = linearSearch( array, Integer.parseInt( searchKey
) );

       // display search result
       if ( element != -1 )
           resultField.setText( "Found value in element " + element );
       else
           resultField.setText( "Value not found" );

   }   // end method actionPerformed
   
}   // end class LinearSearch
IchBin - 12 Nov 2005 21:58 GMT
> How would I get The string "Result", to display on a new line along
> with its accompanying JTextField, given the following code?
[quoted text clipped - 21 lines]
>         Container container = getContentPane();
>         container.setLayout( new FlowLayout() );

The quick and dirty ans is just to change
setLayout(new FlowLayout()) to setLayout(new GridLayout(2,2));

You may want to look at How to... for layouts
http://java.sun.com/docs/books/tutorial/uiswing/layout/layoutlist.html

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

geeksatlarge - 12 Nov 2005 23:08 GMT
IchBin,

Thanks a lot. I'll be browsing this link quite a bit as I get the Java
learning curve under control. It's a *great* resource.

Thanks again,

geeksatlarge
IchBin - 12 Nov 2005 23:25 GMT
> IchBin,
>
[quoted text clipped - 4 lines]
>
> geeksatlarge

There is one more layout that you may want to look at. It's at JGoodies
website http://www.jgoodies.com/freeware/index.html. Its called Forms.
It is free and much easier than sun's layout managers.

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

Roedy Green - 13 Nov 2005 02:40 GMT
>How would I get The string "Result", to display on a new line along
>with its accompanying JTextField, given the following code?

you need a layout. see http://mindprod.com/jgloss/layout.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

geeksatlarge - 13 Nov 2005 20:51 GMT
Hi Roedy,

Thanks for the tip again. I'm on the link you sent me right now. It's a
lot to grasp, but it puts me further ahead than I've been.

Thanks again,
geeksatlarge


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.