• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding a window to a container

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
frame.setMenuBar(mbar);

//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"));
mbar.add(file);

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"));
mbar.add(edit);

JToolBar toolbar =new JToolBar();
JButton open = new JButton();
toolbar.add(open);
toolbar.addSeparator();

frame.setBounds(100, 100,600,500);
setGUIPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

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());
content.add(basePanel);

//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");
main.addActionListener(this);
employ = new JButton("Employee");
employ.addActionListener(this);
admin = new JButton("Admin");
admin.addActionListener(this);
settings = new JButton("Settings");
settings.addActionListener(this);
//add button to the BoxLayout.
controlBox.add(main);
controlBox.add(employ);
controlBox.add(admin);
controlBox.add(settings);

//add controlBox to navPanel
navPanel.add(controlBox);

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



//add navPanel to BasePanel.
basePanel.add(navPanel,BorderLayout.WEST);
basePanel.add(choicePanel,BorderLayout.CENTER);
frame.setVisible(true);

}

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

choicePanel.removeAll();
if(source == main) {
choicePanel.removeAll();
choicePanel.add(new MainScreen());
choicePanel.validate();
//plot.setEnabled(true);
}else if(source == employ) {
choicePanel.removeAll();
choicePanel.add(new EmployeeScreen());
choicePanel.validate();
//plot.setEnabled(true);
}
}



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.*;
import java.io.*;

public class MainScreen extends JFrame {

//declare variables.
JFrame frame;

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

//Constructor
public MainScreen() {

frame = new JFrame("MainScreen");

JPanel panel= new JPanel();
Container content = frame.getContentPane();
content.add(panel);
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));
welcomePanel.add(welcomeNote);
welcomePanel.add(welcomeNote1);

//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));
loginInfoPanel.add(loginName);
loginInfoPanel.add(logintxt);
loginInfoPanel.add(password);
loginInfoPanel.add(passwordtxt);
loginInfoPanel.add(previlage);
loginInfoPanel.add(previlagecb);

//add every component to the loginPanel.
loginPanel.add(loginNote);
loginPanel.add(loginInfoPanel);

//add welcomePanel and loginPanel to panel.
panel.add(welcomePanel,BorderLayout.NORTH);
panel.add(loginPanel,BorderLayout.CENTER);

//frame.setSize(300,300);

frame.setVisible(true);
}



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

}
}


how can i correct it?
thanks
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
thanks



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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic