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 / GUI / October 2005

Tip: Looking for answers? Try searching our database.

How to set textfield in panel with values entered by a user in dlgbox?

Thread view: 
brightoceanlight@hotmail.com - 12 Oct 2005 09:02 GMT
I have a textfield, textfield1 in a panel.  The user clicks on a button
that opens a dialog box and enters values into another textfield,
textfield2.  The user closes the dialog box.  How can I get the value
from textfield2 into textfield1?

Thank you,
Andrew Thompson - 12 Oct 2005 09:25 GMT
> I have a textfield, textfield1 in a panel.  The user clicks on a button
> that opens a dialog box and enters values into another textfield,
> textfield2.  The user closes the dialog box.  How can I get the value
> from textfield2 into textfield1?

Which bit are you having problems with?
onclick...

textfield1.setText(
  JOptionPane.showInputDialog(textfield1,
    "Type a string") );
Thomas Weidenfeller - 12 Oct 2005 09:26 GMT
> I have a textfield, textfield1 in a panel.  The user clicks on a button
> that opens a dialog box and enters values into another textfield,
> textfield2.  The user closes the dialog box.  How can I get the value
> from textfield2 into textfield1?

You read the text from textfield2 and place it in textfield1 (hint:
textfields have an API for getting and setting text, read the API
documentation). You do this in the event handler of the close button of
the dialog box.

Oh, and

http://java.sun.com/books/tutorial/uiswing/
http://www.physci.org/codes/sscce.jsp

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Michael Rauscher - 12 Oct 2005 09:39 GMT
brightoceanlight@hotmail.com schrieb:
> I have a textfield, textfield1 in a panel.  The user clicks on a button
> that opens a dialog box and enters values into another textfield,
> textfield2.  The user closes the dialog box.  How can I get the value
> from textfield2 into textfield1?

Quick and dirty example:

// File: Test.java

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

class MyDialog extends JDialog {
    private JTextField textField = new JTextField(50);

    public MyDialog( JFrame owner ) {
        super(owner, "MyDialog");
        setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
        getContentPane().add( textField );
        pack();
    }

    public String getText() { return textField.getText(); }

}

public class Test {
    private JTextField textField = new JTextField(50);
    private JButton button = new JButton("Open");
    private JFrame frame;
    private MyDialog dlg;

    public void createAndShowGUI() {
        frame = new JFrame("Test");

        dlg = new MyDialog(frame);

        dlg.addWindowListener( new WindowAdapter() {
            public void windowClosing( WindowEvent e ) {
                textField.setText(dlg.getText());
            }
        });

        button.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                dlg.setVisible(true);
            }
        });

        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().add( textField, BorderLayout.NORTH );
        frame.getContentPane().add( button, BorderLayout.SOUTH );
        frame.pack();
        frame.setVisible(true);
    }

    public static final void main( String args[] ) {
        Test app = new Test();
        app.createAndShowGUI();
    }
}

Bye
Michael
Roedy Green - 12 Oct 2005 09:41 GMT
>I have a textfield, textfield1 in a panel.  The user clicks on a button
>that opens a dialog box and enters values into another textfield,
>textfield2.  The user closes the dialog box.  How can I get the value
>from textfield2 into textfield1?

Here is a ColorChooser Dialog.

The trick in to instantiate the ColorChooser, then call query every
time you want to query the user about his colour choice.
The key to understanding this code is to know that setvisible( true )
stalls at that point until the user has made a selection and the
actionlistener calls dispose..

package com.mindprod.fontshowerawt;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* Simple ColorChooser for AWT use. Reusable Dialog. Construct, then
call query
* to trigger a pop-up and modal user interaction. Query will return
the new
* color selected. You can reuse the same ColorChooser Object by
calling query
* again.
*
* @author Roedy Green
*/
public class ColorChooser extends Dialog {

   /** decimal value for blue */
   private Choice blueDecimalChoice;

   /** hex value for blue */
   private Choice blueHexChoice;

   /** label for blue */
   private Label blueLabel;

   /** Cancel request to change colour, undoes selection */
   private Button cancel;

   /** true if choosing foreground colour */
   private boolean choosingForeground;

   /** colour swatch */
   private TextArea colorSwatch;

   /** "decimal" */
   private Label decimalLabel;

   /** dddd */
   private TextField decimalValue;

   /** decimal value for green */
   private Choice greenDecimalChoice;

   /** hex value for green */
   private Choice greenHexChoice;

   /** label for green */
   private Label greenLabel;

   /** "hex" */
   private Label hexLabel;

   /** #hhhh */
   private TextField hexValue;

   /** OK to select colour */
   private Button ok;

   /**
    * original color selection before we started meddling
    */
   private Color originalColor;

   /** hex value for red */
   private Choice redDecimalChoice;

   /** hex value for red */
   private Choice redHexChoice;

   /** label for red */
   private Label redLabel;

   /** color the user chose */
   private Color selectedColor;

   /**
    * change the colour of the display swatch to reflect the recent
color
    * change.
    */
   private void displayColorSwatch ()
       {

       redDecimalChoice.select( selectedColor.getRed() );
       greenDecimalChoice.select( selectedColor.getGreen() );
       blueDecimalChoice.select( selectedColor.getBlue() );
       redHexChoice.select( selectedColor.getRed() );
       greenHexChoice.select( selectedColor.getGreen() );
       blueHexChoice.select( selectedColor.getBlue() );
       int rgb = selectedColor.getRGB() & 0xffffff;
       decimalValue.setText( Integer.toString( rgb ) );
       // pad with lead zeros as necessary
       hexValue.setText( '#' + Integer.toHexString( rgb + 0x1000000 )
           .substring( 1 ) );
       if ( choosingForeground )
           {
           colorSwatch.setForeground( selectedColor );
           }
       else
           {
           colorSwatch.setBackground( selectedColor );
           }
       this.validate();
       colorSwatch.repaint();
       }

   /**
    * query user which color he wants.
    *
    * @param chooseForeground
    *        true if want to choose the forground colour, false if
the
    *        background.
    * @param originalBackground
    *        background before user changes it.
    * @param originalForeground
    *        foreground before user changes it.
    * @return new color selected. Will match original selection if
hits CANCEL.
    */
   public Color query ( boolean chooseForeground, Color
originalBackground,
       Color originalForeground )
       {
       this.choosingForeground = chooseForeground;

       if ( choosingForeground )
           {
           this.selectedColor = originalForeground;
           this.originalColor = originalForeground;
           setTitle( "Choose Foreground Color" );
           }
       else
           {
           this.selectedColor = originalBackground;
           this.originalColor = originalBackground;
           setTitle( "Choose Background Color" );
           }
       colorSwatch.setForeground( originalForeground );
       colorSwatch.setBackground( originalBackground );
       displayColorSwatch();

       // won't return from setVisible until entire dialog exchange
is complete
       // which many consist of many selections before hitting OK or
cancel.
       setVisible( true );
       return selectedColor;
       }

   /**
    * constructor
    *
    * @param owner
    *        enclosing frame, unfortuately Applet is a panel, not a
Frame.
    *        Chase parents to get the Frame.
    */
   public ColorChooser( Frame owner )
   {
   super( owner, "Choose Color", true /* modal */);

   decimalLabel = new Label( "Decimal", Label.LEFT );
   hexLabel = new Label( "Hex", Label.RIGHT );
   redDecimalChoice = new Choice();
   greenDecimalChoice = new Choice();
   blueDecimalChoice = new Choice();
   for ( int i = 0; i <= 255; i++ )
       {
       String decimal = Integer.toString( i );
       redDecimalChoice.addItem( decimal );
       greenDecimalChoice.addItem( decimal );
       blueDecimalChoice.addItem( decimal );
       }
   ItemListener theDecimalListener = new ItemListener() {

       public void itemStateChanged ( ItemEvent e )
           {
           int red = redDecimalChoice.getSelectedIndex();
           int green = greenDecimalChoice.getSelectedIndex();
           int blue = blueDecimalChoice.getSelectedIndex();
           selectedColor = new Color( red, green, blue );
           displayColorSwatch();
           }
   };

   redDecimalChoice.addItemListener( theDecimalListener );
   greenDecimalChoice.addItemListener( theDecimalListener );
   blueDecimalChoice.addItemListener( theDecimalListener );

   redLabel = new Label( "Red", Label.CENTER );
   redLabel.setForeground( Color.white );
   redLabel.setBackground( Color.red );

   greenLabel = new Label( "Green", Label.CENTER );
   greenLabel.setForeground( Color.white );
   greenLabel.setBackground( new Color( 0x009900 ) ); // darken green
a bit

   blueLabel = new Label( "Blue", Label.CENTER );
   blueLabel.setForeground( Color.white );
   blueLabel.setBackground( Color.blue );

   redHexChoice = new Choice();
   greenHexChoice = new Choice();
   blueHexChoice = new Choice();

   for ( int i = 0; i <= 0xff; i++ )
       {
       String hex = Integer.toHexString( i + 0x100 ).substring( 1 );
       redHexChoice.addItem( hex );
       greenHexChoice.addItem( hex );
       blueHexChoice.addItem( hex );
       }
   ItemListener theHexListener = new ItemListener() {

       public void itemStateChanged ( ItemEvent e )
           {
           int red = redHexChoice.getSelectedIndex();
           int green = greenHexChoice.getSelectedIndex();
           int blue = blueHexChoice.getSelectedIndex();
           selectedColor = new Color( red, green, blue );
           displayColorSwatch();
           }
   };
   redHexChoice.addItemListener( theHexListener );
   greenHexChoice.addItemListener( theHexListener );
   blueHexChoice.addItemListener( theHexListener );

   decimalValue = new TextField( "16777215" );
   decimalValue.setFont( new Font( "Dialog", Font.PLAIN, 15 ) );

   // no such things as right justify is AWT with TextField.
   hexValue = new TextField( "#ffffff" );
   hexValue.setFont( new Font( "Dialog", Font.PLAIN, 15 ) );

   colorSwatch = new TextArea( "The quick brown fox jumps\n"
       + "over the lazy dog's back.", 2, 25, TextArea.SCROLLBARS_NONE
);
   colorSwatch.setEditable( false );

   ok = new Button( "OK" );
   ok.addActionListener( new ActionListener() {

       public void actionPerformed ( ActionEvent e )
           {
           // selectedColor has our choice
           ColorChooser.this.dispose();
           }
   } );

   cancel = new Button( "Cancel" );
   cancel.addActionListener( new ActionListener() {

       public void actionPerformed ( ActionEvent e )
           {
           // backout to what we started with.
           selectedColor = originalColor;
           ColorChooser.this.dispose();
           }
   } );
   this.addWindowListener( new WindowAdapter() {

       /**
        * Handle request to close Dialog.
        *
        * @param e
        *        event giving details of closing.
        */
       public void windowClosing ( WindowEvent e )
           {
           // backout to what we started with.
           selectedColor = originalColor;
           ColorChooser.this.dispose();
           }
   } );

   this.setLayout( new GridBagLayout() );

   // ---0--1-2------3----
   // 0 decimal----- hex 0
   // 1 rd-- red---- rh -1
   // 2 gd-- green-- gh -2
   // 3 bd-- blue--- bh -3
   // 4 ddddd---- #xxxxx-4
   // 5 ---swatch------ -5
   // 6 ok -- -- -cancel-6

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( decimalLabel, new GridBagConstraints( 0, 0, 1, 1, 0.0,
0.0,
       GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(
10, 10,
           5, 5 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( hexLabel, new GridBagConstraints( 3, 0, 1, 1, 0.0, 0.0,
       GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(
10, 5, 5,
           10 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( redDecimalChoice, new GridBagConstraints( 0, 1, 1, 1,
0.0, 0.0,
       GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(
5, 10, 5,
           5 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( redLabel, new GridBagConstraints( 1, 1, 2, 1, 0.0, 0.0,
       GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new
Insets(
           5, 20, 5, 5 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( redHexChoice, new GridBagConstraints( 3, 1, 1, 1, 0.0,
0.0,
       GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(
5, 5, 5,
           10 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( greenDecimalChoice, new GridBagConstraints( 0, 2, 1, 1,
0.0, 0.0,
       GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(
5, 10, 5,
           5 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( greenLabel, new GridBagConstraints( 1, 2, 2, 1, 0.0,
0.0,
       GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new
Insets(
           5, 20, 5, 5 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( greenHexChoice, new GridBagConstraints( 3, 2, 1, 1, 0.0,
0.0,
       GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(
5, 5, 5,
           10 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( blueDecimalChoice, new GridBagConstraints( 0, 3, 1, 1,
0.0, 0.0,
       GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(
5, 10, 5,
           5 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( blueLabel, new GridBagConstraints( 1, 3, 2, 1, 0.0, 0.0,
       GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new
Insets(
           5, 20, 5, 5 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( blueHexChoice, new GridBagConstraints( 3, 3, 1, 1, 0.0,
0.0,
       GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(
5, 5, 5,
           10 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( decimalValue, new GridBagConstraints( 0, 4, 1, 1, 0.0,
0.0,
       GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(
5, 10, 5,
           5 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( hexValue, new GridBagConstraints( 3, 4, 1, 1, 0.0, 0.0,
       GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(
5, 5, 5,
           10 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( colorSwatch, new GridBagConstraints( 0, 5, 4, 1, 50.0,
50.0,
       GridBagConstraints.CENTER, GridBagConstraints.BOTH, new
Insets( 5, 10,
           5, 10 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( ok, new GridBagConstraints( 0, 6, 2, 1, 0.0, 0.0,
       GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(
5, 10,
           10, 5 ), 0, 0 ) );

   // x y w h wtx wty anchor fill T L B R padx pady
   this.add( cancel, new GridBagConstraints( 2, 6, 2, 1, 0.0, 0.0,
       GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(
5, 5, 10,
           10 ), 0, 0 ) );

   this.pack();
   } // end constructor
}
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 12 Oct 2005 09:55 GMT
>The trick in to instantiate the ColorChooser, then call query every
>time you want to query the user about his colour choice.
>The key to understanding this code is to know that setvisible( true )
>stalls at that point until the user has made a selection and the
>actionlistener calls dispose..

see http://mindprod.com/jgloss/dialog.html for yet another
explanation.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



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.