0

Hello there. Can anybody help me with this problem.

I am trying to write a program that creates instances of classes that extend JFrame to make my application windows....

This code runs but I can not reference the JTextField shown on the GUI - it seems that there is another instance of it created somehow that is used - to see what I mean run the attatched code, and click connect. The text in the userName text box is not displayed on System.out.....

Any ideas????

Thanks Matthew Deaves

Attachments
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;

public class DocumentClient
{
	//INPUT OUTPUT DECLARATIONS
	private static InetAddress host;
	private static final int PORT = 1234;
	private static Socket link;
	private static BufferedReader in;
	private static PrintWriter out;
	private static ObjectInputStream objIn;
	private static BufferedReader keyboard;
	
	//WINDOW DECLARATIONS
	private static loginFrame loginWindow;

	public static void main (String[]args) throws IOException
	{
		//CODE HERE TO SHOW LOGIN FORM
		loginWindow = new loginFrame();
	}
	
	public static boolean loginUser(String name)
	{
		System.out.println("\nUser Name: " + name);
		return true;
	}

	static class loginFrame extends JFrame
	{
		//LOGIN WINDOW ELEMENTS
		private static JTextField userName;
		private static JPasswordField password;
		private static JLabel name, pword;
		private static JButton connect, quit;
		private static JPanel buttonPanel, fieldPanel;
		private static Container pane;
		
		//CONSTRUCTOR
		public loginFrame()
		{
			//GET PANE TO DRAW TO
			pane = getContentPane();
			pane.setLayout(new BorderLayout());
			
			//SET UP BUTTONS AND BUTTON PANEL
			connect = new JButton("Connect");
			quit = new JButton("Quit");
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new FlowLayout());
			buttonPanel.add(connect);
			buttonPanel.add(quit);
				
			//SET UP ACTION LISTENER FOR BUTTONS
			myActionListener loginListener = new myActionListener();
			quit.addActionListener(loginListener);	
			connect.addActionListener(loginListener);
				
			//SET UP TEXTFIELDS AND PANEL
			password = new JPasswordField();
			userName = new JTextField();
			fieldPanel = new JPanel();
			fieldPanel.setLayout(new GridLayout(2,2));
				
			JLabel name = new JLabel("User Name:");
			fieldPanel.add(name);
			JTextField userName = new JTextField();
			fieldPanel.add(userName);
			JLabel pword = new JLabel("Password:");
			fieldPanel.add(pword);
			password = new JPasswordField();
			fieldPanel.add(password);
				
			pane.add(fieldPanel, BorderLayout.CENTER);
			pane.add(buttonPanel, BorderLayout.SOUTH);
			setSize(300,300);
			setVisible(true);
			
			//Set up windowAdapter
			myWindowAdapter loginAdapter = new myWindowAdapter();
			addWindowListener(loginAdapter);
											
		}
		
		private class myActionListener implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				//Login forms has its own listener
				if (e.getSource() == quit)
				{
					System.exit(0);
				}
				if (e.getSource() == connect)
				{
					//THIS DOES NOT THROW THE VALUE OF userName TO loginUser()??????
					loginUser(userName.getText());
					
				}
			}
		}
		
		private class myWindowAdapter extends WindowAdapter
		{
			public void windowClosing (WindowEvent e)
			{
				System.exit(0);
			}
		}
	}
}
2
Contributors
1
Reply
2
Views
13 Years
Discussion Span
Last Post by apcxpc
0

hmmm...

if (e.getSource() == connect){
//THIS DOES NOT THROW THE VALUE OF userName TO loginUser()??????
loginUser(userName.getText());
}

I don't have Java installed on this computer, so I can't run your program... but the static method you are calling returns a boolean value, so this ActionListener bit should be something like...

Boolean x = loginUser(userName.getText()); (to get said method to run, and to accept the returned boolean value)


if that doesn't work... then try

Boolean x = DocumentClient.loginUser(DocumentClient.userName.getText());

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.