1

I made little application and I need help for one I think easy thing but am stack atm with it. There are 2 Classes with JFrames . One is login and other I GUI and program that does something.Login is connected with Database and all how can I take value from textField where is username and when it passes verification to show up in other GUI class like USER : (name which u first wrote in login frame).

I tried with set get method to and add value to that string for example

String s = textField.getText().toString();

But when I call get method in GUI nothin happens.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2467769
  • 13
  • 1
  • 3
  • 3
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) *"One is login and.."* That should be a modal dialog or a `JOptionPane`. The rest of the question will be solved then. – Andrew Thompson Jun 09 '13 at 07:20

3 Answers3

1

try this one to pass data:

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

class PassData extends JFrame
{
    JTextField text;
    PassData(){
        JLabel l=new JLabel("Name: ");
        text=new JTextField(20);
        JButton b=new JButton("Send");
        setLayout(null);
        l.setBounds(10,10,100,20);
        text.setBounds(120,10,150,20);
        b.setBounds(120,40,80,20);
        add(l);
        add(text);
        add(b);
        setVisible(true);
        setSize(300,100);
          b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            String value=text.getText();
            NextPage page=new NextPage(value);
            page.setVisible(true);
            }
        });
    }
        public static void main(String[] args) 
    {
        new PassData();
    }
}

try this code for next page:

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

class NextPage extends JFrame
{
  NextPage(String st)
   {
     setLayout(null);
     setDefaultCloseOperation(javax.swing. WindowConstants.DISPOSE_ON_CLOSE);
     setTitle("Welcome");
     JLabel lab=new JLabel("Welcome  "+st);
     lab.setBounds(10,10,500,20);
     add(lab);
     setSize(300, 100);
      }
 }
Roshan
  • 645
  • 1
  • 11
  • 36
0

I assume you are taken to another screen when you are logged in. In that case, the value of textField is empty. So, textField.getText() will be empty. You have already read the text from login screen for validation. So, why read it again. Just pass it on to the next GUI and render it as you wish.

Suganthi
  • 417
  • 3
  • 14
0

make two classes of GUI extend / inherit eachother. like for example

class A 
{
  private username;
  private password;

public void A(username,password)
{

}
}

class B extends A
{
  private A(username,password);

public setA(username,password){}

public void verify()
{
  // send this response to GUI   
}

}

you can get values from textfield by textField.getText() and onclick button pass value from class A to class B and use classB to verify and Display output via 2nd GUI.

Asd
  • 103
  • 2
  • 12