• 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

JMenuBar reference being set to null!

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Steven Ostrowski
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic