Java GUI Clear Panel To Blank Image - java

Quick question, couldn't find a definitive answer from searching so I thought I'd ask here. In my code I set a JLabel to an image and I would like that image to clear and disappear after I perform a certain action such as clicking a button.
JLabel one = new JLabel(ruby); //ruby is a seperate ImageIcon already defined.
I'm not sure what the command to clear the panel is, an answer would be appreciated! Thanks.

Assuming that the label is already on the screen and you have a reference to the label you should be able to either call remove(label) on the parent container that contains the label or label.setIcon(null) if you want to reuse the label. In either case, you may be required to call revalidate on the parent container

Related

How to add another button using JButton, without resizing the frame to make it visible?

I'm still new at Java and having trouble at adding buttons using another JButton, the problem is that I can able to add them but right after I resize the form only. It means that the frame did not shown any JButton added to the panel unless I resize it.
right after I posted it, they already recommended related topics here and it was resolved now thanks!
just need to add 'revalidate();' at the component for it to dynamically change along the actionListener.

How to make JTextFields clickable?

I posted a similar question a year ago, but it was not really well written and I didn't get an answer I could work with. Now I stand in front of the same problem. I got a JPanel (my content pane), where a MouseListener is implemented.
Everywhere I click, I get the exact coordinates of my mouse click. Except my JTextField components. When I click on those, the MouseEvent isn't even triggered. H
ow do I do this, so my mouse clicks on those will also call the mouse event?
Tried: setEnable(false) and setHighlighter(null)
Sorry thought I fixed the X/Y problem.
The X/Y problem simply means you are telling us what your attempted solution is without telling us what your requirement is. We can't suggest a different approach if we don't know what you are trying to do.
I want to open a menu,
Now we know what the requirement is.
The solution is to add the MouseListener to the text field, not the panel. If you have the same popup of the panel and the text field, then you still need to add the listener to both the panel and the text field.
You can do this in one of two ways:
Read the section from the Swing tutorial on Bringing up a Popup Menu for a working example.
Note the above tutorial is a little old, you can also check out the setComonentPopuMenu(...) method of the JComponent class. This approach will create the listener for you.

Java Swing - How do I add an appearing effect for JLabel?

I have to create a menu bar with a logo panel at far end of the bar and I have done it. But It's required that logo must have "appearing" effect? How can I do it?
I'm not exactly sure what you mean with "appearring effect", but if you want to create some color effects, you could extend the JLabel class and add some custom Color/Image Effects. Maybe this example for creating a JLabel with various effects gets you started.
See also How to animate a JLabel

setting JLabel transparency

You may see this as a duplicate question but please here me out.
I have a JLabel with an image. This JLabel has a mouse listener binded to some function.
Is there any way to make the JLabel disappear (or technically, transparent) in order for the mouse listener function to be preserved?
I know I can set a transparent image as an icon to the JLabel, but I'm wondering if there's some kind of "toggle" function out there.
you could use yourLabel.setVisible(false); is not the same as dispose the frame; however, would you extend your question a little bit more or make it clearer?(I'm having a hard time trying to understand what do you want to do)

Java Swing Scrollpane in NetBeans

I have Java application which adds JTextFields # runtime to JPanel. Basically user clicks a button and new JTextField is added, clicks again added again...
Each new JTextField is directly below the previous one. Obviously I run out of space pretty soon so I'm trying to use JScrollPane and thats where the hell begins, because it just doesnt work no matter what I try.
Right click on JPanel and Enclose in Scroll Pane. Didnt work.
After reading some examples I realized I must have JPanel as an argument for JScrollPane constructor. Which I did via right clicking on ScrollPane and CustomizeCode. Because apparently auto-generated code is protected in NetBeans and I cannot just change all those declarations, etc. manually. Still doesnt work.
I did try to set PreferedSize to null for JPanel and/or JScrollPane, didnt help.
JScrollPane is a child of lets call it TabJPanel (which in turn is a tab of TabbedPane). I tried to mess with their relationships, basically trying every possible way of parentship between JFrame, JPanel(holding textfields), TabJPanel and JScrollPane, but nothing worked.
I also made VerticalScrollBar "always visible" just in a case. So I see the scrollbar, it's just that populating that JPanel with JTextFields does not affect it.
When there are too many JTextFields I they go "below" the bottom border of JPanel and I cannot see them anymore.
Code for adding new JTextFields is like this, in a case it's relevant.
JTextField newField = new JTextField( columns );
Rectangle coordinates = previousTextField.getBounds();
newField.setBounds(coordinates.x , coordinates.y + 50, coordinates.width, coordinates.height);
JPanel.add(newField);
JPanel.revalidate();
JPanel.repaint();
Sorry for a long post I'm just trying to provide as much info as possible, because being newbie I dont know whats exactly relevant and whats not. Thanks in advance :)
As there is another answer now, I'm adding my suggestion too.
This sounds exactly like a problem to use a JTable with a single column. JList is not yet editable (and might never be).
JTable would handle the layout problems for you, and you can easily access the values via the table.
Use your own TableModel (a simple Vector should be sufficient in your case), and add values to it.
An option you have is to utilize a LayoutManager, instead of setting the bounds directly on the components. To test this, a simple single column GridLayout with the alignment set to vertical should prove the concept.
panel.setLayout(new GridLayout(0,1));
zero in the rows param allows for rows to be added to the layout as needed.
I do this way to add a scrollpane, create a panel and fill it with few components, then create a scrollpane in the component you want to add it, cut and paste the panel in which all your details will fall in and resize the scrollpane.Because the components take a larger space than the one visible right click on the scrollpane and select design this container, there you can increase the size of the scrollpane and add as many components as you have.

Categories