getting two classes working together

hello.

this is james mcfadden. i am currently developing a video library system in Java. it involves developing a video library menu. the video library menu is supposed to consist of an administrator's section, a new user's section and an existing user's section. i am using JTabbedPane to navigate between the 3 sections of the menu. the following program compiles but it does not run the way i expect it to. when i run the program, the frame with the tabbed pane stuff is displayed on screen, but when i press any of the 2 Log On buttons the logon dialog box called "Welcome to Home Entertainment" doesn't display on screen. how can i solve this problem?

thanks.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

publicclass VideoLibrarySystemextends JPanelimplements ActionListener{

private JTabbedPane jtp =new JTabbedPane();

private JPanel cP1 =new JPanel();

private JPanel cP2 =new JPanel();

private JPanel cP3 =new JPanel();

private JPanel bP1 =new JPanel();

private JPanel bP2 =new JPanel();

private JPanel bP3 =new JPanel();

private JButton jbAdministratorLogOn =new JButton("Log On");

private JButton jbNewUserRegister =new JButton("Register");

private JButton jbExistingUserLogOn =new JButton("Log On");

public VideoLibrarySystem(){

cP1.setLayout(new BorderLayout());

cP2.setLayout(new BorderLayout());

cP3.setLayout(new BorderLayout());

bP1.setBorder(new TitledBorder("Make a choice"));

bP2.setBorder(new TitledBorder("Make a choice"));

bP3.setBorder(new TitledBorder("Make a choice"));

bP1.add(jbAdministratorLogOn);

bP2.add(jbNewUserRegister);

bP3.add(jbExistingUserLogOn);

cP1.add(bP1, BorderLayout.SOUTH);

cP2.add(bP2, BorderLayout.SOUTH);

cP3.add(bP3, BorderLayout.SOUTH);

jtp.addTab("Administrator", cP1);

jtp.addTab("New User", cP2);

jtp.addTab("Existing User", cP3);

JFrame jf =new JFrame("Video Library System");

jf.addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){

System.exit(0);

}

});

jf.getContentPane().add(jtp, BorderLayout.CENTER);

jf.setSize(500, 300);

jf.setVisible(true);

jbAdministratorLogOn.addActionListener(this);

jbNewUserRegister.addActionListener(this);

jbExistingUserLogOn.addActionListener(this);

}

publicvoid actionPerformed(ActionEvent e){

if(e.getSource() == jbAdministratorLogOn){

LogOn logon =new LogOn();

}

if(e.getSource() == jbNewUserRegister){

//RegistrationForm registration = new RegistrationForm();

//registration.getInput();

//registration.setVisible(true);

}

if(e.getSource() == jbExistingUserLogOn){

LogOn logon =new LogOn();

}

}

publicstaticvoid main(String[] args){

new VideoLibrarySystem();

}

}

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass LogOn{

publicstaticvoid main(String[] args){

new LogOnInfoDialog().setVisible(true);

}

}

class LogOnInfoDialogextends JDialog{

private JTextField jtfUN =new JTextField(20);

private JPasswordField jtfPW =new JPasswordField(20);

private JButton jbOK =new JButton("OK");

private JButton jbCancel =new JButton("Cancel");

private LogOn logOn =new LogOn();

class LogOn{

String username;

String password;

}

public LogOnInfoDialog(){

this(null,true);

}

public LogOnInfoDialog(java.awt.Frame parent,boolean child){

super(parent, child);

setTitle("Welcome to Home Entertainment");

// Group two labels

JPanel jp1 =new JPanel(new GridLayout(2, 1));

jp1.add(new JLabel("Username"));

jp1.add(new JLabel("Password"));

// Group two text fields

JPanel jp2 =new JPanel(new GridLayout(2, 1));

jp2.add(jtfUN);

jp2.add(jtfPW);

// Group jpLabels and jpTextFields

JPanel jp3 =new JPanel(new BorderLayout(5, 2));

jp3.add(jp1, BorderLayout.WEST);

jp3.add(jp2, BorderLayout.CENTER);

JPanel jp4 =new JPanel();

jp4.add(jbOK);

jp4.add(jbCancel);

getContentPane().add(jp4, BorderLayout.SOUTH);

getContentPane().add(jp3, BorderLayout.CENTER);

jbOK.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

logOn.username = jtfUN.getText().trim();

logOn.password =new String(jtfPW.getPassword());

setVisible(false);

}

});

jbCancel.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

logOn =null;

setVisible(false);

}

});

}

}

[9863 byte] By [james-mcfaddena] at [2007-11-27 9:00:08]
# 1

Try changing

LogOn logon = new LogOn();

to

LogOn logon = new LogOn();

logon.show();

as I did not notice any where in that code where you were actually showing the Dialog. You may wish to add a pack() to the end of the LogOn constructor, also.

Edit: Also, your first class is poorly designed (possibly in multiple ways, but only one that I am going to mention now) in that it extends JPanel, but then, in its constructor creates and show a JFrame containing itself. If you must do this (which you don't, you could have left the JFrame in the main and added a add(jtp) to the end of the constructor) then the class should, at least extend JFrame and let whatever class invokes it handle the setVisible after its creation.

masijade.a at 2007-7-12 21:28:27 > top of Java-index,Java Essentials,New To Java...
# 2

i did what you told me to do. but i got the following errors. what do i do next?

-jGRASP exec: javac -g X:\CP4B Project\VideoLibrarySystem.java

VideoLibrarySystem.java:68: cannot find symbol

symbol : method show()

location: class LogOn

logon.show();

^

VideoLibrarySystem.java:69: cannot find symbol

symbol : method pack()

location: class LogOn

logon.pack();

^

VideoLibrarySystem.java:79: cannot find symbol

symbol : method show()

location: class LogOn

logon.show();

^

VideoLibrarySystem.java:80: cannot find symbol

symbol : method pack()

location: class LogOn

logon.pack();

^

VideoLibrarySystem.java:94: non-static variable jtp cannot be referenced from a static context

jf.getContentPane().add(jtp, BorderLayout.CENTER);//lets the GUI components be put on the content pane in the centre of the frame

^

VideoLibrarySystem.java:98: non-static variable this cannot be referenced from a static context

jbAdministratorLogOn.addActionListener(this);//allows the listener object to be registered by the source object

^

VideoLibrarySystem.java:98: non-static variable jbAdministratorLogOn cannot be referenced from a static context

jbAdministratorLogOn.addActionListener(this);//allows the listener object to be registered by the source object

^

VideoLibrarySystem.java:99: non-static variable this cannot be referenced from a static context

jbNewUserRegister.addActionListener(this);//allows the listener object to be registered by the source object

^

VideoLibrarySystem.java:99: non-static variable jbNewUserRegister cannot be referenced from a static context

jbNewUserRegister.addActionListener(this);//allows the listener object to be registered by the source object

^

VideoLibrarySystem.java:100: non-static variable this cannot be referenced from a static context

jbExistingUserLogOn.addActionListener(this);//allows the listener object to be registered by the source object

^

VideoLibrarySystem.java:100: non-static variable jbExistingUserLogOn cannot be referenced from a static context

jbExistingUserLogOn.addActionListener(this);//allows the listener object to be registered by the source object

^

11 errors

-jGRASP wedge2: exit code for process is 1.

-jGRASP: operation complete.

james-mcfaddena at 2007-7-12 21:28:27 > top of Java-index,Java Essentials,New To Java...
# 3

> Try changing

> > LogOn logon = new LogOn();

>

> to

> > LogOn logon = new LogOn();

> logon.show();

>

>

> as I did not notice any where in that code where you

> were actually showing the Dialog. You may wish to

> add a pack() to the end of the LogOn constructor,

> also.

>

> Edit: Also, your first class is poorly designed

> (possibly in multiple ways, but only one that I am

> going to mention now) in that it extends JPanel, but

> then, in its constructor creates and show a JFrame

> containing itself. If you must do this (which you

> don't, you could have left the JFrame in the main and

> added a add(jtp) to the end of the constructor) then

> the class should, at least extend JFrame and let

> whatever class invokes it handle the setVisible after

> its creation.

Also, I really don't understand why everyone of these school projects has a window listener that implements WindowAdaptor(), and the only thing it does is call System.exit() in teh windowClosing() event. Are they not teaching (or are the students not reading the API) Use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

more clear, more concise, and less typing.

JMHO

~Tim

SomeoneElsea at 2007-7-12 21:28:27 > top of Java-index,Java Essentials,New To Java...
# 4

There are a few problems with your code, I will not list them all. But to get the log on to show, here is what the issue is.

1) You have 2 LogOn classes: the public one that is called from the VideoLib class, and an inner classes in the LogOnInfoDialog that stores username and password. It works, but I would rethink that.

2) You public LogOn class never creates a dialog. The main method is never called, and should not be. You should instantiate a LogOnInfoDialog in teh constructor for the LogOn class.

public class LogOn{

LogOnInfoDialog loInfo;

public LogOn()

{

loInfo = new LogOnInfoDialog();

loInfo.setVisible(true);

}

}

and set the dialog to visible.

3) In the main constructor for the InfoDialog, the last thing in there should be

pack();

That will set all the components to their prefered size.

Hope all that helps

~Tim

SomeoneElsea at 2007-7-12 21:28:28 > top of Java-index,Java Essentials,New To Java...
# 5

> i did what you told me to do. but i got the following

> errors. what do i do next?

>

> -jGRASP exec: javac -g X:\CP4B

> Project\VideoLibrarySystem.java

>

> VideoLibrarySystem.java:68: cannot find symbol

> symbol : method show()

> location: class LogOn

> logon.show();

>^

> annot find symbol

> symbol : method pack()

> location: class LogOn

> logon.pack();

>^

> annot find symbol

> symbol : method show()

> location: class LogOn

> logon.show();

>^

> annot find symbol

> symbol : method pack()

> location: class LogOn

>logon.pack();

> ^

> 94: non-static variable jtp cannot be referenced from

> a static context

> jf.getContentPane().add(jtp,

> p, BorderLayout.CENTER);//lets the GUI components be

> put on the content pane in the centre of the frame

>^

> ariable this cannot be referenced from a static

> context

> jbAdministratorLogOn.addActionListener(this);//allow

> s the listener object to be registered by the source

> object

>

>

> VideoLibrarySystem.java:98: non-static variable

> jbAdministratorLogOn cannot be referenced from a

> static context

> jbAdministratorLogOn.addActionListener(this);//allow

> s the listener object to be registered by the source

> object

>^

> em.java:99: non-static variable this cannot be

> referenced from a static context

>

> bNewUserRegister.addActionListener(this);//allows the

> listener object to be registered by the source

> object

>^

> iable jbNewUserRegister cannot be referenced from a

> static context

>

> bNewUserRegister.addActionListener(this);//allows the

> listener object to be registered by the source

> object

>^

> ibrarySystem.java:100: non-static variable this

> cannot be referenced from a static context

>

> bExistingUserLogOn.addActionListener(this);//allows

> the listener object to be registered by the source

> object

> ^

> able jbExistingUserLogOn cannot be referenced from a

> static context

>

> bExistingUserLogOn.addActionListener(this);//allows

> the listener object to be registered by the source

> object

>^

> ors

>

> -jGRASP wedge2: exit code for process is 1.

> -jGRASP: operation complete.

Post your new code, as you have made a few mistakes in some of the changes, so it seems.

masijade.a at 2007-7-12 21:28:28 > top of Java-index,Java Essentials,New To Java...