*
The moose likes Swing / AWT / SWT and the fly likes JMenuBar reference being set to null! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JMenuBar reference being set to null!" Watch "JMenuBar reference being set to null!" New topic
Author

JMenuBar reference being set to null!

Steven Ostrowski
Greenhorn

Joined: Jul 31, 2002
Posts: 22
Hello,
I'm building up a GUI and have noticed a very weird thing happening. I instantiate a JMenuBar but do nothing with the reference until the end of the init method. Before the end of that method is reached, however, the menubar reference gets set back to null when I try to add a JPanel to the JTabbedPane. I can't understand why/how Java would let such a method set a non-connected reference to null. Any help would be appreciated. Here's the initComponents() method. It is mostly auto-generated by Netbeans, although I did add some custom pre/post-init code generation in there. Still, note how the mainMenuBar reference is not used. It is set to null on the first call to subSystemPane.addTab:
private void initComponents() {//GEN-BEGIN:initComponents
subSystemPane = new javax.swing.JTabbedPane();
exerciseManagementPanel = new javax.swing.JPanel();
exerciseNameLabel = new javax.swing.JLabel();
exerciseNameTextField = new javax.swing.JTextField();
exerciseLoadButton = new javax.swing.JButton();
exerciseSaveButton = new javax.swing.JButton();
exerciseDetailsPanel = new javax.swing.JPanel();
descriptionLabel = new javax.swing.JLabel();
descriptionTextField = new javax.swing.JTextField();
authorLabel = new javax.swing.JLabel();
authorTextField = new javax.swing.JTextField();
objectiveLabel = new javax.swing.JLabel();
objectiveTextField = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
PlanViewDisplay pvd = new PlanViewDisplay();
pvdPanel = pvd.getMainPanel();
mainMenuBar = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
getContentPane().setLayout(new java.awt.FlowLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("ReadiSim GUI");
setBackground(new java.awt.Color(0, 0, 0));
setName("MainFrame");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
subSystemPane.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
subSystemPane.setMaximumSize(new java.awt.Dimension(800, 600));
subSystemPane.addChangeListener(this);
exerciseManagementPanel.setPreferredSize(new java.awt.Dimension(500, 200));
exerciseNameLabel.setText("Exercise Name");
exerciseManagementPanel.add(exerciseNameLabel);
exerciseNameTextField.setColumns(10);
exerciseManagementPanel.add(exerciseNameTextField);
exerciseLoadButton.setText("Load");
exerciseLoadButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exerciseLoadButtonActionPerformed(evt);
}
});
exerciseManagementPanel.add(exerciseLoadButton);
exerciseSaveButton.setText("Save");
exerciseManagementPanel.add(exerciseSaveButton);
exerciseDetailsPanel.setBorder(new javax.swing.border.MatteBorder(null));
exerciseDetailsPanel.setPreferredSize(new java.awt.Dimension(450, 100));
descriptionLabel.setText("Description:");
exerciseDetailsPanel.add(descriptionLabel);
descriptionTextField.setColumns(15);
exerciseDetailsPanel.add(descriptionTextField);
authorLabel.setText("Author:");
exerciseDetailsPanel.add(authorLabel);
authorTextField.setColumns(10);
exerciseDetailsPanel.add(authorTextField);
objectiveLabel.setText("Objective:");
exerciseDetailsPanel.add(objectiveLabel);
objectiveTextField.setColumns(16);
exerciseDetailsPanel.add(objectiveTextField);
exerciseManagementPanel.add(exerciseDetailsPanel);
jButton1.setText("DUMMY CREATE");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
exerciseManagementPanel.add(jButton1);
jButton2.setText("GET HOSTS");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
exerciseManagementPanel.add(jButton2);
subSystemPane.addTab("Exercise Management", exerciseManagementPanel);
subSystemPane.addTab("PVD", pvdPanel);
getContentPane().add(subSystemPane);
jMenu1.setText("Menu");
mainMenuBar.add(jMenu1);
setJMenuBar(mainMenuBar);
pack();
}//GEN-END:initComponents
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Steven,
That is odd. Is it possible that the bean container is doing something with the reference? Have you tried just compiling and running from a command line? If you can remove any IDE dependencies and test it that way, it may give you some more insight as to what's happening.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Steven Ostrowski
Greenhorn

Joined: Jul 31, 2002
Posts: 22
Originally posted by Michael Morris:
Hi Steven,
That is odd. Is it possible that the bean container is doing something with the reference? Have you tried just compiling and running from a command line? If you can remove any IDE dependencies and test it that way, it may give you some more insight as to what's happening.
Hope this helps,
Michael Morris


Yes, I tried compiling the file outside the IDE and running it, but I get the same thing happening.
As an update, I added a line to re-instantiate the menubar after the addTab call, and that made it "work." However, I don't like doing hacks like that.
also - are you able to dynamically swap a JMenuBar? I can't find anything that says you can't, but it appears it's not working.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Steven,
I've never tried changing a JMenuBar dynamically before, but my guess is that it will work. Most things like that do work fine in swing. Just make sure you do it on the swing event thread since swing components are not thread-safe.
I hope you find out what's causing that reference to end up in a blackhole and let the rest of us know. I'm like you in that anytime I put in a "kludge" to make something work knowing that it shouldn't be that way, I can't seem to think about anything else in the program.
Good luck on finding the bug,
Michael Morris
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

The problem I am having debugging your code is the fact that all the code is not posted. I understand that the problem lies in the code you posted, but there doesn't seem to be anything wrong with it. Is you parent container a JFrame or what? Could I possible see at the the part of the code where you instantiate and create the parent Frame that all these components reside in? Then I can Test your code without having to make up the holes you have left out of your post.


GenRocket - Experts at Building Test Data
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
could you refactor your code and make the components declarations inside the initComponents() method? at least the declaration of the menubar. if it's in the scope of the method you can be sure that no other class changes it. if other classes have to act on these components I'm inclined to say that it's bad design.
if you have to change the menubar dynamically you should rather make an encapsulate it with composition inside a class and make all the changes there.
Steven Ostrowski
Greenhorn

Joined: Jul 31, 2002
Posts: 22
Chantal - This code is auto-generated by the Netbeans IDE. I know that's not an excuse to not change it, however if you do change it you lose the ability to graphically manipulate components on their forms.
BTW - if I reinstantiate the menu bar after the tabbedpane addTab method, it does show up, however, I have been unsuccessful still on switching the JMenuBar. I'm going to try some invalidating/validating today. I'm doing that on the stateChanged method of the JTabbedPane listener.
Thanks for the replies!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JMenuBar reference being set to null!