Search code examples
javaswingjlabel

I have a JLabel that doesn't want to update


I've looked at other JLabel threads and though similar, a few just don't seem to apply to what I'm experiencing. First, I want to say I am a novice when it comes to Java. Next, I am trying to follow tutorials and demos at the docs.oracle.com site. Now, I can update the label when I type something into a JTextField and there is an ActionListener on that...

But I also have a Menu, and when I select a Menu Item, that Action does not want to update the label.

Questions -

  1. How do I have action listeners on both JTextFields and JMenuItems? Are there two ActionEvent methods or do I use one method and somehow identify each type of action?
  2. If I use the same basic code in the JTextField ActionEvent and JMenuItem ActionEvent, the JLabel updates correctly with the JTextField event but not JMenuItem event. They both use the messageField.setText property. Could the JMenuItem action be doing something to block the setText?

I can easily copy code in here, but it's pretty spaghetti-like at the moment, so if you want to see anything, let me know specifically and I'll post it.

I would appreciate any assistance that anyone would be able to provide.

---edit--- Wow!! Thanks for all of the comments and suggestions.

1 - I know it has to be my code. As I mentioned, I am really just cobbling stuff together from demos and tutorials, and trying to learn Java along the way. I've just never gotten the hang of object oriented.... 2 - I do know the individual Listeners are working. I'm using System.out.println to validate, as well as inspecting those labels in debug mode to see they have indeed changed.
3 - I will look at the various links and code posted here and see if I can figure out what's wrong with my code.

Really, thanks again!

---edit---

Here is what I originally had in my createAndShowGUI method....

private static void createAndShowGUI()   
{  
    JFrame frame = new JFrame("Create XML for Photo Gallery");  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

    CreateGalleryXML window = new CreateGalleryXML();  
    frame.setJMenuBar(window.createMenuBar());  
    frame.add(new CreateGalleryXML());  

    frame.pack();  
    frame.setVisible(true);  
}  

Solution

  • Seems like you yourself are doing something wrong, in your code. Without a proper SSCCE it's hard to say what thing you doing wrong, since it works perfectly, using same ActionListener for both JMenuItem and JTextField.

    Here is a sample program to match with yours :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class UpdateLabel
    {
        private JLabel label;
        private String labelText;
    
        private ActionListener action = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                setLabelText((String) ae.getActionCommand());
                label.setText(getLabelText());
            }
        };
    
        private void setLabelText(String text)
        {
            labelText = text;
        }
    
        private String getLabelText()
        {
            return labelText;
        }
    
        private void createAndDisplayGUI()
        {
            final JFrame frame = new JFrame("Update Label");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setLocationByPlatform(true);
    
            JMenuBar menuBar = new JMenuBar();
            JMenu programMenu = new JMenu("Program");
            JMenuItem exitMenuItem = new JMenuItem("Exit");
            exitMenuItem.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    frame.dispose();
                }
            });
            JMenu labelMenu = new JMenu("Label");
            JMenuItem updateMenuItem = new JMenuItem("Update Label");
            updateMenuItem.setActionCommand("Updated by JMenuItem");
            updateMenuItem.addActionListener(action);
    
            programMenu.add(exitMenuItem);
            labelMenu.add(updateMenuItem);
            menuBar.add(programMenu);
            menuBar.add(labelMenu);
    
            frame.setJMenuBar(menuBar);
    
            JPanel contentPane = new JPanel();
    
            label = new JLabel("I am the LABEL which will be updated!!");
            contentPane.add(label);
    
            JTextField tfield = new JTextField(10);
            tfield.setActionCommand("Updated by JTextField");
            tfield.addActionListener(action);
    
            frame.add(contentPane, BorderLayout.CENTER);
            frame.add(tfield, BorderLayout.PAGE_END);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new UpdateLabel().createAndDisplayGUI();
                }
            });
        }
    }
    

    And here is the output in both the cases :

    Updated with JMenuItem and Updated with JTextField

    Do check out the main method, might be you had failed to put your code inside EDT - Event Dispatcher Thread, that can lead to such issues too. All updates on the Swing GUI, must be done on the Event Dispatcher Thread.

    LATEST EDIT

    It seems to me that CreateGalleryXML extends JPanel by the look of it. See at Line 3 of this below code taken from your update, here you are initializing a new Object of CreateGalleryXML inside, when you already had one Object window created at Line 1:

    CreateGalleryXML window = new CreateGalleryXML();  
    frame.setJMenuBar(window.createMenuBar());  
    frame.add(new CreateGalleryXML());
    

    So try to change the above thingy to this

    CreateGalleryXML window = new CreateGalleryXML();  
    frame.setJMenuBar(window.createMenuBar());  
    frame.add(window);
    

    and see what happens and Please do revert back again :-)