JPanel adjust to JScrollpane and children - java

I'm trying to make a ToDoManager in java. For now I have about what I want it to be for a basic version. But I'm having a problem with the size of a panel.
I have a main JFrame. This contains a JPanel, say jPanel1.
jPanel1 has 2 buttons (add and remove) and another JPanel (say jPanel2).
jPanel2 contains a JScrollPane, which contains a modified version of JTable.
The thing I want is to tell the JTable to stretch out, so i can view everything in the JTable, and then tell the JScrollPane and jPanel2 to "Pack", or resize, so the JTable is completely vissable (if not possible the JScrollPane should do its work and draw the scrollbars).
This is what I have got at the moment:
So maybe you can see 2 problems:
1) The horizontal scroll bar does not appear. (But I did set the scroll bar: HORIZONTAL_AS_NEEDED)
2) I did not set any preferred size for the main JFrame, nor for the jPanel1, but it packs always as the same size. So I would like to stretch the jPanel2 to the full JTable, and if that would exceed the screen size, draw the scroll bars.

Using another layout manager, it's a lot easier to comprehend the usage of the JPanels and this concludes the problem.

Related

Java GridLayout how to dynamically change grid layout

I am trying to create a character sheet program that arranges 3 panels based on how wide the frame is. If the frame is not very wide, I want the panels to be arranged vertically, but if the user chooses to make the frame wider, I would like to arrange the panels horizontally.
I also have a scrollPanel that the 3 panels are arranged on, so the scrollPanel is being added to a scrollPane.
I have seen posts that say that an eventlistener would work, however most of these are for buttons, and I need the Frame size to pass a certain threshold before the layout changes. I seen other posts that recommend html which I don't see as necessary right now.
So in this picture the 3 panels are arranged vertically, and are able to be scrolled through.
scrollPanel.add(leftPanel); //this is the yellow panel that is being added
scrollPanel.add(centerPanel); //this one is farther down
scrollPanel.add(rightPanel); //even farther down
scrollPanel.setBackground(Color.CYAN);
scrollPanel.setLayout(new GridLayout(0, 1)); //so here I would like to have an if statement or event swap the 0 and the 1.
Summary:
Can I change a GridLayout dynamically based on the size of a Frame?
How do I monitor for a Frame size change? event? if statement?
Can I do the same with a ScrollPane with setting the horizontal and vertical scroll bars to never appear based on if the panels are vertical or horizontal?
You can change the GridLayout as follows:
gr.setRows(newR);
gr.setColumns(newC);
myFrame.validate();
where gr is the layout you created somewhere and newR/newC are the new numbers of rows/columns.
Or you can say
gr=new GridLayout(newR, newC);
myFrame.setLayout(gr);
Then
if(myFrame.getWidth()>thershold) {
.. do the above
}

How to make a JFrame scrollable?

I would like to scroll through the contents of my JFrame up and down, preferably with a scroll bar. I don't want to wrap the contents inside a JPanel or JScrollPane, because this causes some visual glitches with my application.
Any idea on how to do this?
JScrollPane would be the easiest way; you say there are glitches, but that probably indicates a problem in your code that will still be a problem even without using a JScrollPane.
If you're absolutely set on not using a JScrollPane, you should create a JPanel using BorderLayout, add a JPanel (call it 'center') with BorderLayout.CENTER and layout set to null. Add your content within 'center', and add another JScrollBar to BorderLayout.EAST, add an AdjustmentListener to the JScrollBar. When the adjustmentListener triggers, you need to move your content (Component.setLocation(...)) that's in center to the relative y offset of the JScrollBar and call repaint on 'center'

Make buttons unresizable

So I was trying to google how to set a default size to JButtons so that they don't grow as the JFrame is resized. I didn't see a setDefaultSize method but the closest one I could find that does a similar job is setMaximumSize(). However, it doesn't seem to work in my situation and I'm guessing it's because I'm using Grid Layout for positioning my buttons in the frame, here's a small piece of my code:
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
Here's a picture of what happens:
I would also like to have my buttons in the middle of the right panel when I'm resizing (just like they are now but a lot smaller). Any idea of how I can fix this? I'm assuming that I have to use another layout or something.
Thanks
EDIT: I modified my code to use BoxLayout but it does not seem to put the buttons in the middle. The X Alignment is working but Y Alignment is not doing anything:
ButtonA.setAlignmentX(CENTER_ALIGNMENT);
ButtonA.setAlignmentY(CENTER_ALIGNMENT);
ButtonB.setAlignmentX(CENTER_ALIGNMENT);
ButtonB.setAlignmentY(CENTER_ALIGNMENT);
ButtonC.setAlignmentX(CENTER_ALIGNMENT);
ButtonC.setAlignmentY(CENTER_ALIGNMENT);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
EDIT2: Fixed with vertical glue.
A GridLayout will always resize the components to fill the space available.
Try using a vertical BoxLayoutinstead. See the section from the Swing tutorial on How to Use Box Layout for more information and examples.
Encapsulate each JButton in a JPanel with a FlowLayout, and then add those FlowLayout JPanels to the rightPanel instead of the JButtons themselves. This will allow you to keep your evenly spaced buttons, but won't make them expand to take up the entire space that the parent container has available.
If you don't want them evenly spaced, but to be three consecutive buttons one after another top down, you can make the right panel have a BorderLayout, add a sub panel to the north area of the BorderLayout with the original GridLayout that the right panel had, and then add those FlowLayout panels containing the JButtons.

Scrollbars in JScrollPane don't refresh

I have a JPanel in a JScrollPane. When JPanel changes (in my case, I draw an image inside), the scroll bars are not refreshing accordingly. I need to move them sligthly or resize the whole frame and then everything is fine. How to force JScrollPane to show correct scroll bars (ie. after loading the picture)?
You should include these methods when you want to modify the JScrollPane according to some changes:
JScrollPane.setViewportView(this);
JScrollPane.revalidate();
JScrollPane.repaint();

How to make JTextPane wrapped in JScrollPane shrink to fit the text

I have a JTextPane with HTML text.
I used GroupLayout (using WindowBuilder).
I've set the minimum size of my JFrame to 800x600 so the user cannot make it smaller than that.
The app has a big scrolling JPanel the size of the entire window. The top part of the panel is taken up by a JTextPane wrapped in JScrollPane. I have disabled the scroll bars and sized the JScrollPane to make the entire text visible.
In group layout the JScrollPane is set to stay constant vertically, but size horizontally.
My issue is that when the user makes the window larger the JScrollPane also expands, but now there is a big white space left at the bottom of the text pane. Is there a way that I can make JTextPane shrink to fit its contents.
Also if you suggest a different layout, I would be willing to try it.
I used this TextPanePerfectSize example from #camickr to solve a similar problem. The example uses validate() and pack() to adjust to the preferred size. You might be able to adapt it to your situation.
Take a look at SpringLayout. It gives you far more control over the positioning of components. Look at the SpringLayout tutorial if you get stuck.
The trick in your case is to bind the bottom (south) of your JScrollPane to the top (north) of the screen.

Categories