Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to remove all components from a JFrame with removeAll()?
    primarykey
    data
    text
    <p>So I want to have everything removed from my <code>JFrame</code> when a string equals a certain value, but when I call <code>removeAll();</code> followed by <code>revalidate();</code> and <code>repaint();</code>, it doesn't change anything.</p> <p>I have tried calling <code>getContentPane.removeAll();</code> as directed <a href="https://stackoverflow.com/questions/9347076/how-to-remove-all-components-from-a-jframe-in-java">here</a>, but that did not do anything.</p> <p>My code is as follows: </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"; 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"}; if ("Main".equals(selectionMenu)) { 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(); repaint(); } if ("VehiclesValue".equals(selectionMenu)) { removeAll(); revalidate(); repaint(); } } @Override public void actionPerformed(ActionEvent e) { if ("Main".equals(selectionMenu)) { 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)) { selectionMenu = "VehiclesValue"; System.out.println("Menu selected: " + selectionMenu); revalidate(); repaint(); } } } } public void createAndShowGUI() { JFrame f = new MTGSAMPServerReference(); f.pack(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //f.add(new drawOnPanel()); f.setSize(1200, 800); f.setLocationRelativeTo(null); list1.setSize(250, 250); list1.setLocation(0, 0); select1.setSize(75, 25); select1.setLocation(251, 276); MTGSAMPServerReference.this.repaint(); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { MTGSAMPServerReference gui = new MTGSAMPServerReference(); gui.createAndShowGUI(); } }); } } </code></pre> <p>I have done my research, but can't figure out what it is that I am doing wrong.</p> <p>And if I try changing my <code>JFrame f to a Global Variable instead of a Local Variable</code>, it doesn't display anything to begin with.</p> <p>And yes, I know I am <code>mixing Commmand Line with GUI</code>, but that is <code>only for debugging purposes</code>. When I am finished, I will simply remove everything <code>Command Line related</code>.</p> <p>Anyways, any ideas on my problem?</p> <p>And thanks in advance!</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.
 

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