Results 1 to 4 of 4
  1. #1
    romina is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default I'm trying to add a separate dialog to a Java program...

    I'm trying to add a separate dialog/window to a Java program that collects the user's information from three different text boxes.

    I have looked at the sun tutorial, swing textbooks, and a graphic java text, but I'm still confused.

    How do I layout the three textboxes and the four related labels in the dialog? And how do I get this information from the dialog/window and send it back to the main frame in a single string (e.g. textbox1+textbox2+textbox3)?
    Thanks

  2. #2
    trill is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    I recommend you start off with only one textbox and a label. After you complete it, experiment a bit with more labels and boxes..

    Here is how you would add a JTextfield and a JLabel. Note you will still need to create a main class:
    Java Code:
    import java.awt.*;
    import java.awt.event;
    import javax.swing.*;
    
    public class YouClassNameHere extends JPanel{
        
        private JLabel YourInputLabel;
        private JTextfield YourTextField;
        public YourClassNameHere(){
           
            YourInputLabel = new JLabel("You write something here");
            YourTextField = new JTextField(#); //where # is any number. Gives a size to the TextField.
            YourTextField.addActionListener(new TempListener());//this will tell the program that you want to interact with it.
            
            add (YourInputLabel);
            add(YourTextField);
            setPreferredSize(new Dimension(300,75));
            setBackground (Color.yellow);
         }
    //Then just add a TempListener class. Inside this class, you might want
    //to define some variables, and perform some operations.
    }
    As far as joining the inputs, you can do it this way
    Java Code:
    String String1 = new String("Hello");
    String String2 = new String("World!");
    String String3 = new String(String1 + " " + String2); //Creates a new String with the values of String1 and String2
    System.out.println(String3);
    Greetings.

  3. #3
    hellogayathri is offline Member
    Join Date
    Jul 2015
    Posts
    3
    Rep Power
    0

    Default Re: I'm trying to add a separate dialog to a Java program...

    Hi,
    I suggest you to try this example of login action...with user name and password as jlabel and jtextfields to enter the data.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class SwingControlDemo {

    private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;

    public SwingControlDemo(){
    prepareGUI();
    }

    public static void main(String[] args){
    SwingControlDemo swingControlDemo = new SwingControlDemo();
    swingControlDemo.showTextFieldDemo();
    }

    private void prepareGUI(){
    mainFrame = new JFrame("Java Swing Examples");
    mainFrame.setSize(400,400);
    mainFrame.setLayout(new GridLayout(3, 1));
    mainFrame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent windowEvent){
    System.exit(0);
    }
    });
    headerLabel = new JLabel("", JLabel.CENTER);
    statusLabel = new JLabel("",JLabel.CENTER);

    statusLabel.setSize(350,100);

    controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());

    mainFrame.add(headerLabel);
    mainFrame.add(controlPanel);
    mainFrame.add(statusLabel);
    mainFrame.setVisible(true);
    }

    private void showTextFieldDemo(){
    headerLabel.setText("Control in action: JTextField");

    JLabel namelabel= new JLabel("User ID: ", JLabel.RIGHT);
    JLabel passwordLabel = new JLabel("Password: ", JLabel.CENTER);
    final JTextField userText = new JTextField(6);
    final JPasswordField passwordText = new JPasswordField(6);

    JButton loginButton = new JButton("Login");
    loginButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String data = "Username " + userText.getText();
    data += ", Password: "
    + new String(passwordText.getPassword());
    statusLabel.setText(data);
    }
    });

    controlPanel.add(namelabel);
    controlPanel.add(userText);
    controlPanel.add(passwordLabel);
    controlPanel.add(passwordText);
    controlPanel.add(loginButton);
    mainFrame.setVisible(true);
    }
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,887
    Rep Power
    20

    Default Re: I'm trying to add a separate dialog to a Java program...

    Quote Originally Posted by hellogayathri View Post
    Hi,
    I suggest you to try ...
    And I suggest you look at the date of a question. This one was asked 8 years ago.

    Also, people come here to learn. Posting a possible solution without a word of explanation hinders the learning progress. Don't do it again.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Hash table with separate chaining
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:42 PM
  2. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  3. Dialog typeface outside Java
    By Dreen in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 08:35 AM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  5. Java Print Dialog Framework 1.7
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-19-2007, 05:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •