Hi,

I made two classes in Bluej, in the first class I have constructed a window and the other class is a form where data can be inputted.

How can I put the form in the window and make the window visible. Is this possible or do I have to construct a window in the class where I made the form?

Thank you.

Recommended Answers

All 8 Replies

No, you don't have to do both things in just one class. Which class has a main method? Inside main you will construct instances of both classes, and then you can put them together (add the form to the window). If you don't understand this, post your code (using code tags, please) and someone can give you more explicit instructions.

If your window class extends JFrame, you can just call setVisible(true) on it. Which will make it visible in the default location. Also if it is a JFrame and the form class is a JPanel then you can just do the following from Class1:

getContentPane().add(Class2)

If not .... what Kramerd said.

I've added that now but its still not working.

Here's the coding I've done for the two classes.

Window class:

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

public class window1 
{
    /**
     * Constructor for objects of class window1
     */
    public window1()
    {
               
      JFrame window = new JFrame("Empty Window");
        //makes a window
        
   // Display Window
       window.setLocation( 100, 100 );
       window.setSize( 400, 300 );
       window.setVisible( true);                                                              
       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       getContentPane().add(MyForm);  

    }

}

Form Class:

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

public class MyForm extends JPanel
{
  private JPanel pane = new JPanel();

  private JLabel IDLbl = new JLabel( "ID: " );
  private JTextField ID = new JTextField( 20 );
  private JLabel firstNameLbl = new JLabel( "First Name: " );
  private JTextField firstName = new JTextField( 20 );
  private JLabel lastNameLbl = new JLabel( "Last Name: " );
  private JTextField lastName = new JTextField( 20 );
  private JLabel CourseCodeLbl = new JLabel( "Course Code: " );
  private JTextField CourseCode = new JTextField( 20 );
  private JLabel StageLbl = new JLabel( "Stage: " );
  private JRadioButton One = new JRadioButton("1",true);
  private JRadioButton Two = new JRadioButton("2",true);
  private JRadioButton Three = new JRadioButton("3",true);
 
  private JButton clearButton = new JButton( "Clear" );
  private JButton submitButton = new JButton( "Submit" );
 


public MyForm()
 {
  super();
  pane = new JPanel();
  GridBagLayout gridbag = new GridBagLayout();
  GridBagConstraints c1 = new GridBagConstraints();
  this.pane.setLayout( gridbag );
  c1.weightx = 1.0;
  c1.weighty = 1.0;
  c1.ipadx = 2;

  // Add ID
  c1.gridx = 0;
  c1.gridy = 0;
  c1.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints(IDLbl, c1 );
  this.pane.add( IDLbl );
  c1.gridx = 1;
  c1.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints(ID, c1 );
  this.pane.add( ID );

  // Add first name
  c1.gridx = 0;
  c1.gridy = 1;
  c1.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( firstNameLbl, c1 );
  this.pane.add( firstNameLbl );
  c1.gridx = 1;
  c1.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( firstName, c1 );
  this.pane.add( firstName );

  // Add last name
  c1.gridx = 0;
  c1.gridy = 2;
  c1.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( lastNameLbl, c1 );
  this.pane.add( lastNameLbl );
  c1.gridx = 1;
  c1.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( lastName, c1 );
  this.pane.add( lastName );

  //Add course code
  c1.gridx = 0;
  c1.gridy = 3;
  c1.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( CourseCodeLbl, c1 );
  this.pane.add( CourseCodeLbl );
  c1.gridx = 1;
  c1.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( CourseCode, c1 );
  this.pane.add( CourseCode );

  //Add Stage
  c1.gridx = 0;
  c1.gridy = 4;
  c1.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints(StageLbl, c1 );
  this.pane.add( StageLbl );
  c1.gridx = 1;
  c1.weighty = 2;
  c1.anchor = GridBagConstraints.WEST;
  gridbag.setConstraints( One, c1 );
  this.pane.add( One ); 
  
  c1.anchor = GridBagConstraints.CENTER;
  gridbag.setConstraints( Two, c1 );
  this.pane.add( Two ); 
 
  c1.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints( Three, c1 );
  this.pane.add( Three ); 
  
  //Add Buttons
  c1.gridx = 0;
  c1.gridy = 5;
  c1.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints(clearButton, c1 );
  this.pane.add( clearButton );

  clearButton.addActionListener( new ClearListener() );

   
  c1.gridx = 0;
  c1.gridy = 6;
  c1.anchor = GridBagConstraints.EAST;
  gridbag.setConstraints(submitButton, c1 );
  this.pane.add( submitButton );
  


 }

private class ClearListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
          ID.setText("");
	 firstName.setText("");
	 lastName.setText("");
	 CourseCode.setText("");
	 One.setSelected(false);
          Two.setSelected(false);
	 Three.setSelected(false);
	    } // end actionPerformed

	  }// end ClearListener class


public static void main( String[] args ) {
  MyForm form = new MyForm();
 }
}

Thanks!

change line 23 in window1 to

getContentPane().add(new MyForm());

And move it to before line 17.

Change line 137 in MyForm to

window1 test = new window1();

Also, since window1 is not a JFrame (you create a JFrame inside of the constructor), you will need to do

window.getContentPane().add(new MyForm());

I've tried that but its still not working :(

Is there something wrong with my coding?

OK a few other changes to make.

First in your MyForm class. You extend JPanel therefore MyForm is a JPanel. Inside the constructor you create a new panel and add everything to it, but nothing is done with that panel. So replace (in the MyForm class) all the this.pane to this. That will cause you to add to yourself (if that makes sense ... you are taking the component [label, text button, etc] and gluing it to yourself)

Now you need in window1 to do the following:
1) Move the adding of MyForm before the setvisible(true);
2) I added the main method here, since this is where you are creating your form (made sense to me)

Below is the code I used and it works.

Window

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
 
public class window1 
{
    /**
     * Constructor for objects of class window1
     */
    public window1()
    {
 
      JFrame window = new JFrame("Empty Window");
        //makes a window
 
   // Display Window
       window.setLocation( 100, 100 );
       window.setSize( 400, 300 );
       window.getContentPane().add(new MyForm());
	   window.setVisible( true);                                                              
       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 
       
    }
	
	public static void main(String[] args)
	{
		new window1();
	}
 
}

MyForm

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class MyForm extends JPanel
{
	//private JPanel pane = new JPanel(); THIS IS NOT NEEDED ANYMORE
 
	private JLabel IDLbl = new JLabel( "ID: " );
	private JTextField ID = new JTextField( 20 );
	private JLabel firstNameLbl = new JLabel( "First Name: " );
	private JTextField firstName = new JTextField( 20 );
	private JLabel lastNameLbl = new JLabel( "Last Name: " );
	private JTextField lastName = new JTextField( 20 );
	private JLabel CourseCodeLbl = new JLabel( "Course Code: " );
	private JTextField CourseCode = new JTextField( 20 );
	private JLabel StageLbl = new JLabel( "Stage: " );
	private JRadioButton One = new JRadioButton("1",true);
	private JRadioButton Two = new JRadioButton("2",true);
	private JRadioButton Three = new JRadioButton("3",true);
 
	private JButton clearButton = new JButton( "Clear" );
	private JButton submitButton = new JButton( "Submit" );
 
 
 
	public MyForm()
	{
		super();
		// pane = new JPanel(); THIS IS NOT NEEDED ANYMORE
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c1 = new GridBagConstraints();
		this.setLayout( gridbag );
		c1.weightx = 1.0;
		c1.weighty = 1.0;
		c1.ipadx = 2;
	 
		// Add ID
		c1.gridx = 0;
		c1.gridy = 0;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(IDLbl, c1 );
		this.add( IDLbl );
		c1.gridx = 1;
		c1.anchor = GridBagConstraints.WEST;
		gridbag.setConstraints(ID, c1 );
		this.add( ID );
	 
		// Add first name
		c1.gridx = 0;
		c1.gridy = 1;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints( firstNameLbl, c1 );
		this.add( firstNameLbl );
		c1.gridx = 1;
		c1.anchor = GridBagConstraints.WEST;
		gridbag.setConstraints( firstName, c1 );
		this.add( firstName );
	 
		// Add last name
		c1.gridx = 0;
		c1.gridy = 2;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints( lastNameLbl, c1 );
		this.add( lastNameLbl );
		c1.gridx = 1;
		c1.anchor = GridBagConstraints.WEST;
		gridbag.setConstraints( lastName, c1 );
		this.add( lastName );
	 
		//Add course code
		c1.gridx = 0;
		c1.gridy = 3;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints( CourseCodeLbl, c1 );
		this.add( CourseCodeLbl );
		c1.gridx = 1;
		c1.anchor = GridBagConstraints.WEST;
		gridbag.setConstraints( CourseCode, c1 );
		this.add( CourseCode );
	 
		//Add Stage
		c1.gridx = 0;
		c1.gridy = 4;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(StageLbl, c1 );
		this.add( StageLbl );
		c1.gridx = 1;
		c1.weighty = 2;
		c1.anchor = GridBagConstraints.WEST;
		gridbag.setConstraints( One, c1 );
		this.add( One ); 
	 
		c1.anchor = GridBagConstraints.CENTER;
		gridbag.setConstraints( Two, c1 );
		this.add( Two ); 
	 
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints( Three, c1 );
		this.add( Three ); 
	 
		//Add Buttons
		c1.gridx = 0;
		c1.gridy = 5;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(clearButton, c1 );
		this.add( clearButton );
	 
		clearButton.addActionListener( new ClearListener() );
	 
	 
		c1.gridx = 0;
		c1.gridy = 6;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(submitButton, c1 );
		this.add( submitButton );
 
	}
 
	class ClearListener implements ActionListener {
		public void actionPerformed(ActionEvent e)
		{
			ID.setText("");
			firstName.setText("");
			lastName.setText("");
			CourseCode.setText("");
			One.setSelected(false);
			Two.setSelected(false);
			Three.setSelected(false);
		} // end actionPerformed
 
	}// end ClearListener class
}

OK a few other changes to make.

First in your MyForm class. You extend JPanel therefore MyForm is a JPanel. Inside the constructor you create a new panel and add everything to it, but nothing is done with that panel. So replace (in the MyForm class) all the this.pane to this. That will cause you to add to yourself (if that makes sense ... you are taking the component [label, text button, etc] and gluing it to yourself)

Now you need in window1 to do the following:
1) Move the adding of MyForm before the setvisible(true);
2) I added the main method here, since this is where you are creating your form (made sense to me)

Below is the code I used and it works.

Window

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
 
public class window1 
{
    /**
     * Constructor for objects of class window1
     */
    public window1()
    {
 
      JFrame window = new JFrame("Empty Window");
        //makes a window
 
   // Display Window
       window.setLocation( 100, 100 );
       window.setSize( 400, 300 );
       window.getContentPane().add(new MyForm());
	   window.setVisible( true);                                                              
       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 
       
    }
	
	public static void main(String[] args)
	{
		new window1();
	}
 
}

MyForm

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class MyForm extends JPanel
{
	//private JPanel pane = new JPanel(); THIS IS NOT NEEDED ANYMORE
 
	private JLabel IDLbl = new JLabel( "ID: " );
	private JTextField ID = new JTextField( 20 );
	private JLabel firstNameLbl = new JLabel( "First Name: " );
	private JTextField firstName = new JTextField( 20 );
	private JLabel lastNameLbl = new JLabel( "Last Name: " );
	private JTextField lastName = new JTextField( 20 );
	private JLabel CourseCodeLbl = new JLabel( "Course Code: " );
	private JTextField CourseCode = new JTextField( 20 );
	private JLabel StageLbl = new JLabel( "Stage: " );
	private JRadioButton One = new JRadioButton("1",true);
	private JRadioButton Two = new JRadioButton("2",true);
	private JRadioButton Three = new JRadioButton("3",true);
 
	private JButton clearButton = new JButton( "Clear" );
	private JButton submitButton = new JButton( "Submit" );
 
 
 
	public MyForm()
	{
		super();
		// pane = new JPanel(); THIS IS NOT NEEDED ANYMORE
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c1 = new GridBagConstraints();
		this.setLayout( gridbag );
		c1.weightx = 1.0;
		c1.weighty = 1.0;
		c1.ipadx = 2;
	 
		// Add ID
		c1.gridx = 0;
		c1.gridy = 0;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(IDLbl, c1 );
		this.add( IDLbl );
		c1.gridx = 1;
		c1.anchor = GridBagConstraints.WEST;
		gridbag.setConstraints(ID, c1 );
		this.add( ID );
	 
		// Add first name
		c1.gridx = 0;
		c1.gridy = 1;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints( firstNameLbl, c1 );
		this.add( firstNameLbl );
		c1.gridx = 1;
		c1.anchor = GridBagConstraints.WEST;
		gridbag.setConstraints( firstName, c1 );
		this.add( firstName );
	 
		// Add last name
		c1.gridx = 0;
		c1.gridy = 2;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints( lastNameLbl, c1 );
		this.add( lastNameLbl );
		c1.gridx = 1;
		c1.anchor = GridBagConstraints.WEST;
		gridbag.setConstraints( lastName, c1 );
		this.add( lastName );
	 
		//Add course code
		c1.gridx = 0;
		c1.gridy = 3;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints( CourseCodeLbl, c1 );
		this.add( CourseCodeLbl );
		c1.gridx = 1;
		c1.anchor = GridBagConstraints.WEST;
		gridbag.setConstraints( CourseCode, c1 );
		this.add( CourseCode );
	 
		//Add Stage
		c1.gridx = 0;
		c1.gridy = 4;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(StageLbl, c1 );
		this.add( StageLbl );
		c1.gridx = 1;
		c1.weighty = 2;
		c1.anchor = GridBagConstraints.WEST;
		gridbag.setConstraints( One, c1 );
		this.add( One ); 
	 
		c1.anchor = GridBagConstraints.CENTER;
		gridbag.setConstraints( Two, c1 );
		this.add( Two ); 
	 
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints( Three, c1 );
		this.add( Three ); 
	 
		//Add Buttons
		c1.gridx = 0;
		c1.gridy = 5;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(clearButton, c1 );
		this.add( clearButton );
	 
		clearButton.addActionListener( new ClearListener() );
	 
	 
		c1.gridx = 0;
		c1.gridy = 6;
		c1.anchor = GridBagConstraints.EAST;
		gridbag.setConstraints(submitButton, c1 );
		this.add( submitButton );
 
	}
 
	class ClearListener implements ActionListener {
		public void actionPerformed(ActionEvent e)
		{
			ID.setText("");
			firstName.setText("");
			lastName.setText("");
			CourseCode.setText("");
			One.setSelected(false);
			Two.setSelected(false);
			Three.setSelected(false);
		} // end actionPerformed
 
	}// end ClearListener class
}

Thanks alot for your help. I've understood what you said and it works! :icon_cheesygrin:

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.