java problem (GUI)

jonneymendoza

Supreme [H]ardness
Joined
Sep 11, 2004
Messages
6,395
Hi, I am trying to create a very basic form using java netbeans. i have jdk 1.5.

I have two classes, the main class and the gui class. The main class creates a Jframe and adds then adds the gui components from the gui class I made. The problem is when I try to run the program, the compiler throws an exception saying that I am adding a container to a window. The line it points at is highlighted below:



Code:
import com.sun.org.apache.bcel.internal.verifier.structurals.Frame;
import javax.swing.JFrame;
import java.awt.*;

public class Main 
{
    

    
 
    public static void main(String[] args) 
    {
        
        JFrame frame = new JFrame();
        //frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setTitle("Login");
        Login test1 = new Login();
        frame.getContentPane().add(test1);   //Line in which the compiler throws an exception
        frame.setSize(200, 150);
        frame.setVisible(true);
        
        
        
        // TODO code application logic here
    }
    
}
 
What do you mean by Login is a gui subclass?

Do you have Login extending a swing container? Such as:
Code:
public class Login extends JPanel
 
Off the top of my head, I can't figure out the problem. I'm pretty sure the problem lies in the code that you didn't give us :p

If you could provide us with the code of the Login class, then we could be able to recreate the problem on our own machines. Which would help us greatly in helping you.

Cheers!
Simon.
 
ok cool here it goes, thanks

Code:
/*
 * Login.java
 *
 * Created on 18 January 2006, 16:55
 */

/**
 *
 * @author  jonathan
 */
class Login extends javax.swing.JFrame implements java.awt.event.ActionListener {
    
    /** Creates new form Login */
    public Login() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();

        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jButton1.setText("Login");
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 220, -1, -1));

        jLabel1.setText("Username");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 120, -1, -1));

        jLabel2.setText("Password");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 150, 50, -1));

        jTextField1.addActionListener(this);

        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 120, 170, -1));

        jButton2.setText("Register");
        jButton2.addActionListener(this);

        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 220, -1, -1));

        getContentPane().add(jPasswordField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 150, 170, 20));

        pack();
    }

    // Code for dispatching events from components to event handlers.

    public void actionPerformed(java.awt.event.ActionEvent evt) {
        if (evt.getSource() == jButton2) {
            Login.this.jButton2ActionPerformed(evt);
        }
        else if (evt.getSource() == jTextField1) {
            Login.this.jTextField1ActionPerformed(evt);
        }
    }
    // </editor-fold>                        

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
// TODO add your handling code here:
    }                                           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
    }                                        
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
       // java.awt.EventQueue.invokeLater(new Runnable() {
           // public void run() {
              //  new Login().setVisible(true);
           // }
        //});
    }
    
    // Variables declaration - do not modify                     
    public javax.swing.JButton jButton1;
    public javax.swing.JButton jButton2;
    public javax.swing.JLabel jLabel1;
    public javax.swing.JLabel jLabel2;
    public javax.swing.JLayeredPane jLayeredPane1;
    public javax.swing.JPasswordField jPasswordField1;
    public javax.swing.JTextField jTextField1;
    // End of variables declaration                   
    
}
 
Well there you go,

Code:
class Login extends javax.swing.JFrame

You cannot add a JFrame to a panel. You just need to make Login extends JPanel or some other low-level container and you'll be good to go!

Cheers,
Simon.
 
Code:
class Login extends JPanel implements java.awt.event.ActionListener {

I added the code above and now i get over 10 errors :( .

one of the error tells me that it cannot find the symbol from the above code :( damm netbeans it auto generates some of the code
 
Well, that's probably because you have no imports at the start of the file ;)

Wether add;

import javax.swing.*;

or simply extend;

class Login extends javax.swing.JPanel implements java.awt.event.ActionListener {

Cheers!
Simon.
 
Its not working :( i seem to be generating more errors refring to the login class :( I just want the Main class to run the Login Gui class but notings working. man i am no good at SWING GUI's but i amgood at awt...........
 
Well alright, we'll try and get it working so let's try this then:

Put things as they were, meaning that Login extends JFrame.

Now, replace your Main.main method to this:

Code:
    public static void main(String[] args) 
    {
        Login test1 = new Login();
        test1.setTitle("Login");
        test1.setSize(200, 150);
        test1.setVisible(true);
        
        
        // TODO code application logic here
    }

this won't be a cure at all. But it should allow you to finally see your Frame in action.

Hope this works,
Simon.
 
It works!!! Thanks it did cure my program thanks man. one more question, how can i open the window so that it opens right at the middle of the screen instead of on the top left?
 
(Nemezer eagerly awaits the return of jonneymendoza when he'll realize that he still isn't out of the woods!)

But I'm glad it worked.

Cheers!
Simon.
 
In response to your edit.

You need two variables for that. First you need to know the Dimentions of your Frame;
javax.swing.JFrame.getSize();

and then you need to know the resolution of the clients computer;

java.awt.Toolkit.getScreenSize();

Afterwards, a little magic like;

x = (screen.x -frame.x) /2;
y = (screen.y -frame.y) /2;

once you have x and y, you can call your JFrame's setLocation(new Point(x, y));

That is the recipy to rigthly frame your Frames.

Tourelou,
Simon.
 
Back
Top