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.
Related
I'm posting this question because I'm new to programming at the present time and I have a pet peeve that when I create the app I don't want the objects to go across the entire window.
I use GridLayout the most often and I was wondering if there was a way to make components such as a JTextField or JTextArea NOT span the entire window, leave a little space on both ends?
You can add a component to a JPanel, which uses a FlowLayout by default and all components are displayed at their preferred sizes. Then add the panel to the layout using the GridLayout. The panel will increase in size but the components on the panel will stay at their preferred size.
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.
I have a JDialog that consists of two JPanels, one above the other. Currently, when I resize the JDialog only the bottom panel resizes in the vertical direction. However, I only want the top panel to resize. The only component that the top panel contains is a JScrollPane, so I want any vertical resizing to result in an increased/decreased view of the top panel's content. What is a good way to do this?
Thanks in advance!
elise
, I only want the top panel to resize
dialog.add(topPanel, BorderLayout.CENTER);
dialog.add(anotherPanel, BorderLayout.SOUTH);
This is a job for the proper LayoutManger. Here is a good link that explains LayoutManagers visually and does it quite well.
Guys, I need to put some buttons in a jscrollpanel, but the JScrollPane won't create a scroll vertically. I'm using a JPanel inside the JScrollPane which is using the simple FlowLayout layout. How can I make the JScrollPanel to scroll only in the vertical??
Problem:
Desired Solution:
Check out the Wrap Layout
The fact you use a JScrollPane changes quite a few things concerning the internal FlowLayout. indeed, when the FlowLayout tries to layout contained JButtons, it use for that the available space. In your case, you don't have limits to the space in the "scrollable client" of your JScrollPane. As a consequence, considering your FlowLayout has infinite space, it uses this space to display items according to it.
So the solution would be to change your scrollable client in order to limit its viewable area to the same than your JScrollPane's JViewport.
However, you would not even in this case have your line returns, as FlowLayout don't really well handle this case.
Were I to be you, I would of course choose an other layout. As GridLayout don't really well handles borders, i think the only reasonible standard layout you can use is GridBagLayout, althgough I fear your dynamic content constraints may require you something even more customizable.
JTextArea c = new JTextArea();
c.setLineWrap(true);
c.setWrapStyleWord(false);
This will wrap anything in a text area to the next line without creating a Horizontal Scroll.
Use the modified Flow Layout that I posted in this answer: How can I let JToolBars wrap to the next line (FlowLayout) without them being hidden ty the JPanel below them?
It will wrap to the next line and your scrollbar should scroll vertically.
scrollbar = new Scrollbar(Scrollbar.VERTICAL);
Or you could use a JList.
See this site for more info: http://docs.oracle.com/javase/tutorial/uiswing/components/list.html
the example class: ListDialog uses only a vertical scrollbar, when the window is resized or the elements don't fit the view.
I want my JTextArea to resize itself (expand vertically) when the last line (that the text area's height can offer) is reached and the user wants to start a new line. You know, like the textbox in MSWord.
I have an idea to use getLineCount() and determine (if necessary) the new height of the JTextArea. Do you have, or know of better approaches for implementing this?
Actually, the JTextArea always has the correct size so all lines of text are visible. What you experience is probably that you wrapped the text area in a JScrollPane. Just omit the scroll pane and make the text area a direct child of the container.
Another solution is to listen to resize events of the text area and size the scroll pane accordingly. This way, you can grow to a certain size and then start to display scroll bars (for example, when someone pastes 500KB of text into the text area).
I had the same problem. From my tests, I do not believe that the JTextArea sets its size dynamically. Instead, its size seems to be limited by its container (a JPanel in my case). However, the JTextArea does change its preferred size based on the text it contains. From the documentation:
java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane to match the functionality provided by java.awt.TextArea. JTextArea has a preferred size of what is needed to display all of the text, so that it functions properly inside of a JScrollPane. If the value for rows or columns is equal to zero, the preferred size along that axis is used for the viewport preferred size along the same axis.
Go to JTextArea "Properties" - checklist "lineWrap".
I had the same problem,I put the JTextArea into a JScrollPane and set the preferred size of JTextArea, and I believe that's the cause of the problem.
So the right solution is to put the JTextArea into a JScrollPane, and don't touch the preferred size of JTextArea, set JScrollPane's instead.