private JButton btnTask = new JButton();
...
TaoGlobal.taskbar.add(btnTask);
How to remove btnTask from JToolBar?
Thanx.
i tried remove, but forgotten for
repaint
Well the general code should be:
panel.remove(...);
panel.revalidate();
panel.repaint();
The revalidate() is important because it tells the panel to layout the components. Your code may work if your are removing the last component, but I doubt is will work when you remove the first component.
JToolBar is a Container, and hence removal can be achieved via toolbar.remove(btnTask).
If you look at that javadoc you'll see other useful methods, like remove(index) and removeAll().
Maybe this would be useful for you:
http://java.sun.com/docs/books/tutorial/uiswing/components/toolbar.html
and
http://java.sun.com/j2se/6/docs/api/javax/swing/JToolBar.html
The last link shows you all the methods that you can use.
Related
As many might know, components in a JList are not interactable (you can't interact with them) and they are just like snapshots from the component's appearance. I'm working on a simple application and JList is what I want, except that the components in the list should be interactable.
So far I've created this, by implementing a simple AbstractListModel, and ListCellRenderer:
Each component in the JList is a JPanel, which as you can see contains other components like JProgressBar, JLable and JButton. The only problem is that the buttons (and all other components) are not interactable. How can I fix this ?
NOTE: I don't want a single column JTable!
Adding this answer for people in the future.
I just ran into the exact same problem, using JList and ListCellRenderer is wonderful if you only need custom rendered items, but not fully functional components.
This is what I did and works a treat:
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
and then when I have the list of the information I need to dynamically assemble the panel:
public void build(List<Item> items){
panel.removeAll();
for(Item item: items){
panel.add(new ItemEntry(item));
}
}
wrap your panel in a JScrollPane and itll work just like before only all your controls will now function properly :).
I think you answered it yourself, JList is not editable, it was not designed to be and trying to retrofit it to be, is a LOT of hard work (and yes, I've actually tried).
You "could" use a MouseListener on the JList, find the row that was clicked, translate the MouseEvent to be within the context of the ListCellRenderer's Component and try and determine what was clicked ... but, again this is a lot of additional work which can more easily be achieved through other means...
Consider using a layout to generate a list of vertically aligned components instead, maybe VerticalLayout from SwingX or BoxLayout for example
I have one main JPanel container and three JPanels inside. How to empty this panel and add new panels? I tried with remove(Component) but it doesn't work. Can anybody give me advice?
This will do it. The trick is to call revalidate.
mainPanel = ...
mainPanel.removeAll();
mainPanel.add(newPanel1);
mainPanel.add(newPanel2);
mainPanel.add(newPanel3);
mainPanel.revalidate();
But really, consider using CardLayout, if you want to change what appears in a JPanel.
Here in this link i found a simple tutorial on how to add and remove elements from panels.
The other panels inside your main panel, are also elements, so the same principle applies to them.
A good practices when adding something new in the panel is not just to use the method add():
we might also want to use revalidate() and repaint() They should be called when some event occurs(button clicked or similar...)
Also i want to mention that in the tutorial remove() i being used to remove elements, you are doing it corretly. Maybe calling again revalidate() and repaint() for the other panels make the removed panel dissapear from the GUI(The object is deleted just the GUI is not refreshed)
Note: I suppose that the elements of your inner panels are visible = true. If some of the inner elements struggle to render try to call also revalidate() and repaint() at them.
I think this way should work.
# Harry Joy
if you added or removed (already visible container) then you have to call
revalidate();
repaint(); // not required in all cases
# Damir
if JComponents isn't public (or private) static then you can just call
myContainer.removeAll();
myContainer.revalidate();
nyCOntainer.repaint();
possible is remove JComponent(s) by some parameter(s) with Component[] a = myContainer.getComponents(); then you could call if (components[i] instanceof JComboBox) { ...
Try the other remove method remove(int index);
this works 100%
this.panelname.Controls.Clear();
I too had the same problem. All I did to resolve the issue was
panelName.setVisible(false);
mainPanel.remove(panelName);
In my case, panelName is a JPanel which lies inside mainPanel.
I have a JFrame inside of which is a jpanel that im using as the content pane.
So basically im using the jpanel to load content into on click. New content is returned as a Jpanel also so its ends up being jpanel -> inside jpanel -> inside Jframe. When i need to load in new content i clear the panel, load the new content and validate() the jframe & jpanel and the new content displays.
My problem is that when the new content displays its clear that the validate method is working because i can see the new interface but i can also see the old interface as if its become the background; i can resize the window and it just disappears and looks as it should.
Is this just the way validate works or can i fix it?
Edit: this worked. The problem was i wasn't calling repaint manually.
public BaseWindow setContent(JComponent comp){
contentPane.add(comp);
contentPane.revalidate();
contentPane.repaint();
return this;
}
Generally the code for adding/removing one or two components from a panel is:
panel.remove(..);
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
However, if you are replacing all the components on the panel, then the better approach is to use a Card Layout.
You have already stated the revaliate() followed by repaint() doesn't work so the only suggestion I have is to post your SSCCE that demonstrates the problem.
Don't use validate. Use revalidate() instead.
Revalidate first calls invalidate() followed by a validate().
In Swing, you would rarely use validate().
Note: I also feel that maybe the old panel is not cleared/removed.Check again!
Validate() is for causing components to re arrange themselves according to the layoutmanager that you have installed. This is not really what you should be using.
I can't see your code, so I'm not sure exactly what you are doing. I could speculate that calling repaint() on your "inner panel" will solve the problem you are having...but really, if you are doing things properly, you shouldn't need to call repaint() or validate().
Make two JPanels, one with content A (e.g. your buttons), and one with content B (e.g. your "static" field). Use the "add()" and "remove()" methods on the parent container (the JFrame's content pane?) to swap these two JPanels with each other whenever you want to switch the content that is displayed in that part of the JFrame.
Then you shouldn't need to do anything else; it should just work.
I don't know if validate() makes any promise about fully repainting the container. You might have to call repaint() yourself to make it behave as you want to.
Here's another possible solution:
Put both JPanels in at the same time, side by side, and then make sure only one of them is ever visible at any one time:
JPanel p = new JPanel(new BorderLayout());
p.add( panelA, BorderLayout.EAST );
p.add( panelB, BroderLayout.WEST );
panelA.setVisible(true);
panelB.setVisible(false);
Then when the user clicks the button to switch panels:
panelA.setVisible(false);
panelB.setVisible(true);
The setVisible() method and BorderLayout should take care of validating, layout, and calls to repaint() for you.
I ended up fixing my issue (display not shown, buttons would stay clicked/weren't unclicking) by changing which panels were added/removed.
Problem:
frame.removeAll();
frame.add(getNewPanelDisplay());
frame.revalidate();
frame.repaint();
Solution:
//initializer()
mainPanel = new JPanel();
frame.add(mainPanel());
// ...
//update()
mainPanel.remove(0);
mainPanel.add(getTablePanel(), 0);
frame.revalidate();
frame.repaint();
I am having a form built up in netbeans and want to add or remove a component with an actionperformed event of a button or a combobox is it possible?
if yes, how?
You can add components at run time, but you have to call paint() method of jframe to show the added component.
Create a JPanel where you want to add dynamic components and then use add/remove and setLayout() methods to control components on it.
The general code for adding components at runtime is:
panel.add( someComponent );
panel.revalidate();
panel.repaint();
However, I believe NetBeans uses the GroupLayout which will cause a problem. You need to understand how all the constraints work and then specify the proper constraints when using the add(...) method.
So my suggestion is to NOT use NetBeans to design your form and to learn to use LayoutManagers on your own, then you will be in full control of the layout and adding components will be as easy as the code above.
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.