I'm trying to make a JTextPane inside a GridBagLayout JPanel. The program adds the the JTextPane in the right place, and everything is fine and dandy until I try to add text to the JTextPane. Then everything goes to hell. No matter what I do or try the JTextPane won't stop resizing itself by a width equal to the longest row and a height equal to the number of rows I'm adding.
This is extremely annoying because it causes all the rest of the Layout to go to hell. (The rest of the Layout is a Grid of rectangles on the main JPanel and another JPanel on the right, contained by the main one containing another grid of rectangles). I COULD use preferred size to stop the culprit from auto-resizing, but that would also prevent it from resizing on a JFrame resize (tip: this == bad).
I was wondering if there is any method involved in the auto-resizing of the JTextPane when text is added I could override to prevent this behaviour.
If it is needed I'll post a snippet later.
Related
I'm making a calculator same as provided in windows 10 for practice purpose, but I am unable to remove the space between the JButtons. I'm using Netbeans designer view to do this. I have tried by doing margin as 0 even doing -2 of both the buttons but whenever I resize the button and drag it to the other one, the other button goes away automatically.
Here is the screen shot what I want to do:
Here is the design view:
Give the JPanel that holds the JButtons a GridLayout, one with proper rows and columns (call the constructor that uses 2 parameters, again for rows and columns) but that uses no more parameters -- so the layout's horizontal and vertical gap is set to the default size of 0. GridLayout API
Add your JButtons to this JPanel.
Be sure to pack() the JFrame (or other top-level window) after adding components
And calling setVisible(true) after packing
That's really all there is to this.
I'm making a calculator same as provided in windows 10 for practice purpose, but I am unable to remove the space between the JButtons. I'm using Netbeans designer view to do this. I have tried by doing margin as 0 even doing -2 of both the buttons but whenever I resize the button and drag it to the other one, the other button goes away automatically.
Here is the screen shot what I want to do:
Here is the design view:
Give the JPanel that holds the JButtons a GridLayout, one with proper rows and columns (call the constructor that uses 2 parameters, again for rows and columns) but that uses no more parameters -- so the layout's horizontal and vertical gap is set to the default size of 0. GridLayout API
Add your JButtons to this JPanel.
Be sure to pack() the JFrame (or other top-level window) after adding components
And calling setVisible(true) after packing
That's really all there is to this.
Working with a vastly complex JFrame with numerous panels within panels within panels....you get the picture. If it were simple I would post an example.
The main panel has 4 panels and each of those panels have 6 or so other panels and those may even have some panels. The main panel is placed within the JScrollPane.
The JFrame opens and as soon as the window is made smaller, the scrollbars appear which is great, but the window originally opens about 3/4 of what it should be.
This means the window has to manually be made bigger to see it all as the scrollbars only appear if made smaller.
When dealing with so many layers of panels, what is the rule of thumb when working in this scenario. Should the setPreferredSize() be done at every panel, or just the parent panel or just the JScrollPane level or a mixture?
I have found in this particular scenario that using setPreferredSize() on the top most panel resolved this particular issue. There are setPreferredSize() methods called on other panels and those were removed to only use the call on the top panel and this seemed to be the key.
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.
I'm using a JFrame in which the CENTER portion of the BorderLayout is occupied by a JScrollPane that wraps around a JPanel. What I'm finding is that when I initiate the action that actually causes the JPanel to be displayed, the display doesn't change. But when I resize the JFrame, the new JScrollPane has now magically appeared.
So what methods are called when you resize a JFrame? If I know, then I can call it in the code and avoid having to resize the frame just to see the results of the operation.
Its been a little bit since I've done swing, but from memory, calling validate() on the panel should do the trick. This will cause it and its children to have their layout calculated which is when the scrollbars decision is made. If that doesn't work, try calling validate on the frame's content pane. This is a little more costly, but may be needed if other components are being considered.