Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see several problems with that code:</p> <ul> <li><p>MTGSAMPServerReference is called two times, one in the main, and another by <code>createAndShowGUI()</code>.</p></li> <li><p>MTGSAMPServerReference extends JFrame, but createAndShowGUI() contains a local variable <code>JFrame f</code> too. Esentially you are assigning <code>this</code> to a local variable <code>f</code>, which serves no purpose.</p></li> <li><p>To initialize the GUI you execute the constructor and <code>createAndShowGUI()</code>, after that all the code that can be executed is due to events. In your case in the <code>actionPerformed()</code> method. If you want to remove the components inside the frame, you have to do the proper code there (or at some point called from there).</p></li> <li><p>As stated by your link, to remove all components you have to execute <code>removeAll()</code> on the contentPane of the JFrame, obtained with <code>getContentPane()</code>.</p></li> <li><p>I don't know what you intend to do with those comparations <code>if("Main".equals(selectionMenu))</code>. Again that is initialization code, it's ran once. There you build all the GUI. The variable <code>selectionMenu</code> may change in the future, but that doesn't retroactively execute that code. If you want to do anything using the new value of <code>selectionMenu</code>, do it on the actionListener.</p></li> </ul> <p>Here's a working modification of your code.</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class MTGSAMPServerReference extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private static JList list1; private static JButton select1; public static String selectionMenu = "Main"; //accomplishes nothing public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { MTGSAMPServerReference gui = new MTGSAMPServerReference(); gui.createAndShowGUI(); } }); } public void createAndShowGUI() { pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //f.add(new drawOnPanel()); setSize(1200, 800); setLocationRelativeTo(null); list1.setSize(250, 250); list1.setLocation(0, 0); select1.setSize(75, 25); select1.setLocation(251, 276); setVisible(true); } public MTGSAMPServerReference() { this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING)); Object[]mainMenu = {"Vehicles", "Bikes/Bicycles", "Boats", "Houses", "Businesses", "Objects", "Jobs", "Ranks", "Licenses", "VIP", "FAQ's"}; Object[]VehiclesValueMenu = {"Lower Class", "Upper Class", "VIP"}; JPanel controls = new JPanel(new BorderLayout(5,5)); list1 = new JList&lt;Object&gt;(mainMenu); list1.setVisibleRowCount(10); select1 = new JButton("Select"); select1.addActionListener(this); controls.add(new JScrollPane(list1)); controls.add(select1, BorderLayout.PAGE_END); controls.setBorder(new EmptyBorder(25,25,0,0)); add(controls); //revalidate(); //uneeded at this point the JFrame is not yet visible, thus nothing to repaint //repaint(); } @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Select")) { int indexMain = list1.getSelectedIndex(); System.out.println("Index Selected: " + indexMain); String valueMain = (String) list1.getSelectedValue(); System.out.println("Value Selected: " + valueMain); if ("Vehicles".equals(valueMain)) { System.out.println("Menu selected: " + selectionMenu); getContentPane().removeAll(); //equivalent to this.getContentPane().removeAll(); revalidate(); repaint(); } } } } </code></pre> <p>Also I've erased the modifications of the variable <code>selectionMenu</code> it doesn't really do anything now.</p> <p>As stated by others, you probably want to use another Layout at some point. Which one depends on what else you want on the JFrame.</p> <p>Cheers.</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.
 

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