JPanel issue when font is increased - java

My friend is facing an issue where in he has a Swing Dialog and it has several text fields, combo boxes and radio buttons. Also it has a JPanel which holds the search results if at all the user wants to perform any search.
It looks fine with normal font size. But once the font size is increased to say 150% or even 200%, then the text boxes are not growing and the text in them is growing. So, they are not fitting in and are getting clip-ed.
He managed to overcome this limitation by using the setPreferredSize method on the UI components. Now it seems that he is able to control the behaviour in case of text boxes, combo boxes etc. But the search panel is still an issue.
Could some one please point out what the issue could be?
UPDATE:
They also have a JTable where the search results are displayed. Now, the thing is, they are hardcoding the height of each row in that JTable using the call setRowHeight. And due to this, if the font size is increased, the row height still remains the same. Is there any method call that resolves this.
We honestly think that they should not have done that hardcoding. Is there any solution for this? Please share.
Thanks,
Pavan.

Which layout is your friend using? Choosing a suitable layout may help.

Try pack() it will automatically adjust the Window to fit the preferred size of the components.

Related

unable to resize components in java windowbuilder

I'm designing GUI using java swing with the help of windowbuilder. I found that in any layout it's not possible to resize components by using mouse drags (even though it shows points to pick and drag to resize). Specifically reducing size is what most important to do.
Resizing is allowed only in two layouts: one in Absolute Layout (which is not at all good for practical purpose, considering different screen-sizes with which GUI should be better displayed) and another is Group Layout (which is also not a good for design due to it's complex code).
Following is the sample where I have placed two JLabels and now trying to add JComboBox at the location indicated by Green box.
But when I place the JComboBox it's default size is to fill horizontally. Even if I change fill to 'None' and try to resize, I'm unable to resize it. Following is the result after addition of JComboBox:
In the Background there is JPanel with GridBagLayout with following properties:
I found that changing values in columnWidths and rowHeights properties of GridBagLayout, the size of grid columns/rows can be controlled. But I'm unable to understand Size of which columns/rows all those values represents?. (I found no direct relation between number of those values and number of columns/rows displayed on Panel)
Is there any way out to resize components? And can anybody explain what those values in columnWidths and rowHeights properties of GridBagLayout represent?
It's simple, you need to add grow in you WindowBuilder. It looks like this:
picture
Click on this with your right mouse button and click on 'grow':
picture
Only objects with 'grow' are resizable.

JTextPane resizing on wordwrap

I'm creating a chat program, similar to IRC. With my client though, I have the problem that when text is added to a JTextPane (using a GridBagLayout), it resizes instead of wordwraps. Well, it actually will wordwrap eventually, but it shouldn't be resizing. Here is what I mean:
I could set the JTextPane dimension to an exact number, but I want the user to be able to resize the window, and the parts inside as needed. How can I get to put as much text in without it resizing?
I ended up fixing it by adding setting it into a JScrollPand and using .setPreferredSize().

How to make JComponent always fill parent event if parent is resizing or content of JComponent is changing?

I have a JCombobox and I'm adding some items at runtime. Some of them are very long and my Jcombobox is growing very long too. It's in a JPanel container with BoxLayout (PAGE_AXIS). I have no idea how to prevent growing...
I wonder there should be a property like overflow and in combobox item will be shown like "123..." if it's "123456789".
EDIT:
I read your answers, thank you very much. But the problem is that my JPanel can resize and I need to make it's children always fill parent. So I cant set preferred size because my JPanel's size can change at runtime.
And I also can't set maximum size or use setPrototypeDisplayValue() because of the same reason.
I tried to use other layouts (GroupLayout, BorderLayout) but the result is the same.
I can post code if you want but I think it's not necessary. Let me know if you need it anyway.
Just tried with JTextArea with single row instead of JCombobox. Result is the same. It's growing when I type.
I solved similair issue by overrode JComboBox.setPrototypeDisplayValue()
and there are two ways (as mentioned #Stas) resize JComboBox or its derived Popup or both together
You can try this :
myCombo.setPreferredSize(new Dimension(myCombo.getHeight(), myCombo.getWidth()))
There are multiple solutions for the problem. You can change layout to limit growing.
Or fix somehow max size of the combobox
Or add a custom renderer (extending default renderer) and set preferred size there.

jButton resizing upon changing text label

I'm reasonably new to java GUIs and have been using netbeans to help me out.
I've set up a jButton such that when clicked its label changes. My issue is that the size of the button refuses to remain fixed despite setting a maximum and minimum size as well as the setting the preferredSize method. Do I need to change my layout? Should I go through and place each button on a panel or is there a simpler way?
I feel like this should be an easy problem to fix yet I've been at it for over an hour now. I'd appreciate any ideas. Thanks
If you are new to Swing don't use a GUI builder as you will run into all sorts of issues like this one.
It sounds like your Layout is preventing resizing. Make sure you are using the correct Layout Manager for your designed look. Double check any constraints that you have set for the layout. You could experiment with a different layout manager like FlowLayout to check to make sure your setPreferredSize () calls are working correctly etc.
There are a number of ways to handle this:
A clean and easy way would be to create image icons for the different buttons, making them the same size. This lets you completely control what they will look like.
A quick-and-dirty way to do this is the add spaces until the buttons are approximately the same size. This won't be perfect because the fonts that appear on JButons are typically not fixed-width.
The 'proper' Swing way would be to use a custom Layout. For instance, if you use a GridBagLayout to arrange your components, and set the 'weightx' and 'weighty' for the JButton to 1.0, then it will take up as much space as possible, which will keep it the same size.

Java JPanel redraw issues

I have a Java swing application with a panel that contains three JComboBoxes that do not draw properly.
The combox boxes just show up as the down arrow on the right side, but without the label of the currently selected value.
The boxes will redraw correctly if the window is resized either bigger or smaller by even one pixel.
All of my googling has pointed to calling revalidate() on the JPanel to fix this, but that hasn't worked for me.
Calling updateUI() on the JPanel has changed it from always displaying incorrectly to displaying incorrectly half of the time.
Has anyone else seen this and found a different way to force a redraw of the combo boxes?
Can you give us some more information on how you add the combo boxes to the JPanel? This is a pretty common thing to do in Swing so I doubt that it's a JVM issue but I guess anything is possible.
Specifically, I would double check to make sure you're not accessing the GUI from any background threads. In this case, maybe you're reading the choices from a DB or something and updating the JComboBox from a background thread, which is a big no-no in Swing. See SwingUtils.invokeLater().

Categories