Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Seems like you yourself are doing something wrong, in your code. Without a proper <a href="http://sscce.org/" rel="nofollow noreferrer">SSCCE</a> it's hard to say what thing you doing wrong, since it works perfectly, using same ActionListener for both <code>JMenuItem</code> and <code>JTextField</code>.</p> <p>Here is a sample program to match with yours : </p> <pre><code>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(); } }); } } </code></pre> <p>And here is the output in both the cases : </p> <p><img src="https://i.stack.imgur.com/lWtZa.png" alt="Updated with JMenuItem"> and <img src="https://i.stack.imgur.com/8CnZ8.png" alt="Updated with JTextField"></p> <p>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.</p> <p><strong>LATEST EDIT</strong></p> <p>It seems to me that <code>CreateGalleryXML</code> extends <code>JPanel</code> by the look of it. See at <code>Line 3</code> of this below code taken from your update, here you are initializing a new <code>Object</code> of <code>CreateGalleryXML</code> inside, when you already had one Object <code>window</code> created at <code>Line 1</code>: </p> <pre><code>CreateGalleryXML window = new CreateGalleryXML(); frame.setJMenuBar(window.createMenuBar()); frame.add(new CreateGalleryXML()); </code></pre> <p>So try to change the above thingy to this </p> <pre><code>CreateGalleryXML window = new CreateGalleryXML(); frame.setJMenuBar(window.createMenuBar()); frame.add(window); </code></pre> <p>and see what happens and Please do revert back again :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload