hi,
i have a JFrame... one menuBar placed on a JPanel.. and then two JPanel with different things (let me call them ActionPanelOne, and ActionPanelTwo).

i put the JPanel (with JMenuBar) North of the JFrame, and menuIteam will call the 2 ActionPanel individually to come up.

my problem is:
Once the ActionPanel is visible, the view of my dropped down menu items will be partially block by the ActionPanel.

is there a way to set the Panel(with menuBar) or the menuBar dropped down list alway on top or something? This is really bothering me.

thanks.

Recommended Answers

All 14 Replies

I'm hardly an expert, but have you tried programmatically "clicking" the menu item again after the JPanel is set visible? That might make it appear on top again. Worth a try anyway, although even if it works, there is probably a better solution. I ran into a similar problem but mine was because I was using netbeans and the panels actually overlapped on each other.

I'm hardly an expert, but have you tried programmatically "clicking" the menu item again after the JPanel is set visible? That might make it appear on top again. Worth a try anyway, although even if it works, there is probably a better solution. I ran into a similar problem but mine was because I was using netbeans and the panels actually overlapped on each other.

i am using eclipse, i don't know if it has the same problem netbeans has. but it is weird that i have a JScrollPane that works perfectly fine along with the JMenu.. the dropped down wouldn't be blocked. but the Jpanel just block the dropped down. huh... tried diff things and no luck so far... hope someone GUI expert stopped by and shed me some light.

sigh... never had a professor really know stuffs. people in this forum can answer my question much better.

Well, post your code. I'll run it and fool with it and try to help you out. Also, I didn't post this yesterday because I don't want to lead you in the wrong direction, but perhaps it has something to do with some of the issues discussed here?

edit:

And this is a guess, but I think JPanels are opaque by default, (see: setOpaque method) whereas JScrollPanes are not. So that would mean that when you add the JPanel, it paints the entire JPanel, covering your menu. But since JScrollPanes are not, this doesn't happen when you add it. Try invoking the JPanels setOpaque(false) method before you add it and see what happens.

set opaque method

well, i do have to say thank you for the on going help.

but yea, i tried the setOpaque(false) for my panel and guess this is not the solution.

i'll post my code and see if you may see something.
those sql thing you may comment out .. sorry, i can't send you the mdb file here so...

////// main , the frame ///////
import java.text.MessageFormat;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;

public class TheFrame extends JFrame
{
	private JPanel menuPanel;
	private JPanel text;
	private BorderLayout bl = new BorderLayout();
	
	
	public TheFrame()
	{
		
		menuPanel = new MenuBar(this);
		menuPanel.setBackground(java.awt.Color.white);
		
		JLabel one = new JLabel("Java Final Project: Employee Record");
		one.setAlignmentX(0.5f);
		one.setAlignmentY(1f);
		JLabel two = new JLabel("Written by Henry Li");
		two.setAlignmentX(0.5f);
		two.setAlignmentY(1f);
		
		text = new JPanel();
		text.setBackground(java.awt.Color.white);
		LayoutManager boxLayout = new BoxLayout(text, BoxLayout.Y_AXIS);
		
		text.setLayout(boxLayout);
		text.add(one);
		text.add(two);
		
		
		
		
		
		add(menuPanel, BorderLayout.NORTH);
		add(text, BorderLayout.CENTER);
		validate();
		
		setBounds(400, 400, 600, 300);
		setVisible(true);
   
	}
	
	

	
	

	public static void main(String arg[]) throws SQLException
	{
		TheFrame frame = new TheFrame();
	}

	
}// end the Class TheFrame
// the menu class 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;

public class MenuBar extends JPanel
{
	private FlowLayout fl = new FlowLayout();
	private JMenuBar menuBar;
	private JMenu menuFile, menuEdit;
	private JMenuItem closeMenuItem, printMenuItem, showTableMItem, addRecordMItem;

    private JFrame frame;
	private JScrollPane scrollPane;
	private JPanel panel;
	
	
	public MenuBar(JFrame theFrame)
	{

		this.frame = theFrame;
	
		menuFile = new JMenu("File");           // menu file
		menuFile.setForeground(java.awt.Color.black);
		closeMenuItem = new JMenuItem("Close"); // menu closeItem
		menuFile.add(closeMenuItem);            // menu file with closeItem
		closeMenuItem.setAccelerator(KeyStroke.getKeyStroke('q', java.awt.Event.CTRL_MASK, false));
		closeMenuItem.addActionListener(new closeMenuHandler());
	
	
		printMenuItem = new JMenuItem("Print");  // menu printItem
		menuFile.add(printMenuItem);            // menu file with printItem
		printMenuItem.setAccelerator(KeyStroke.getKeyStroke('p', java.awt.Event.CTRL_MASK, false));
		////////////// ActionListener /////////////
	
		
		
		//*****************************************************************************
		//////////// Menu Bar Edit items /////////////////
		menuEdit= new JMenu("Edit");          // menu edit
		showTableMItem = new JMenuItem("Show Table");  // showTable item 
		menuEdit.add(showTableMItem);                   // menu edit with showTable item
		showTableMItem.addActionListener(new ShowTable(frame));
	
		//*****************************************************************************
	
	
		addRecordMItem = new JMenuItem("Add Record");  // addRecord item
		menuEdit.add(addRecordMItem);					// edit menu with addRecord item 
		addRecordMItem.addActionListener(new AddRecord(frame));
		///////////////////////////////////////
	
		//////// Add all menu things to the menu bar /////////
		menuBar = new JMenuBar();    // menu bar
		menuBar.setBackground(java.awt.Color.white);
		menuBar.add(menuFile);		// menu bar with menu file
		menuBar.add(menuEdit);		// menu bar with menu edit
		
		
		
		setLayout(fl);
		fl.setAlignment(FlowLayout.LEFT);
		
		add(menuBar);
		setVisible(true);
		
	  
	}// end Constructor
	
	
	
}


class closeMenuHandler implements ActionListener
{
	public void actionPerformed(ActionEvent ae)
	{
		System.exit(0);
		
	}
}
/// and the Jpanel class which blocks my dropped down// 
import java.sql.*;
import java.text.MessageFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class AddRecord implements ActionListener
{
      // DataHolder is another sql class to get all the data from mdb file
	private DataHolder dh = new DataHolder();
	private JFrame frame; 
	private JPanel panel; 
	private JTextField idField, fnField, lnField, phoneField, addField, cityField, stateField, zipField, 
	                   sexField, ageField, hourlyField, hoursField;
	private JButton saveButton, cancelButton;
	private int id, age;
	private String fn=null, ln=null, phone=null, add=null, city=null, state=null, zip=null, sex=null;
	private double hourly, hours;
	
	public AddRecord(JFrame frame)
	{
		this.frame = frame;
		GridLayout gl = new GridLayout(13,2);
		panel = new JPanel();
		panel.setBackground(java.awt.Color.white);
		panel.setOpaque(false);
		panel.setLayout(gl);
		panel.setSize(500, 500);
		
		///////////      the labels and fields ////////////// 
		    panel.add(new Label("Employee Serial #:"));
		    idField = new JTextField();
		    panel.add(idField);
		    panel.add(new Label("First Name:"));
		    fnField = new JTextField();
		    panel.add(fnField);
		    panel.add(new Label("Last Name:"));
		    lnField = new JTextField();
		    panel.add(lnField);
		    panel.add(new Label("Telephone:"));
		    phoneField = new JTextField();
		    panel.add(phoneField);
		    panel.add(new Label("Address:"));
		    addField = new JTextField();
		    panel.add(addField);
		    panel.add(new Label("City:"));
		    cityField = new JTextField();
		    panel.add(cityField);
		    panel.add(new Label("State:"));
		    stateField = new JTextField();
		    panel.add(stateField);
		    panel.add(new Label("Zip Code:"));
		    zipField = new JTextField();
		    panel.add(zipField);
		    panel.add(new Label("Sex:"));
		    sexField = new JTextField();
		    panel.add(sexField);
		    panel.add(new Label("Age:"));
		    ageField = new JTextField();
		    panel.add(ageField);
		    panel.add(new Label("Hourly Rate:"));
		    hourlyField = new JTextField();
		    panel.add(hourlyField);
		    panel.add(new Label("Hours per week:"));
		    hoursField = new JTextField();
		    panel.add(hoursField);
		    
	    //////////////   the Labels and Fields ////////////////////
		    
	    saveButton = new JButton("Save");
	    saveButton.addActionListener(this);
	    panel.add(saveButton);
	    
	    cancelButton = new JButton("Cancel");
	    cancelButton.addActionListener(this);
	    panel.add(cancelButton);
	    //panel.setOpaque(false);
	    
	}// end AddRecord Constructor
	
	
	
	
	public void actionPerformed(ActionEvent e)
	{
		boolean status= true;
		frame.getContentPane().remove(1);
		frame.add(panel);
		frame.setBounds(400, 400, 500, 300);
	    frame.setVisible(true);
	    frame.repaint();
	
	    //////////////////// another action perform ////////////////////

		if(e.getSource() == saveButton)
		{
			if(idField.getText().equals(""))
			{
				JOptionPane.showMessageDialog(null, "Employee ID must be entered!");
				status=false;
			}
			
			else
			{
				
				id = new Integer(idField.getText());
				fn = fnField.getText();
				ln = lnField.getText();
				phone = phoneField.getText();
				add = addField.getText();
				city= cityField.getText();
				state = stateField.getText();
				zip = zipField.getText();
				sex = sexField.getText();
				
			
				if(ageField.getText().equals(""))
				{
					age=0;
				}
			
				else
				{
					age = new Integer(ageField.getText());
				}
			
				if(hourlyField.getText().equals(""))
				{
					hourly= 0;
				}
			
				else
				{
					hourly = new Double(hourlyField.getText());
				}
			
				if(hoursField.getText().equals(""))
				{
					hours=0;
				}
			
				else
				{
					hours = new Double(hoursField.getText());
				}
			}// end idField has something
			
			
			//fieldReset();
			idField.setText("");
			fnField.setText("");
			lnField.setText("");
			phoneField.setText("");
			addField.setText("");
			cityField.setText("");
			stateField.setText("");
			zipField.setText("");
			sexField.setText("");
			ageField.setText("");
			hourlyField.setText("");
			hoursField.setText("");
			idField.requestFocus();
			
			if(status == true)
			{
				dh.addRecord(id, fn, ln, phone, add, city, state, zip, age, sex, hourly, hours);
				JOptionPane.showMessageDialog(null, "The new record has been saved!");
			}
		}// ENN SAVE BUTTON
		
		
		else if(e.getSource()== cancelButton)
		{
			frame.remove(panel);
		}
	
	/////////////// end of another action perform /////////////////////
	
	
	}  // end ActionPerformed method

}// end class

I haven't found out the cause of your problem. . I did look through your code, and there are certainly some things I thought should be changed (minor issues).

1. Why are you adding things explicitly to the JFrame, instead of putting them in a panel, then adding the panel to the JFrame? (I'm referring to the text that shows up when you first start the program). I'm not positive that this is bad practice, but I would do it with a panel.
2. I also think the reason the menu isn't showing up might be that you are adding your items directly to the frame, rather than to a panel. Add a panel to the frame, then add everything else to that panel. . so your AddRecord would be in the panel.

You'd have

JFrame
|
PanelWDesiredLayout
|
AddRecordPanel

huh, that is what it is though. i have a JMenuBar directly added to the JFrame, and then everything else is added to the JPanel or JScrollPane, then add the JPanel or JScrollPane to the Frame.

I just quickly glanced through the code, so perhaps I missed something, but it looks like you've added a JMenuBar to a JPanel and added that to your JFrame. I would recommend using setJMenuBar() method on JFrame to add the menu to the frame.

^ I tried that when I edited his code, it didn't change anything (as far as the problem he mentioned). Good catch though, I forgot to add that to my comments.

edit:

And k2k, I'm not sure if it matters or not, but you didn't add everything to panels. You added your two labels directly to the frame. If I were you though, I would try out some different layout managers. Like I said, I think CardLayout would be a good idea considering you're flipping through different same-sized windows.

Ok, I had a few minutes to play with the code. The main problem is that you are using java.awt.Label instead of javax.swing.JLabel for the labels in AddRecord. It' not a good idea to mix heavyweight AWT components with Swing components in the same UI. That was causing the problems with painting your menu properly.

Here's some revised code that behaves properly. Note that I put some of the separate classes inside "TheFrame" as inner classes just for my own convenience. You can certainly move them back out.

////// main , the frame ///////
import java.text.MessageFormat;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;

public class TheFrame extends JFrame
{
	private JPanel menuPanel;
	private JPanel text;
	private BorderLayout bl = new BorderLayout();


	public TheFrame()
	{

		JLabel one = new JLabel("Java Final Project: Employee Record");
		one.setAlignmentX(0.5f);
		one.setAlignmentY(1f);
		JLabel two = new JLabel("Written by Henry Li");
		two.setAlignmentX(0.5f);
		two.setAlignmentY(1f);

		text = new JPanel();
		text.setBackground(java.awt.Color.white);
		LayoutManager boxLayout = new BoxLayout(text, BoxLayout.Y_AXIS);

		text.setLayout(boxLayout);
		text.add(one);
		text.add(two);

        // add the menu
        createMenu();
        // set close op also
        setDefaultCloseOperation(EXIT_ON_CLOSE);
// don't need
//		add(menuPanel, BorderLayout.NORTH);
		getContentPane().add(text, BorderLayout.CENTER);
		validate();

		setBounds(400, 400, 600, 300);
		setVisible(true);

	}

    // Moved everything from menu panel here
    private void createMenu(){
	 JMenuBar menuBar;
	 JMenu menuFile, menuEdit;
	 JMenuItem closeMenuItem, printMenuItem, showTableMItem, addRecordMItem;

		menuFile = new JMenu("File");           // menu file
		menuFile.setForeground(java.awt.Color.black);
		closeMenuItem = new JMenuItem("Close"); // menu closeItem
		menuFile.add(closeMenuItem);            // menu file with closeItem
		closeMenuItem.setAccelerator(KeyStroke.getKeyStroke('q', java.awt.Event.CTRL_MASK, false));
		closeMenuItem.addActionListener(new closeMenuHandler());


		printMenuItem = new JMenuItem("Print");  // menu printItem
		menuFile.add(printMenuItem);            // menu file with printItem
		printMenuItem.setAccelerator(KeyStroke.getKeyStroke('p', java.awt.Event.CTRL_MASK, false));
		////////////// ActionListener /////////////



		//*****************************************************************************
		//////////// Menu Bar Edit items /////////////////
		menuEdit= new JMenu("Edit");          // menu edit
		showTableMItem = new JMenuItem("Show Table");  // showTable item
		menuEdit.add(showTableMItem);                   // menu edit with showTable item
// I didn't have this class - you'll want to re-enable this I imagine
//		showTableMItem.addActionListener(new ShowTable(frame));

		//*****************************************************************************


		addRecordMItem = new JMenuItem("Add Record");  // addRecord item
		menuEdit.add(addRecordMItem);					// edit menu with addRecord item
		addRecordMItem.addActionListener(new AddRecord(this));
		///////////////////////////////////////

		//////// Add all menu things to the menu bar /////////
		menuBar = new JMenuBar();    // menu bar
		menuBar.setBackground(java.awt.Color.white);
		menuBar.add(menuFile);		// menu bar with menu file
		menuBar.add(menuEdit);		// menu bar with menu edit

        // set this as the frame's menu
        setJMenuBar(menuBar);
    }



	public static void main(String arg[]) throws SQLException
	{
		TheFrame frame = new TheFrame();
	}


}// end the Class TheFrame

// removed this entirely
 class MenuBar extends JPanel {}

class closeMenuHandler implements ActionListener
{
	public void actionPerformed(ActionEvent ae)
	{
		System.exit(0);

	}
}

/// and the Jpanel class which blocks my dropped down//
class AddRecord implements ActionListener
{
// didn't have this class
      // DataHolder is another sql class to get all the data from mdb file
//	private DataHolder dh = new DataHolder();
	private JFrame frame;
	private JPanel panel;
	private JTextField idField, fnField, lnField, phoneField, addField, cityField, stateField, zipField,
	                   sexField, ageField, hourlyField, hoursField;
	private JButton saveButton, cancelButton;
	private int id, age;
	private String fn=null, ln=null, phone=null, add=null, city=null, state=null, zip=null, sex=null;
	private double hourly, hours;

	public AddRecord(JFrame frame)
	{
		this.frame = frame;
		GridLayout gl = new GridLayout(13,2);
		panel = new JPanel();
		panel.setBackground(java.awt.Color.white);
//		panel.setOpaque(false);  // no need
		panel.setLayout(gl);
		panel.setSize(500, 500);

		///////////      the labels and fields //////////////

               // NOTE THE JLabel CHANGE 
		    panel.add(new JLabel("Employee Serial #:"));
		    idField = new JTextField();
		    panel.add(idField);
		    panel.add(new JLabel("First Name:"));
		    fnField = new JTextField();
		    panel.add(fnField);
		    panel.add(new JLabel("Last Name:"));
		    lnField = new JTextField();
		    panel.add(lnField);
		    panel.add(new JLabel("Telephone:"));
		    phoneField = new JTextField();
		    panel.add(phoneField);
		    panel.add(new JLabel("Address:"));
		    addField = new JTextField();
		    panel.add(addField);
		    panel.add(new JLabel("City:"));
		    cityField = new JTextField();
		    panel.add(cityField);
		    panel.add(new JLabel("State:"));
		    stateField = new JTextField();
		    panel.add(stateField);
		    panel.add(new JLabel("Zip Code:"));
		    zipField = new JTextField();
		    panel.add(zipField);
		    panel.add(new JLabel("Sex:"));
		    sexField = new JTextField();
		    panel.add(sexField);
		    panel.add(new JLabel("Age:"));
		    ageField = new JTextField();
		    panel.add(ageField);
		    panel.add(new JLabel("Hourly Rate:"));
		    hourlyField = new JTextField();
		    panel.add(hourlyField);
		    panel.add(new JLabel("Hours per week:"));
		    hoursField = new JTextField();
		    panel.add(hoursField);

	    //////////////   the Labels and Fields ////////////////////

	    saveButton = new JButton("Save");
	    saveButton.addActionListener(this);
	    panel.add(saveButton);

	    cancelButton = new JButton("Cancel");
	    cancelButton.addActionListener(this);
	    panel.add(cancelButton);
	    //panel.setOpaque(false);

	}// end AddRecord Constructor




	public void actionPerformed(ActionEvent e)
	{
		boolean status= true;
        // add components to content pane
		frame.getContentPane().removeAll();
		frame.getContentPane().add(panel);
		frame.setBounds(400, 400, 500, 300);
	    frame.pack();
	    frame.repaint();

	    //////////////////// another action perform ////////////////////

		if(e.getSource() == saveButton)
		{
			if(idField.getText().equals(""))
			{
				JOptionPane.showMessageDialog(null, "Employee ID must be entered!");
				status=false;
			}

			else
			{

				id = new Integer(idField.getText());
				fn = fnField.getText();
				ln = lnField.getText();
				phone = phoneField.getText();
				add = addField.getText();
				city= cityField.getText();
				state = stateField.getText();
				zip = zipField.getText();
				sex = sexField.getText();


				if(ageField.getText().equals(""))
				{
					age=0;
				}

				else
				{
					age = new Integer(ageField.getText());
				}

				if(hourlyField.getText().equals(""))
				{
					hourly= 0;
				}

				else
				{
					hourly = new Double(hourlyField.getText());
				}

				if(hoursField.getText().equals(""))
				{
					hours=0;
				}

				else
				{
					hours = new Double(hoursField.getText());
				}
			}// end idField has something


			//fieldReset();
			idField.setText("");
			fnField.setText("");
			lnField.setText("");
			phoneField.setText("");
			addField.setText("");
			cityField.setText("");
			stateField.setText("");
			zipField.setText("");
			sexField.setText("");
			ageField.setText("");
			hourlyField.setText("");
			hoursField.setText("");
			idField.requestFocus();

			if(status == true)
			{
//				dh.addRecord(id, fn, ln, phone, add, city, state, zip, age, sex, hourly, hours);
				JOptionPane.showMessageDialog(null, "The new record has been saved!");
			}
		}// ENN SAVE BUTTON


		else if(e.getSource()== cancelButton)
		{
			frame.remove(panel);
		}

	/////////////// end of another action perform /////////////////////


	}  // end ActionPerformed method

}// end class
commented: Noticing a 'J' missing on JLabel is good stuff. Nice. +3
commented: Nicely done +29

Ezzaral,
thanks very much. I didn't mean to user Label instead of JLabel, it was just something I did carelessly. and I didn't know it would cause this problem took me that long to figure it out.

yea, IT works now.. thanksssss !!!


BestJewSinceJC,
thanks again for the help and opinions. thankssssss

wow, Ezzaral, that is a pretty amazing catch. I was going to fool with the code a little more tonight, but. . unnecessary now. Doubt I would have ever noticed that anyway.

It did take a little fiddling around with it before suspecting the AWT Label thing. The behavior was just odd enough to suspect some mixing of AWT and Swing components and sure enough he was using Label. The missing "J" certainly didn't just jump right out of all that code :)

Yeah, there is a difference between finding it by accident and looking for it. Although I have heard not to mix the two before (perhaps on this forum?) it is hardly ingrained in my mind, therefore I probably never would have seen it.

yea, that was a careless mistake but i think i would never find out coz i never knew the awt and swing don't get along.

Ezzaral is really good, he solved at least 3 of my threads in here i think. (those hard one no one was able to solve)

i took both of your suggestions. i used the setJMenuBar() for my menubar and i do put things on the JPane, i didn't know that method before,, good that you guys point it out and that is another extra thing i learned from this.

and thankssssssssss,,, i m glad you guys gave the hands.

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.