Is there a way to use setDividerLocation on the bottom componet? - java

I have a JSplitPane with a vertical split. The top component is a table which is in a JScrollPane, and the bottom component is a detailed description of a row. The bottom component always have the same number of elements, so essentially it stays the same height.
Right now, I am using setDividerLocation to position where the pane is being split. If I put 100 into it, the top portion will be 100px, if 500 then the top will be 500px. But if I re-size the JFrame, then the top component is still 100px and the bottom component is really big.
I was wondering if there is a way to set the divider location based on the bottom component instead of the top component?
Sorry if I didn't explain it well, if what I said is still confusing, please let me know and I can try to explain it better.
------ EDIT ------
Thanks to ControlAltDel for figuring it out. This is what my code looks kinda looks like now:
JSplitPane splitPane = new JSplitPane();
splitPane.setTopComponenet(new JTable());
splitPane.setBottomComponent(new JPanel());
splitPane.setResizeWeight(1); // This gives the top component priority when the window is resized

http://docs.oracle.com/javase/7/docs/api/javax/swing/JSplitPane.html#setResizeWeight(double)

Related

how to expand a jScrollPane in all four directions?

I am working on a very simple mind mapping application, and I use a JPanel to draw the structure of the mind map on. The nodes of the mind map are circles, and if you write in a node, the structure expands in that direction. I put my JPanel in a JScrollPane, but my problem is, that I can only expand it downwards, or to the right, but it should be expandable to the left and upwards as well. When my JScrollPane should expand in "negative directions" so upwards or to the left, I tried calculating how many coordinates I shift, and than redrawing the whole graphic, changing the x and y coordinates of everything respectively. But it still did not work, it drew the part that is visible correctly, but then when I scrolled down, the rest of my drawing disappeared.
I hope there is a solution for this, because I've searched for hours and found nothing useful, and because I can't think of any other way to solve this. Thank you for your help! :)
When my JScrollPane should expand in "negative directions" so upwards or to the left
You can't have negative coordinates, so you need to translate all the indexes to positive values.
and if not, I resize the JPanel.
This is the key. You need to override the getPreferredSize(...) method of your custom panel to return the appropriate size of the panel after all nodes have been translated to positive values.
Then you invoke:
revalidate();
repaint();
on the panel to make sure the layout manager is invoked and the nodes are repainted.

JScrollPane does not scroll properly: scrollRectToVisible acting up?

I have got the following basic setup on a Part of my GUI:
A JScrollPane
On it, a JPanel with a BoxLayout (new BoxLayout(tablePanel, BoxLayout.PAGE_AXIS))
And on this Panel, a Bunch ob Panels.
I am trying to scroll to the Panel that has been highlighted... this works ALMOST.
Currenly, if a Panel is only half-visible on the bottom Part, the ScrollPane scrolls to make it fully visible.. great.
If it is half-visible on the TOP part, it does not... I could live with that.
But if a totally invisible Panel at the very bottom is highlighted, the system does not comment, but neither does it scroll there!
if(selectedPanel!=null){
Rectangle targetRectangle = new Rectangle(selectedPanel.getX(), selectedPanel.getY(), selectedPanel.getWidth(), selectedPanel.getHeight());
Rectangle r = scrollPane.getVisibleRect();
if (!r.contains(targetRectangle)) {
tablePanel.scrollRectToVisible(targetRectangle);
}
}
I am unfortunately not 100% sure how it behaves when the second-to-last panel is selected while not visible, because I cannot make that happen without some code-gymnastics; perhaps someone can help with the information I can give at this point.
you have to compare Rectangle from/returns JViewport(visible rectangle from JScrollPane), not from JScrollPane
use selectedPanel.getBounds instead of (selectedPanel.getX(), selectedPanel.getY(), selectedPanel.getWidth(), selectedPanel.getHeight());
still isn't centerred, have to divide JVievports and selectedPanel with 2
the same result as to use single code line JComponentPlacedIntoJScrollPane.scrollRectToVisible(selectedPanel.getBounds())
for better help sooner post an SSCCE/MCVE, short, runnable, compilable

Java Swing, making a gridlayout fill parent?

I just recently started using Swing to create GUIs for programs, and it's been pretty fun to mess around with so far. However, I'm having an issue with a JPanel with the layout set to gridLayout. Right now it looks like this:
The grid on the right is a JPanel set to a GridLayout, with each cell being a bordered JLabel. The options on the left are also inside a JPanel, and the left JPanel and right JPanel are nested in a GridBagLayout set on a JFrame.
Essentially, my problem is that I want to "scale" the grid on the right so that each cell is a certain height and width. The grid itself will have a variable number of rows and columns, which are set when the program first starts up. Eventually, I plan to have the right JPanel in a JScrollPane (if that's how that works...), so I'm not really concerned about whether or not all of the grid shows up onScreen.
I tried setting the fill value for the gridLayout to "BOTH" and it gave me the following result:
This is closer to my intention, but I wanted the actual ImageIcon in the JLabels to fill the entire JLabel. Additionally, I would want the JLabels to be the same height and width. However, I don't know exactly how to do that. I've been messing around with it for a while now, and I'm not sure if I'm just too much of a noob with Swing, or if I'm missing something in the documentation.
In the end, I'd like the grid cells to be a fixed height and width, no matter the number of cells, and no matter whether it goes offscreen or doesn't fill it.
(Also, I just thought, maybe it's not the best idea to code this and then shove it in a JScrollPane later and expect it to perform the same.... I guess I'll just see what happens.)
but I wanted the actual ImageIcon in the JLabels to fill the entire JLabel.
Check out Darryl's Stretch Icon which will allow the icon to resize to file the space available for the JLabel.

Java's FlowLayout does not resize correctly

I created a JFrame initialized with a BorderLayout and a JScrollPane as its CENTER element.
The scroll pane is set with VERTICAL_SCROLLBAR_ALWAYS and HORIZONTAL_SCROLLBAR_NEVER policies. The intent of my frame is to have a controlled width, while the height should grow/shrink as data is added/removed.
Inside my scroll pane, I added a simple JPanel (lets call it the content panel) which is initialized with a FlowLayout (and LEADING policy).
In order to test this, I simply populate my content panel with 20 JLabel("Item " + n) components where n is the loop counter.
I would expect to see my labels shown on a single row if the frame is large enough and the labels wrap to other lines when I shrink the width. But instead, there is only a single line displayed with no wrapping... ever.
Does anyone know why the flow layout does not wrap when a scroll pane is involved?
If I remove the scroll pane all together and put the content panel directly in the frame, the desired wrapping effect occurs, but if the frame height is shrunk smaller than the content panel height it just disappears.
The idea is that I want my labels to be wrapped when necessary but also always be visible if it means having to scroll up/down.
Any suggestions on how to fix this?
Thanks.
Wrap Layout gives an explanation and a solution.
If you work with the designer, you have to set the prefferedSize property to null (delete what is set) then set the preferred size by clicking the triple dots [...] button next to the prefferedsize property name and put your preferred value.
I encountered the same problem and it works for me.

JScrollPane doesn't show if BorderLayout constraint is CENTER?

Been developing a game for a while, and currently re working the GUI, or at least trying to. Had the massive problem of not being able to resize the frame (without issues), as I didn't understand layout managers very well. A few projects later, and time to come back and do some more on the game, and I hit a problem...
The basic layout of the main frame is, mainPane, containing one gameScrollPane and one controlPanel. The scroll pane is a scroll pane, and the control panel a normal panel. The scroll pane contains the main game panel.
As I wanted the scroll pane to take up most of the screen, with the control panel taking up a small lower area, much the same as many Sim like games, so chose the Border layout for the mainPane. I added the scroll pane and set the constraints CENTER and the control panel added and constriants SOUTH. This didn't show the scroll pane, so I played around trying different constraints, and it seems that only when I set the scroll pane constraint to North, does it display at all.
To demonstrate this, I have created a quick video...
http://screenjel.ly/q5RjczwZjH8
As you can see, when I change the value of NORTH to CENTER and re run, it's like its not there!
Bonus points for anyone who can see a clear second problem which I may start another question for after this issue is solved!
I thank you for your time to read this.
Thanks in advance for any ideas or thoughts :)
Rel
If you'd posted some code to start with then you might have gotten a really quick answer. Luckily, you posted a link in the comments to the other response.
The setContentPane() stuff is weird, especially after doing some things to it that will then get wiped out. However, that's not your problem.
The issue is that you are adding levelMaker and personMover right to mainPane without any constraints. These will then be blowing away anything you set for CENTER... in this case the previously set gameScrollPane.
That's why you see it for NORTH and not for CENTER.
I can't get the video to show. It's been buffering for ages.
My guess would be that the scrollpane is in fact filling the center; it's just your game panel that's not being shown.
Your game panel needs to return reasonable values for getPreferredSize().
Update
Another thing you may want to do is have your game panel implement the Scrollable interface. You can then override getScrollableTracksViewportWidth and ...height to return true so your panel will be forced to the scrollpane's dimensions.

Categories

Resources