This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Adding a window to a container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding a window to a container" Watch "Adding a window to a container" New topic

Adding a window to a container

joseph okon
Ranch Hand

Joined: Dec 07, 2004
Posts: 63
Hi Rangers,
I am write a simple navigation programme, if i click any button on the navigation panel instead of the called panel be display on the display panel of the application, it now display on the top left corner of the screen.

this the 1st code:
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.JPanel;

public class HomePage extends JFrame implements ActionListener {
JFrame frame;
JPanel choicePanel;
MainScreen mainScreen;
EmployeeScreen employeeScreen;
JButton employ,admin,settings,main;

public HomePage() {
frame = new JFrame("Home");
MenuBar mbar = new MenuBar();

//create the menu items
Menu file = new Menu("File");
MenuItem newF,openF,saveF,savasF,pagesetF,printF,exitF;
file.add(newF = new MenuItem("New"));
file.add(openF = new MenuItem("Open..."));
file.add(saveF = new MenuItem("Save"));
file.add(savasF = new MenuItem("Save as..."));
file.add(pagesetF = new MenuItem("Page setup"));
file.add(printF = new MenuItem("Print"));
file.add(exitF = new MenuItem("Exit"));

Menu edit = new Menu("Edit");
MenuItem undoE,cutE,copyE,pasteE,deleteE,findE,findnxtE,replaceE,selectE;
edit.add(undoE = new MenuItem("Undo.."));
edit.add(cutE = new MenuItem("Cut..."));
edit.add(copyE = new MenuItem("Copy"));
edit.add(pasteE = new MenuItem("Paste..."));
edit.add(deleteE = new MenuItem("Delete"));
edit.add(findE = new MenuItem("Find"));
edit.add(findnxtE = new MenuItem("Find next"));
edit.add(replaceE = new MenuItem("Replace"));
edit.add(selectE = new MenuItem("Select all"));

JToolBar toolbar =new JToolBar();
JButton open = new JButton();

frame.setBounds(100, 100,600,500);


private void setGUIPanel() {
Container content = frame.getContentPane();

//create a Base panel to hold navigation and display panels.
JPanel basePanel = new JPanel();
basePanel.setLayout(new BorderLayout());

//create left panel and it's components and.
//add to the Base panel.
JPanel navPanel = new JPanel();

JPanel controlBox = new JPanel(new GridLayout(0, 1, 0, 5));
main = new JButton("Main");
employ = new JButton("Employee");
admin = new JButton("Admin");
settings = new JButton("Settings");
//add button to the BoxLayout.

//add controlBox to navPanel

//create right panel and it's components.
//add to the basePanel.
choicePanel = new JPanel();

//add navPanel to BasePanel.


public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();

if(source == main) {
choicePanel.add(new MainScreen());
}else if(source == employ) {
choicePanel.add(new EmployeeScreen());

public static void main(String[] args) {
HomePage hp = new HomePage();


2nd code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class MainScreen extends JFrame {

//declare variables.
JFrame frame;

JLabel welcomeNote,welcomeNote1,loginNote,loginName,password,previlage;
JTextField logintxt;
JPasswordField passwordtxt;
JComboBox previlagecb;

public MainScreen() {

frame = new JFrame("MainScreen");

JPanel panel= new JPanel();
Container content = frame.getContentPane();
panel.setLayout(new BorderLayout());

//create welcome panel.
String name = "Welcome to " +"\n" +"Management Information System ";
String name1 ="of Abak L.G.A";
JPanel welcomePanel =new JPanel(new GridLayout(0,1,0,5));
welcomeNote = new JLabel(name);
welcomeNote1 = new JLabel(name1);
welcomeNote.setFont(new Font("Arial",Font.BOLD,22));
welcomeNote1.setFont(new Font("Arial",Font.BOLD,22));

//create login panel.
JPanel loginPanel = new JPanel(new GridLayout(0,1,0,5));
loginNote = new JLabel("Login according to your previlage");
loginName = new JLabel("Login Name:");
password = new JLabel("Password:");
previlage = new JLabel("Previlage:");
logintxt = new JTextField(10);
passwordtxt = new JPasswordField(10);
String[] previlageName ={"Management","Employee","Adiminstrator"};
previlagecb =new JComboBox(previlageName);

//create a panel to add login information.
JPanel loginInfoPanel = new JPanel(new GridLayout(0,2,0,5));

//add every component to the loginPanel.

//add welcomePanel and loginPanel to panel.



public static void main(String args[]) {
MainScreen ms =new MainScreen();


how can i correct it?
Brian Cole
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by joseph okon:
I am write a simple navigation programme, if i click any button on the navigation panel instead of the called panel be display on the display panel of the application, it now display on the top left corner of the screen.

JPanel choicePanel;

choicePanel.add(new MainScreen());

public class MainScreen extends JFrame {

Even though java.awt.Window extends java.awt.Component, you can't add
a Window (or a subclass such as JFrame or your MainScreen class) to a
container like this. I haven't tried your code, but I'm surprised you
aren't getting something like an IllegalArgumentException when you try.

If you really want to add a frame-like object to a container you can
add a JInternalFrame. [Be sure to call setVisible(true) on it.] If you
really are going to use JInternalFrames, though, you probably want the
container to which you are adding them to be a JDesktopPane.

bitguru blog
pete stein

Joined: Feb 23, 2007
Posts: 1561
Originally posted by joseph okon:
Hi Rangers,
I am write a simple navigation programme, if i click any button on the navigation panel instead of the called panel be display on the display panel of the application, it now display on the top left corner of the screen.
how can i correct it?

What you really want to do is have your first class, MainScreen, produce a JPanel that has all the stuff currently in the MainScreen JFrame subclass. I'm not sure that you need to or even should subclass JPanel as I don't see your need to alter the basic behavior of this panel. Then add the JPanel held by MainScreenPanel to your choicePanel as needed.

Another way to improve your code is to have the choicePanel use the CardLayout. Then when the appropriate button is pushed, swapping JPanels is as easy as swapping cards in the panel. Believe me, it'll work a lot better this way.
I agree. Here's the link:
subject: Adding a window to a container
Similar Threads
can this button be align at the top left coner of a container
help me I can't work the tree well
Creating a login screen
Windows Explorer in Java
Windows Explorer in Java