Help me.....i'm tryin to figure out how to link to another java class using a button but i falied to do so...kindly assist me....thanks

public Login() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400,400);
    
    java.awt.Container contents = getContentPane();
    contents.setLayout(null);
    setTitle("Air-Malaysia");
    contents.setLayout(null );
    contents.setBackground(Color.black);
    contents.setForeground(Color.orange);
     am.setFont(f1);
     usrnm.setFont(f2);
     clk.setFont(f2);
     clk.setBackground(Color.black);
     clk.setForeground(Color.orange);
     btnOk.setBackground(Color.black);
     btnOk.setForeground(Color.orange);
     btnCancel.setBackground(Color.black);
     btnCancel.setForeground(Color.orange);
     usrnm.setForeground(Color.orange);
     pswrd.setForeground(Color.orange);
     nwm.setForeground(Color.orange);
     am.setForeground(Color.orange);

    am.setLocation(100,0);
    am.setSize(150,150);
    contents.add(am);

        usrnm.setLocation(20,100);
    	usrnm.setSize(120,25);
    	add(usrnm);
        
        txtname.setLocation(100,100);
    	txtname.setSize(120,25);
    	add(txtname);

        pswrd.setLocation(20,150);
    	pswrd.setSize(120,25);
    	add(pswrd);

        txtpswrd.setLocation(100,150);
    	txtpswrd.setSize(120,25);
    	add(txtpswrd);

        nwm.setLocation(20,200);
    	nwm.setSize(120,25);
    	add(nwm);

        clk.setLocation(200,200);
    	clk.setSize(120,25);
    	add(clk);

        btnOk.setLocation(20,250);
    	btnOk.setSize(120,25);
    	add(btnOk);

        btnCancel.setLocation(200,250);
    	btnCancel.setSize(120,25);
    	add(btnCancel);
     btnOk.addActionListener(this);
     btnCancel.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){
        if(e.getSource()==btnOk)
        {
        JOptionPane.showMessageDialog(null, "Welcome Mr/Ms " + txtname.getText());
        OptionWindow option = new OptionWindow();
        setPreferredSize(new Dimension(400,400));
	add(option);
        }
        
     if(e.getSource()==btnCancel)
         JOptionPane.showMessageDialog(null, "Thank You for using Air-Malaysia Online");
          System.exit(0);
}
package oopassignment;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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

public class OptionWindow extends JFrame implements ActionListener {
JButton btnexit = new JButton("Logout");
JButton b1 = new JButton("Customer Profile");
JButton b2 = new JButton("Flight Information");
JButton b3 = new JButton("Reservation");

    public OptionWindow() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400,400);
    java.awt.Container contents = getContentPane();
    contents.setLayout(null);
    setTitle("Air-Malaysia");
    contents.setLayout(null );
    contents.setBackground(Color.black);
    contents.setForeground(Color.orange);
     b1.setBackground(Color.black);
     b1.setForeground(Color.orange);
     b2.setBackground(Color.black);
     b2.setForeground(Color.orange);
     b3.setBackground(Color.black);
     b3.setForeground(Color.orange);
     btnexit.setBackground(Color.red);
     btnexit.setForeground(Color.white);

        btnexit.setLocation(280,0);
        btnexit.setSize(80,25);
        add(btnexit);
         
        b1.setLocation(110,170);
    	b1.setSize(170,25);
    	add(b1);

        b2.setLocation(110,220);
    	b2.setSize(170,25);
    	add(b2);

        b3.setLocation(110,270);
    	b3.setSize(170,25);
    	add(b3);
    }

    public void actionPerformed(ActionEvent e){
}
     public static void main(String[] args) {
         new OptionWindow();
 }// TODO overwrite start(), stop() and destroy() methods
}

ow to link to another java class using a button

Where in your code do you want to "link" to another class?
For which button? I see 4 buttons

What do you mean by "link to"? Call a method in the class, create a new instance of the class or ???

To have code be executed when a button is pressed, you need to add an action listener to the button and implement an action listener object that will be called. In that action listener you can "link to" the other class.

For code samples, Search on the forum for addActionListener

Member Avatar for coil

I believe the OP was talking about here:

OptionWindow option = new OptionWindow();
        setPreferredSize(new Dimension(400,400));
	add(option);

Line 67 and onwards in the first code segment.

Try some basic debugging. Does your button register a click?

It seems you want the btnOk button to bring you to a new 'menu' with the 'logout'etc.
You dont have a frame in the OptionWindow class that I can see. You need to create a frame and add all those things to it. Then its just a matter of hiding the first frame, which can be done by creating a new method such as showFrame(), which makes the frame visible/enabled. So, instead of the add(option) in your ActionListener method, you could have

option.showFrame()//or whatever it is

Where in your code do you want to "link" to another class?
For which button? I see 4 buttons

What do you mean by "link to"? Call a method in the class, create a new instance of the class or ???

To have code be executed when a button is pressed, you need to add an action listener to the button and implement an action listener object that will be called. In that action listener you can "link to" the other class.

For code samples, Search on the forum for addActionListener

This is my full code...

package oopassignment;
import java.awt.*;
import java.lang.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

public class Login extends JFrame implements ActionListener  {
JPanel p1=new JPanel(new GridLayout(5,1));
//JPanel p2=new JPanel(new GridLayout(1,2));
//JPanel p3=new JPanel(new GridLayout(1,2));
// p4=new JPanel(new GridLayout(1,2));
//JPanel p5=new JPanel(new GridLayout(1,2));
JLabel am=new JLabel("Air-Malaysia....");
JLabel usrnm=new JLabel("Username : ");
JLabel nwm=new JLabel("Not yet a member? ");
JButton clk=new JButton("Click Here");
JLabel pswrd=new JLabel("Password : ");
JTextField txtname = new JTextField();
JPasswordField txtpswrd = new JPasswordField();
JButton btnOk=new JButton("Okay");
JButton btnCancel=new JButton("Exit");
Font f1 = new Font("TimesRoman",Font.BOLD,20);
Font f2 = new Font("TimesRoman",Font.BOLD,11);

    public Login() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400,400);
    
    java.awt.Container contents = getContentPane();
    contents.setLayout(null);
    setTitle("Air-Malaysia");
    contents.setLayout(null );
    contents.setBackground(Color.black);
    contents.setForeground(Color.orange);
     am.setFont(f1);
     usrnm.setFont(f2);
     clk.setFont(f2);
     clk.setBackground(Color.black);
     clk.setForeground(Color.orange);
     btnOk.setBackground(Color.black);
     btnOk.setForeground(Color.orange);
     btnCancel.setBackground(Color.black);
     btnCancel.setForeground(Color.orange);
     usrnm.setForeground(Color.orange);
     pswrd.setForeground(Color.orange);
     nwm.setForeground(Color.orange);
     am.setForeground(Color.orange);

    am.setLocation(100,0);
    am.setSize(150,150);
    contents.add(am);

        usrnm.setLocation(20,100);
    	usrnm.setSize(120,25);
    	add(usrnm);
        
        txtname.setLocation(100,100);
    	txtname.setSize(120,25);
    	add(txtname);

        pswrd.setLocation(20,150);
    	pswrd.setSize(120,25);
    	add(pswrd);

        txtpswrd.setLocation(100,150);
    	txtpswrd.setSize(120,25);
    	add(txtpswrd);

        nwm.setLocation(20,200);
    	nwm.setSize(120,25);
    	add(nwm);

        clk.setLocation(200,200);
    	clk.setSize(120,25);
    	add(clk);

        btnOk.setLocation(20,250);
    	btnOk.setSize(120,25);
    	add(btnOk);

        btnCancel.setLocation(200,250);
    	btnCancel.setSize(120,25);
    	add(btnCancel);
     btnOk.addActionListener(this);
     btnCancel.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){
        if(e.getSource()==btnOk)
        {
        JOptionPane.showMessageDialog(null, "Welcome Mr/Ms " + txtname.getText());
        OptionWindow customerPage = new OptionWindow();
    		//dispose();
    		customerPage.show();
        }
        
     if(e.getSource()==btnCancel)
         JOptionPane.showMessageDialog(null, "Thank You for using Air-Malaysia Online");
          System.exit(0);
}
 public static void main(String[] args) {
 new Login();
 }
}
package oopassignment;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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

public class OptionWindow extends JFrame implements ActionListener {
JButton btnexit = new JButton("Logout");
JButton b1 = new JButton("Customer Profile");
JButton b2 = new JButton("Flight Information");
JButton b3 = new JButton("Reservation");

    public OptionWindow() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400,400);
    java.awt.Container contents = getContentPane();
    contents.setLayout(null);
    setTitle("Air-Malaysia");
    contents.setLayout(null );
    contents.setBackground(Color.black);
    contents.setForeground(Color.orange);
     b1.setBackground(Color.black);
     b1.setForeground(Color.orange);
     b2.setBackground(Color.black);
     b2.setForeground(Color.orange);
     b3.setBackground(Color.black);
     b3.setForeground(Color.orange);
     btnexit.setBackground(Color.red);
     btnexit.setForeground(Color.white);

        btnexit.setLocation(280,0);
        btnexit.setSize(80,25);
        add(btnexit);
         
        b1.setLocation(110,170);
    	b1.setSize(170,25);
    	add(b1);

        b2.setLocation(110,220);
    	b2.setSize(170,25);
    	add(b2);

        b3.setLocation(110,270);
    	b3.setSize(170,25);
    	add(b3);
    }

    public void actionPerformed(ActionEvent e){
}
     public static void main(String[] args) {
         new OptionWindow();
 }// TODO overwrite start(), stop() and destroy() methods
}

The thing is i want the btnOk in Login class to call the OptionWindow class....but somehow its not working....thanks

Member Avatar for coil

Here are my suggestions:

1. Move the OptionWindow class into the same file as the Login file. Get rid of the main method in the OptionWindow class and make it a user-defined class.

2. In the constructor for the OptionWindow, set the frame visible (check the API if you're not sure how to).

somehow its not working

Can you explain what happens?
Is the listener code called?
Is the Welcome message shown?
Is the OptionWindow class's constructor executed?

Can you explain what happens?
Is the listener code called?
Is the Welcome message shown?
Is the OptionWindow class's constructor executed?

Yes, the listener is working, cause the message box is appearing but not the OptionWindows constructor. i tried option.show(); but its not working either

Where is the variable 'option'? I don't see it in your last code.

but not the OptionWindows constructor

How do you know it is not executing? Add a println() at the end of it to see.

Member Avatar for coil

Did you try setVisible(true)?

Where is the variable 'option'? I don't see it in your last code.


How do you know it is not executing? Add a println() at the end of it to see.

yeap...i did println at the end of the constructor....its showing tat...but the frame is not appearing...

yes, i did include the setVisible(true);

What does the initial frame show?
Your code doesn't hide it.

What does the initial frame show?
Your code doesn't hide it.

The initial frame is the login windows...from there once the user clicks ok..it should close the current frame and open the OptionWindow frame....

public void actionPerformed(ActionEvent e){
        if(e.getSource()==btnOk)
        {
        JOptionPane.showMessageDialog(null, "Welcome Mr/Ms " + txtname.getText());
        OptionWindow customerPage = new OptionWindow();
        customerPage.show();
        }

it should close the current frame

Does it do that?
Where in your code do you close the current frame?

Where are you hiding the login frame in the code?

Does it do that?
Where in your code do you close the current frame?

public void actionPerformed(ActionEvent e){
        if(e.getSource()==btnOk)
        {
        JOptionPane.showMessageDialog(null, "Welcome Mr/Ms " + txtname.getText());
        OptionWindow customerPage = new OptionWindow();
        setDefaultCloseOperation(Login.HIDE_ON_CLOSE); //[B]New Line[/B]
        customerPage.show();
        }

will this work?

i was under the impression that the _ON_CLOSE is only executed when you click the x button at the top right.

I don't know how to work it into your code but:
frame.setVisible(false); is usually how I did what you are trying to do.

and also maybe setEnabled(false); so you cannot click buttons that are not shown,

Member Avatar for coil
public void actionPerformed(ActionEvent e){
        if(e.getSource()==btnOk)
        {
        JOptionPane.showMessageDialog(null, "Welcome Mr/Ms " + txtname.getText());
        OptionWindow customerPage = new OptionWindow();
        setDefaultCloseOperation(Login.HIDE_ON_CLOSE); //[B]New Line[/B]
        customerPage.show();
        }

will this work?

It will compile, but it won't work as expected. The user has to click the X, as Akill said, to trigger the event of hiding the window.

setVisible(false) will hide the window - no need to setEnabled(false) too, because you can't click buttons if the frame isn't visible.

I'm not sure why you're hiding it though. Unless you need to bring it up again, disposing of the entire frame with the [frame].dispose() method is probably better.

My question was/is: What happens to the initial window when you click on the button to open the new window. The old window should still be shown underneath the new one.
Is it still there? If not where is it? You have a bug in your code that you need to find.

My question was/is: What happens to the initial window when you click on the button to open the new window. The old window should still be shown underneath the new one.
Is it still there? If not where is it? You have a bug in your code that you need to find.

No, i setVisible(false); to the old window, because i want to display the new window. the thing is after calling the OptionWindow constructor the frame(optionWindow's) appears and the disappears....i can't make the frame stay....

sorry regarding the new thread.

Your posted code does NOT setVisible(false) yet the original frame disappears. The reason the frame disappears is because the java program exits. Why does the java program exit?

Your posted code does NOT setVisible(false) yet the original frame disappears. The reason the frame disappears is because the java program exits. Why does the java program exit?

hmm....i'm not sure...because if i run the codes individually, OpenWindows frame juz stays there and doesn't disappears...it only happens when i link it in Login.java....what can cause a program to terminate in such a way?...it waits until i click okay in the dialogbox... it disappears then...

hahaha...i manage to solve it.....the line system.exit(0); was giving problem...now it works fine...thanks...

It's best to ALWAYS put {} with if() statements.
Indention doesn't mean anything to a compiler, but it will fool people looking at the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.