So I have a panel and depending upon users entry they are populated with an x number of jlabels. Now the problem is, when the user entered information the labels successfully populate but they do not display properly in the panel; they don't even show.
Only when I resize the frame they appear?
It's been a while since I did Swing programming and I am trying to remember the method which you are supposed on a container after you add components. I think it's revalidate().
usually you have to call:
JPanel yourPanel = new JPanel();
yourPanel.repaint();
yourPanel.validate();
invalidate marks a component as needing to be relaid out soon because the component or one of its children has been resized or become visible or invisible. invalidate is called on a component automatically when children components are added/removed.
validate checks that a container is valid and if not, calls doLayout or invalidateTree to calculate positions and sizes of children components. validate effectively redoes the layouts if necessary, deciding on new sizes and locations of all components in the container.
After adding/removing components from a container, validate must be called on the parent to let the LayoutManager redo the layout. Calling validate does not schedule a repaint, so you may need to call repaint after the validate.
Related
I faced this new thing today, and I didn't know why. When I want to show something in panel for example, I just add it to panel; but why I cannot add a table to scroll pane directly, and why I have to call the setviewportview() method? What does add() method do and what does setViewProtView() do?
Basically, you should not use JScrollPane#add.
JScrollPane has a single component already attached to it, a JViewport, this is what the JScrollPane uses to display any component added to the view port.
setViewportView is a convenience method for for JScrollPane#getViewport#setView
The basic concept comes down to the fact that from the scroll panes point of view, it will only show a single component, so add doesn't actually make any sense for it. The method is a consequence of extending from JComponent -> Container
I have several custom panels arranged in a basic BoxLayout, on which I have set a SortingFocusTraversalPolicy (and also set setFocusTraversalPolicyProvider(true), in order for the policy to take effect).
The custom focus traversal works perfectly within each custom panel, but when you tab out of the last component in the custom panel, focus goes to the first component in that same panel, rather than the first component in the next custom panel.
How would I go about "chaining" the focus from one custom panel to another?
I faced this new thing today, and I didn't know why. When I want to show something in panel for example, I just add it to panel; but why I cannot add a table to scroll pane directly, and why I have to call the setviewportview() method? What does add() method do and what does setViewProtView() do?
Basically, you should not use JScrollPane#add.
JScrollPane has a single component already attached to it, a JViewport, this is what the JScrollPane uses to display any component added to the view port.
setViewportView is a convenience method for for JScrollPane#getViewport#setView
The basic concept comes down to the fact that from the scroll panes point of view, it will only show a single component, so add doesn't actually make any sense for it. The method is a consequence of extending from JComponent -> Container
I have a State Manager for a game that contains a stack of States. Now in one of my states ("Menu"), I have two JButtons in a JPanel (the JPanel is added to the main JPanel that displays everything). When I click the JButton "Play", I remove the Menu state from the stack. However, the JButtons stay on the screen (even though when i peek() I see that the Menu has been removed from the stack).
I don't want to remove the buttons from the State, because I want to be able to go back to Menu and see my buttons there again. How can I remove the buttons along with the state?
Have you considered using a CardLayout?
A CardLayout object is a layout manager for a container. It treats
each component in the container as a card. Only one card is visible at
a time, and the container acts as a stack of cards. The first
component added to a CardLayout object is the visible component when
the container is first displayed.
Assuming each state corresponds to a single JComponent, you can use the .setVisible(false); method to hide the item when the state is removed.
In this case, when you initialize the main component, you'd want to make sure all components have been added - then when you add/remove states, you can just toggle the visibility.
In my Netbeans code I have JPanels and JDialog which are driving me crazy at times. Some of the controllers on these containers decide not to show up or automatically change size even though I have set up both their size and contents within the code and through using the IDE properties. For instance some of my jButtons on a certain JPanel does not show its text label or the sizes of some of my text field change.
Any solution to this would be grately appreciated!
When you create GUI using the NetBeans IDE wizards the Layout manager attached with JPanel and JFrame is GroupLayout and it works as expected. It keep the size of your JPanel and JFrame as you have specified.
Now if you change the LayoutManager of the JPanel or JFrame then you are on your own. You must know the consequences of changing the LayoutManager and update / add the required code to make the code to run as expected.
I will suggest you to keep the default LayoutManager as GroupLayout if you want to get what you see in the NetBeans component designer.
Unfortunately you did not provide any code snippet that can show your problem. But let me assume that you are confused with layout behavior. Typically we use Layout manager and delegate to it the responsibility of placing and re-sizing the graphical elements. Layout manager does it work when the parent element is being painted, i.e. during execution of method paint() that happens asynchronously and may be caused by various events (e.g. changing focus, re-sizing of window etc).
In this case all your attempts to change size of specific element by calling its setSize() could be overridden by layout manager that decides to change size of the same element differently.
So, if my assumption is correct learn to use layout managers and ask more specific questions if you have any difficulties with them.