+ 1

Java JFrame Components

Hi All, I've created a JFrame program and now I'm attempting to make a component hidden and then reveal it during the course of the program when the user presses a button. I declared the label as setVisible(false) in the initialisation of the components (the label is also set as private) and then set it as setVisible(true) when the user clicks on the button, however this does not work for some reason. Additionally, if I try to set the label as empty and then use .setText(), it still does not work. The part of the program where I try to set it as setVisible(true) is below. Please could you help me and tell me what I am doing wrong. Thank you for your time. private void btnScanActionPerformed(java.awt.event.ActionEvent evt) { lbl.setVisible(true); scanning(); }

25th Jul 2022, 12:34 PM
Michael
2 Answers
+ 1
Also try refreshing the component with revalidate and repaint. Example if your class extends jpanel or jframe: btnScan.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { lbl.setVisible(true); scanning(); // What is it doing ??? revalidate(); repaint(); } } ); You also can use lambda => btnScan.addActionListener( e -> { lbl.setVisible(true); scanning(); revalidate(); repaint(); }) You can also add the element on click (implies not adding it before) btnScan.addActionListener( e -> { this.add(lbl); scanning(); revalidate(); repaint(); })
26th Jul 2022, 5:28 AM
Roland
Roland - avatar
0
usually, it seems like btnScan.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event) { lbl.setVisible(true); scanning(); } });
25th Jul 2022, 10:58 PM
zemiak