1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

new to Java

Discussion in 'Software Development' started by incorrigible, Feb 19, 2003.

Thread Status:
Not open for further replies.
Advertisement
  1. incorrigible

    incorrigible Thread Starter

    Joined:
    Jul 3, 2002
    Messages:
    24
    hi
    im writing a code in java where in i have 2 make menus. i have already made the menus using swing jmenubar and jmenu, etc. but now i do not know how to do the following:
    1)click on any menu item and then it takes us to another page which may be a form where in we can fill text boxes etc.
    2)also what is the use of the code

    public class WindowHandler extends WindowAdapter {
    public void windowClosing(WindowEvent e){ System.exit(0);
    }
    }

    tho it is suppose to exit from the system, i dont think i can exit after i close my application window, on the contrary i have to press ctrl+c at the command prompt.

    hope somebody can help me!
    thanx
     
  2. golfman

    golfman

    Joined:
    Aug 19, 2000
    Messages:
    118
    Hi incorrigible

    First of all, what version of Java are you using?
    this part of the code is just declaring or opening the class...
    <b>public class WindowHandler extends WindowAdapter {
    }</b>

    this part of the code is used to exit the application(it will not exit the command prompt window, or DOS box, as it's called). It is recommended for applications only. If it's used in an applet, a security exception could be thrown.
    <b>public void windowClosing(WindowEvent e){ System.exit(0);
    }</b>


    This part is just really up to you. you could do it one of several different ways(assuming this is an application)

    1) You will have to add an <b>action listener</b> or <b>item listener</b> to each menu item that you have on your jmenu(s).

    2) You could then have the listener open another frame or window(I think thats what youre calling a page?), or you could replace or change the current frame or window to show what you want, or you could just change a panel in the current frame, etc...(there are many, many possibilities here, depending on what you want to do)
     
  3. incorrigible

    incorrigible Thread Starter

    Joined:
    Jul 3, 2002
    Messages:
    24
    hi
    thank you very much on guiding me. i could manage to add the action listener,
    but now my problem is when i click on the menuitem i can c the frame title but not the entire frame. ie the frame is not really "visible ".
    i have added the line of code : - frame.setVisible(true).

    i have attached the code.
    another question is when is the window listener used? can i use it here to open a frame/window. does tht mean the same thing?

    incorrigible

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MainPage extends JFrame implements ActionListener  /*  construct a new frame that is invisible and also that items have to be 		
    
    	added not   directly on the frame but on its contentpane */
    {
    	
    	public static String title = "WAREHOUSE MANAGEMENT SYSTEM";  // declare the title
    
    	JMenuBar menuBar = new JMenuBar(); // creates a menubar
    
    	JMenu vendorMenu = new JMenu("Vendor");   // creates a menu along with its items
    	JMenuItem vendorMain = new JMenuItem("Maintenance");
    	JMenuItem vendorEnq = new JMenuItem("Enquiry");	
    
    	JMenu inventMenu = new JMenu("Inventory");   // creates a menu along with its items
    	JMenuItem inventProduct= new JMenuItem("Product Details");
    	
    	JMenu customerMenu = new JMenu("Customer");   // creates a menu along with its items
    	JMenuItem customerDetails= new JMenuItem("Customer Details");
    
    	JMenu exitMenu = new JMenu("Exit");   // creates a menu along with its items
    	JMenuItem exitClose= new JMenuItem("Close");
    
    	public MainPage()
    	{
    
    		super(title);   // calling title to display
    		setSize(765,690);
    		setBackground(Color.white);
    		vendorMain.addActionListener(this);
    		buildMenu();
    		setupEventHandlers();
    					
    	} // ends constructor
    
    	private void buildMenu()  // actually creates a menu by placing all the items
    	{
    		
    		vendorMenu.add(vendorMain);
    		vendorMenu.add(vendorEnq);
    		inventMenu.add(inventProduct);
    		customerMenu.add(customerDetails);
    		exitMenu.add(exitClose);
    		
    		menuBar.add(vendorMenu);	
    		menuBar.add(inventMenu);
    		menuBar.add(customerMenu);
    		menuBar.add(exitMenu);
    
    			
    		setJMenuBar(menuBar); 
    		setVisible(true);
    	}
    	
    	public void setupEventHandlers()
    	{
    		exitClose.addActionListener(new MenuItemHandler());
    		
    	}
    
    
    	public class MenuItemHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			String item = e.getActionCommand();
    			if(item.equals("Close"))
    				System.exit(0);
    		}		
    	}
    
    	public void actionPerformed (ActionEvent e)
    	{
    		JFrame venMainFrame = new JFrame("Vendor Maintenance");
    		venMainFrame.setSize(765,690);
    		
    		venMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		venMainFrame.pack();
    		venMainFrame.setVisible(true);
    	}
    
    	
    	public static void main(String [ ] args)
    	{
    		MainPage mp = new MainPage();
    
    			
    	} // main 
    
    } // ends class
    
    
     
  4. golfman

    golfman

    Joined:
    Aug 19, 2000
    Messages:
    118
    <b>1)</b> It looks like you are trying to set the background color of the 'MainPage' frame to white. To do this,
    you need to set the background color of the content pane of the frame -- not the frame itself

    <i>getContentPane().setBackground(Color.white);</i>

    <b>2)</b> Your 'venMainFrame is not being sized correctly because you are trying to set the size o9n the frame
    itself. Instead, give the frame a content pane and set its preferred size.

    <i>JPanel contentpane = new JPanel(); //---- create a content pane for the frame</i>
    <i>contentpane.setPreferredSize(new Dimension(765, 690)); //---- set the size of the content pane</i>
    <i>venMainFrame.setContentPane(contentpane); //---- assign the content pane to the frame</i>

    <b>3)</b> Unless you want the entire application to close when you close 'venMainFrame', you need to change
    the default close operation to '(DISPOSE_ON_CLOSE)'. This will allow just the 'venMainFrame' to close,
    and your 'MainPage' frame will remain open.

    <b>4)</b> Also, you really dont need to 'implement ActionListener' and to have 'setupEventHandlers' -- one or
    the other(by itself) will do. Myself, I would get rid of the 'setupEventHandlers' and just add the
    implemented action listener to each of the menu items(just like you have already done with 'vendorMain'.


    I have made all of the above adjustments to your code. It is posted below. There are usually many ways to set
    things up -- the way I would do it may not be what you want or the way you would do it(or it maynot work at all
    in your particular application).


    Here's the revised code... (compile it and see if the changes are what you're looking for)




    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class MainPage extends JFrame implements ActionListener /* construct a new frame that is invisible and also that items have to be
    added not directly on the frame but on its contentpane */
    {

    public static String title = "WAREHOUSE MANAGEMENT SYSTEM"; // declare the title
    JMenuBar menuBar = new JMenuBar(); // creates a menubar
    JMenu vendorMenu = new JMenu("Vendor"); // creates a menu along with its items
    JMenuItem vendorMain = new JMenuItem("Maintenance");
    JMenuItem vendorEnq = new JMenuItem("Enquiry");
    JMenu inventMenu = new JMenu("Inventory"); // creates a menu along with its items
    JMenuItem inventProduct= new JMenuItem("Product Details");

    JMenu customerMenu = new JMenu("Customer"); // creates a menu along with its items
    JMenuItem customerDetails= new JMenuItem("Customer Details");
    JMenu exitMenu = new JMenu("Exit"); // creates a menu along with its items
    JMenuItem exitClose= new JMenuItem("Close");
    public MainPage()
    {
    super(title); // calling title to display
    setSize(765,690);
    getContentPane().setBackground(Color.white);
    vendorMain.addActionListener(this);
    exitClose.addActionListener(this);
    vendorEnq.addActionListener(this);
    inventProduct.addActionListener(this);
    customerDetails.addActionListener(this);
    buildMenu();
    } // ends constructor

    private void buildMenu() // actually creates a menu by placing all the items
    {
    vendorMenu.add(vendorMain);
    vendorMenu.add(vendorEnq);
    inventMenu.add(inventProduct);
    customerMenu.add(customerDetails);
    exitMenu.add(exitClose);

    menuBar.add(vendorMenu);
    menuBar.add(inventMenu);
    menuBar.add(customerMenu);
    menuBar.add(exitMenu);

    setJMenuBar(menuBar);
    setVisible(true);
    }

    public void actionPerformed (ActionEvent e)
    {
    String item = e.getActionCommand();
    if (item.equals("Close"))
    {
    System.exit(0);
    }
    else if (item.equals("Maintenance"))
    {
    JFrame venMainFrame = new JFrame("Vendor Maintenance");
    JPanel contentpane = new JPanel();
    contentpane.setPreferredSize(new Dimension(765, 690));
    venMainFrame.setContentPane(contentpane);
    venMainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    venMainFrame.pack();
    venMainFrame.setVisible(true);
    }
    else if (item.equals("Enquiry"))
    {
    <i>//whatever you want to happen HERE</i>
    }
    else if (item.equals("Product Details"))
    {
    <i>//whatever you want to happen HERE</i>
    }
    else if (item.equals("Customer Details"))
    {
    <i>//whatever you want to happen HERE</i>
    }
    }<i>// close ActionEvent</i>

    public static void main(String [ ] args)
    {
    MainPage mp = new MainPage();
    } // main
    } // ends class
     
  5. incorrigible

    incorrigible Thread Starter

    Joined:
    Jul 3, 2002
    Messages:
    24
    hi golfman

    thank you so much for ur help.
    incorrigible
     
  6. golfman

    golfman

    Joined:
    Aug 19, 2000
    Messages:
    118
    No problem, glad to help :D
     
  7. incorrigible

    incorrigible Thread Starter

    Joined:
    Jul 3, 2002
    Messages:
    24
    hi
    i have a code here where in i have added labels to the JPanel according to the java tutorials

    but i cant get to display the labels. i am going nuts.
    plz help me


    public void actionPerformed (ActionEvent e) { String item = e.getActionCommand(); if(item.equals("Close")) { System.exit(0); } else if(item.equals("Maintenance")) { JFrame venMainFrame = new JFrame("Vendor Maintenance"); JPanel contentpane = new JPanel(); contentpane.setPreferredSize(new Dimension(765,690)); venMainFrame.setContentPane(contentpane); //contentpane.setBackground(Color.white); venMainFrame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); venMainFrame.pack(); setvenfrDesign(venMainFrame); venMainFrame.setVisible(true); } } // action Performed ends here public void setvenfrDesign(JFrame venMainFrame) { JLabel label1, label2, label3, label4, label5, label6; JPanel panel = new JPanel(); panel.add(label1); label2 = new JLabel("Vendor First Name"); label3 = new JLabel("Vendor Last Name"); label4 = new JLabel("Address"); label5 = new JLabel("Telephone #"); label6 = new JLabel("Fax"); } // end of setvenfrDesign
     
  8. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/119744

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice