Results 1 to 7 of 7
  1. #1
    rahid is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Unhappy Error with Menu Bar

    I have problem with Java menu bar. When i use Flowout / BoxLayout / GridBagLayout layout manager it shows no problem. But with Absolute layout manager it shows some compilation error.

    Java Code:
    AbsoluteLayoutDemo.java:76: cannot find symbol
    symbol  : method setJMenuBar(javax.swing.JMenuBar)
    location: class AbsoluteLayoutDemo
    		setJMenuBar( bar ); // Add menu bar to application
    My code is following:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.sql.*;
    import oracle.jdbc.*;
    public class AbsoluteLayoutDemo
    {
    	private static JLabel heading; // JLabel for heading
    	private static JLabel label1;
    	private static JLabel label2;
    	private static JLabel label3;
    	private static JLabel label4;
    	private static JTextField jTextField1;
    	private static JTextField jTextField2;
    	private static JComboBox comboBox1;
    	private static JComboBox comboBox2;
    	private static JButton submit;
    
    	public static void addComponentsToPane(Container pane)
    	{
    		//super( "Project menu" );
    		pane.setLayout(null);
    
    		JMenu fileMenu = new JMenu( "File" ); // Create file menu
    		fileMenu.setMnemonic( 'F' ); // Set Mnemonic to F
    
    		// Create About... menu item
    		JMenuItem aboutItem = new JMenuItem( "About..." );
    		aboutItem.setMnemonic( 'A' ); // Set Mnemonic to A
    		fileMenu.add( aboutItem ); // Add about item to file menu
    		aboutItem.addActionListener
    		(
    			new ActionListener() // Anonymous inner class
    			{
    				// display message dialog
    				public void actionPerformed( ActionEvent event )
    				{
    					//JOptionPane.showMessageDialog( Menu.this,"This program is developed by Mohammad Rahid Hasan","About",JOptionPane.PLAIN_MESSAGE );
    				}
    			}
    		);
    		JMenuItem exitItem = new JMenuItem( "Exit" ); // Create exit item
    		exitItem.setMnemonic( 'x' ); // Set Mnemonic to x
    		fileMenu.add( exitItem );
    		exitItem.addActionListener
    		(
    			new ActionListener() // Anonymous inner class
    			{
    				// Terminate application when user click on exitItem
    				public void actionPerformed( ActionEvent event )
    				{
    					System.exit( 0 );
    				}
    			}
    		);
    
    		JMenu adminMenu = new JMenu( "Admin" ); // Create Admin Menu
    		adminMenu.setMnemonic( 'D' );
    		JMenuItem flightItem = new JMenuItem( "Add flight list" );
    		flightItem.setMnemonic( 'F' );
    		adminMenu.add( flightItem );
    		flightItem.addActionListener
    		(
    			new ActionListener() // Anonymous inner class
    			{
    				// display message dialog
    				public void actionPerformed( ActionEvent event )
    				{
    					//JOptionPane.showMessageDialog( Menu.this,"This item is not yet developed","Add flight list",JOptionPane.PLAIN_MESSAGE );
    				}
    			}
    		);
    
    		JMenuBar bar = new JMenuBar(); // create Menu bar
    		setJMenuBar( bar ); // Add menu bar to application
    		bar.add( fileMenu ); // Add file menu to menu bar
    		bar.add( adminMenu );
    
    		heading = new JLabel( "Fly High Ticket Reservation System" );
    		heading.setHorizontalTextPosition( SwingConstants.LEFT );
    		heading.setVerticalTextPosition( SwingConstants.TOP );
    		heading.setFont(new Font("Serif", Font.BOLD, 20));
    		
    
    		label1 = new JLabel( "First Name:" );
    		label1.setHorizontalTextPosition( SwingConstants.LEFT );
    		label1.setVerticalTextPosition( SwingConstants.TOP );
    		label1.setFont(new Font("Serif", Font.PLAIN, 12));
    
    		// construct textfield with 10 columns
    		jTextField1 = new JTextField( 10 );
    
    		label2 = new JLabel( "Last Name:" );
    		label2.setHorizontalTextPosition( SwingConstants.LEFT );
    		label2.setVerticalTextPosition( SwingConstants.TOP );
    		label2.setFont(new Font("Serif", Font.PLAIN, 12));
    
    		jTextField2 = new JTextField( 10 );
    
    		label3 = new JLabel( "Select source sector:" );
    		label3.setHorizontalTextPosition( SwingConstants.LEFT );
    		label3.setVerticalTextPosition( SwingConstants.TOP );
    		label3.setFont(new Font("Serif", Font.PLAIN, 12));
    
    		String sectors[] = { "DHK", "CTG", "SYL", "COX", "RAJ", "JES" };
    		JComboBox comboBox1 = new JComboBox( sectors );
    
    		label4 = new JLabel( "Select destination sector:" );
    		label4.setHorizontalTextPosition( SwingConstants.LEFT );
    		label4.setVerticalTextPosition( SwingConstants.TOP );
    		label4.setFont(new Font("Serif", Font.PLAIN, 12));
    
    		JComboBox comboBox2 = new JComboBox( sectors );
    
    		JButton submit = new JButton( "Submit" );
    
    		JLabel image = new JLabel("",new ImageIcon("images.jpg"),JLabel.CENTER);
    
    		pane.add( heading );
    		pane.add( label1 );
    		pane.add( jTextField1 );
    		pane.add( label2 );
    		pane.add( jTextField2 );
    		pane.add( label3 );
    		pane.add( comboBox1 );
    		pane.add( label4 );
    		pane.add( comboBox2 );
    		pane.add( submit );
    		pane.add( image );
    
            Insets insets = pane.getInsets();
            //Dimension size = b1.getPreferredSize();
    
            heading.setBounds( 130 + insets.left, 10 + insets.top, 500, 25 );
            label1.setBounds( 85 + insets.left, 80 + insets.top, 100, 50 );
    		jTextField1.setBounds( 70 + insets.left, 120 + insets.top, 150, 18 );
            label2.setBounds( 85 + insets.left, 120 + insets.top, 100, 50 );
    		jTextField2.setBounds( 70 + insets.left, 160 + insets.top, 150, 18 );
            label3.setBounds( 85 + insets.left, 160 + insets.top, 150, 50 );
    		comboBox1.setBounds( 70 + insets.left, 200 + insets.top, 80, 18 );
            label4.setBounds( 85 + insets.left, 203 + insets.top, 170, 50 );
    		comboBox2.setBounds( 70 + insets.left, 243 + insets.top, 80, 18 );
    		submit.setBounds( 70 + insets.left, 270 + insets.top, 90, 18 );
    		image.setBounds( 370, 100, 230, 178 );
    	}
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("AbsoluteLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Set up the content pane.
            addComponentsToPane(frame.getContentPane());
    
            //Size and display the window.
            Insets insets = frame.getInsets();
            frame.setSize(700 + insets.left + insets.right,
                          400 + insets.top + insets.bottom);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    I'm completely new in java. So please help me & get me out of this problem.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by rahid View Post
    I have problem with Java menu bar. When i use Flowout / BoxLayout / GridBagLayout layout manager it shows no problem. But with Absolute layout manager it shows some compilation error.

    Java Code:
    AbsoluteLayoutDemo.java:76: cannot find symbol
    symbol  : method setJMenuBar(javax.swing.JMenuBar)
    location: class AbsoluteLayoutDemo
    		setJMenuBar( bar ); // Add menu bar to application
    Your compiler is right: your class doesn't have a method setJMenuBar( ... ). What did you have in mind? What or where should the menu bar be set?

    kind regards,

    Jos

  3. #3
    rahid is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    I just need a menu bar in the window with Absolute Layout manger. And of course it will be st in top of the window. What should my code.

    Thanx for reply.
    Last edited by rahid; 05-06-2010 at 08:49 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by rahid View Post
    I have problem with Java menu bar. When i use Flowout / BoxLayout / GridBagLayout layout manager it shows no problem. But with Absolute layout manager it shows some compilation error.

    Java Code:
    AbsoluteLayoutDemo.java:76: cannot find symbol
    symbol  : method setJMenuBar(javax.swing.JMenuBar)
    location: class AbsoluteLayoutDemo
    		setJMenuBar( bar ); // Add menu bar to application
    As already mentioned, that error is nothing to do with the layout manager. So I'd look at the version of the code that works with the other managers and compare them to this one.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    the problem is, you're passing the panel to your method addComponentsToPane and a jpanel has no method called setJMenuBar. i changed your code, so that the frame is passed to the method because the jframe has the method called setJMenuBar and then your code runs without problems.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.sql.*;
    import oracle.jdbc.*;
    
    public class AbsoluteLayoutDemo
    {
    	private static JLabel heading; // JLabel for heading
    	private static JLabel label1;
    	private static JLabel label2;
    	private static JLabel label3;
    	private static JLabel label4;
    	private static JTextField jTextField1;
    	private static JTextField jTextField2;
    	private static JComboBox comboBox1;
    	private static JComboBox comboBox2;
    	private static JButton submit;
    
    	// first change is the argument
    	public static void addComponentsToPane(JFrame frame)
    	{
    		// get your pane from the frame
    		Container pane = frame.getContentPane();
    		pane.setLayout(null);
    
    		JMenu fileMenu = new JMenu( "File" ); // Create file menu
    		fileMenu.setMnemonic( 'F' ); // Set Mnemonic to F
    
    		// Create About... menu item
    		JMenuItem aboutItem = new JMenuItem( "About..." );
    		aboutItem.setMnemonic( 'A' ); // Set Mnemonic to A
    		fileMenu.add( aboutItem ); // Add about item to file menu
    		aboutItem.addActionListener
    		(
    			new ActionListener() // Anonymous inner class
    			{
    				// display message dialog
    				public void actionPerformed( ActionEvent event )
    				{
    					//JOptionPane.showMessageDialog( Menu.this,"This program is developed by Mohammad Rahid Hasan","About",JOptionPane.PLAIN_MESSAGE );
    				}
    			}
    		);
    		JMenuItem exitItem = new JMenuItem( "Exit" ); // Create exit item
    		exitItem.setMnemonic( 'x' ); // Set Mnemonic to x
    		fileMenu.add( exitItem );
    		exitItem.addActionListener
    		(
    			new ActionListener() // Anonymous inner class
    			{
    				// Terminate application when user click on exitItem
    				public void actionPerformed( ActionEvent event )
    				{
    					System.exit( 0 );
    				}
    			}
    		);
    
    		JMenu adminMenu = new JMenu( "Admin" ); // Create Admin Menu
    		adminMenu.setMnemonic( 'D' );
    		JMenuItem flightItem = new JMenuItem( "Add flight list" );
    		flightItem.setMnemonic( 'F' );
    		adminMenu.add( flightItem );
    		flightItem.addActionListener
    		(
    			new ActionListener() // Anonymous inner class
    			{
    				// display message dialog
    				public void actionPerformed( ActionEvent event )
    				{
    					//JOptionPane.showMessageDialog( Menu.this,"This item is not yet developed","Add flight list",JOptionPane.PLAIN_MESSAGE );
    				}
    			}
    		);
    
    		JMenuBar bar = new JMenuBar(); // create Menu bar
    		// the bar is added to the frame and not to the panel
    		frame.setJMenuBar( bar ); // Add menu bar to application
    		bar.add( fileMenu ); // Add file menu to menu bar
    		bar.add( adminMenu );
    
    		heading = new JLabel( "Fly High Ticket Reservation System" );
    		heading.setHorizontalTextPosition( SwingConstants.LEFT );
    		heading.setVerticalTextPosition( SwingConstants.TOP );
    		heading.setFont(new Font("Serif", Font.BOLD, 20));
    		
    
    		label1 = new JLabel( "First Name:" );
    		label1.setHorizontalTextPosition( SwingConstants.LEFT );
    		label1.setVerticalTextPosition( SwingConstants.TOP );
    		label1.setFont(new Font("Serif", Font.PLAIN, 12));
    
    		// construct textfield with 10 columns
    		jTextField1 = new JTextField( 10 );
    
    		label2 = new JLabel( "Last Name:" );
    		label2.setHorizontalTextPosition( SwingConstants.LEFT );
    		label2.setVerticalTextPosition( SwingConstants.TOP );
    		label2.setFont(new Font("Serif", Font.PLAIN, 12));
    
    		jTextField2 = new JTextField( 10 );
    
    		label3 = new JLabel( "Select source sector:" );
    		label3.setHorizontalTextPosition( SwingConstants.LEFT );
    		label3.setVerticalTextPosition( SwingConstants.TOP );
    		label3.setFont(new Font("Serif", Font.PLAIN, 12));
    
    		String sectors[] = { "DHK", "CTG", "SYL", "COX", "RAJ", "JES" };
    		JComboBox comboBox1 = new JComboBox( sectors );
    
    		label4 = new JLabel( "Select destination sector:" );
    		label4.setHorizontalTextPosition( SwingConstants.LEFT );
    		label4.setVerticalTextPosition( SwingConstants.TOP );
    		label4.setFont(new Font("Serif", Font.PLAIN, 12));
    
    		JComboBox comboBox2 = new JComboBox( sectors );
    
    		JButton submit = new JButton( "Submit" );
    
    		JLabel image = new JLabel("",new ImageIcon("images.jpg"),JLabel.CENTER);
    
    		pane.add( heading );
    		pane.add( label1 );
    		pane.add( jTextField1 );
    		pane.add( label2 );
    		pane.add( jTextField2 );
    		pane.add( label3 );
    		pane.add( comboBox1 );
    		pane.add( label4 );
    		pane.add( comboBox2 );
    		pane.add( submit );
    		pane.add( image );
    
            Insets insets = pane.getInsets();
            //Dimension size = b1.getPreferredSize();
    
            heading.setBounds( 130 + insets.left, 10 + insets.top, 500, 25 );
            label1.setBounds( 85 + insets.left, 80 + insets.top, 100, 50 );
    		jTextField1.setBounds( 70 + insets.left, 120 + insets.top, 150, 18 );
            label2.setBounds( 85 + insets.left, 120 + insets.top, 100, 50 );
    		jTextField2.setBounds( 70 + insets.left, 160 + insets.top, 150, 18 );
            label3.setBounds( 85 + insets.left, 160 + insets.top, 150, 50 );
    		comboBox1.setBounds( 70 + insets.left, 200 + insets.top, 80, 18 );
            label4.setBounds( 85 + insets.left, 203 + insets.top, 170, 50 );
    		comboBox2.setBounds( 70 + insets.left, 243 + insets.top, 80, 18 );
    		submit.setBounds( 70 + insets.left, 270 + insets.top, 90, 18 );
    		image.setBounds( 370, 100, 230, 178 );
    	}
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("AbsoluteLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Set up the content pane.
            addComponentsToPane(frame);
    
            //Size and display the window.
            Insets insets = frame.getInsets();
            frame.setSize(700 + insets.left + insets.right,
                          400 + insets.top + insets.bottom);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    Last edited by j2me64; 05-06-2010 at 03:19 PM.

  6. #6
    rahid is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    ThanX a lot j2me64. ThanX for help..............:)

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by rahid View Post
    ThanX a lot j2me64. ThanX for help..............:)

    you are welcome!

Similar Threads

  1. How do I get a pop-up menu?
    By xcallmejudasx in forum Eclipse
    Replies: 2
    Last Post: 10-27-2008, 10:06 PM
  2. Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  3. How to use SWT menu and menu event
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:46 PM
  4. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  5. Replies: 7
    Last Post: 05-09-2008, 07:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •