Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>@Steffen Kuehn please check out the below code which presents the 'possible' problem you are experiencing. i.e. you are not revalidating and repainting the panel which was changed.</p> <p>Please notice when you click the panel using left mouse button there is no repainting/revalidation, and at first it seems nothing is happening. Try to resize the frame. It calls the operations and you will see that the elements 5 &amp; 6 are in fact visible. Thus if you want the changes to be instant use the right click which does visibility change and validation.</p> <pre><code> import java.awt.Color; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class FlowComponentsTest { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { final JPanel p = new JPanel(); for(int i = 1; i &lt;= 20; i++) { JComponent c = new JTextField("I am element no. " + i); //so any textfield will not steal the focus from the panel p c.setFocusable(false); p.add(c); if(i == 5 || i == 6) { c.setForeground(Color.GREEN); c.setVisible(false); } } JFrame f = new JFrame(); f.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { JComponent c5 = (JComponent) p.getComponent(4); JComponent c6 = (JComponent) p.getComponent(5); c5.setVisible(!c5.isVisible()); c6.setVisible(!c6.isVisible()); if(e.getButton() == MouseEvent.BUTTON3) { System.out.println("Right click"); p.revalidate(); p.repaint(); } } }); f.setContentPane(p); f.setSize(300, 330); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }); } } </code></pre>
 

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