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?
Related
I'm trying to build a custom UI to practice my interface development.
What I would like to do is create a 'sidebar' on a JFrame, which acts as a tabbed pane for a number of JPanels that I would like to be able to switch between.
Currently, I have a 'sidebar' consisting of a number of buttons, and through clicking a button - I would 'hide' one panel, and 'swap in' another (although I am yet unsure of how best to do this). The panels I am creating are the same size, but I do not know how to swap a panel in one position on my UI with another panel, in that same position.
As a result of this I have researched tabbed panes, and these seem like they could be perfect for what I'm trying to implement. I know I can set each pane to display my own JPanel, but I would like the actual tabs to be represented by the custom buttons I have made.
Is this possible with the JTabbedPane class? Or should I continue figuring out how to swap in and out my panels using the button action listener?
To expand upon the headline :
I have a screen (my main window, an encapsulated JFrame) that's going to be created most likely with a GridBagLayout, because I need a grid whose cells are to be differently-sized rectangles. In one of these rectangles will be a malleable dialog-like functionality, with different options depending on the context of the application.
My question is, are custom JDialogs the way to go here? Or do I simply want a reusable JPanel that has the particular buttons I want displayed or disabled depending on the context? I hope this is clear; thanks. -B.
Go with the JPanel solution.
JDialog is a heavy-weight, top-level container, meaning it's window is managed by the system and cannot be embedded as a child of another component.
Is there a way to bypass the layout manager for a given component in Swing? Something similar to position="absolute" in CSS. Null layout is not an option.
I have an existing GUI which I can't modify and uses different kinds of layouts and I need to add a button a the top right corner of the screen.
If you can't modify the existing GUI, including the top-level containing JFrame, you might be out of luck.
If you can modify the root container, you can achieve what you want with a layered pane. You can put your existing JTabbedPane in a lower layer, and add your button on a higher layer (and there you can use a null layout + setLocation()).
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.
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.