GridBagLayout manager and resizing controls - java

I'm not sure if GridBagLayoutManager is the only layout manager that does this, but here is my problem. I have 4 controls layed out horizontally in a GridBagLayout. To keep things simple for this example, each control get's an equal 1/4 of the form, and each control resizes with the same ratio as the other controls. The four controls are JTextField's where each text field maps to a column in a record from a ResultSet. Additional controls on the form allow one to navigate through the records.
If I navigate from one record to the next, then the text fields update their text to show the new data. However, the text fields also get automatically resized in proportion to the amount of text they are showing... text fields with a large amount of data expand in size, and text fields with a smaller amount of data get squished. If I run through 10 records in a ResultSet, then the controls are always resizing themselves and it looks quite bizarre to say the least.
What I would like to do is prevent these controls from resizing, unless (and until) the underlying container gets resized. So if I resize the window, then I would like the controls to resize (according to the "weight x" variable in the Layout), but I don't want the controls to resize just because the amount of text they are showing becomes more or less.
Anybody have any ideas here?

For JTextField (as mentioned in the contents) call setColumns(int) to set a preferred size on the text.
For JComboBox, call setPrototypeDisplayValue(Object) which will cause that value to be rendered and the preferred size of the JComboBox will be set based on that value.
In general, you can call setPreferredSize(Dimension) on any component directly to get the same behavior. General if not set the value is calculated based on some defaults on the component. What is happening with JTextField, JComboBox, and most JTextComponent derivatives. is that the preferred size on those components is driven by values the user is capable of changing (the text values, the combobox selection). Whereas with most other component (JButton, JCheckBox, etc) the content size doesn't really change when the user acts on it. Setting the columns and rows and the prototype display value fixes the value used to calculate the preferred size.

Related

How do I resize labels, text fields, etc. in the IntelliJ Java Swing form?

So, for example, I created a text field.
Tried to resize it
But once I release the mouse, it gets back to its original size
So how do I do it? How do I resize elements? And yes, I tried to change the minimum size, maximum size, preferred size but it does not work either, nothing happens.
You can do setLayout(null) on the parent or setPreferredSize on the components, either of which will allow you to resize your Components. But the best answer is to set your Font size to a larger size (setFont(...)) which will cause them to become bigger (have larger values in getPreferredSize) automatically.
JTextField tf = ...;
tf.setFont(tf.getFont().deriveFont(tf.getFont().getSize() * 2));

Java FX TextArea ScrollLeft Precision with Backspace

In the TextArea widget of Java FX 2.2, the method getScrollLeft is described in the documentation as returning
The number of pixels by which the content is horizontally scrolled.
More precisely, it accesses the value of the property ScrollLeft which has the above description.
When I slide either the horizontal or vertical scrollbar, this variable is adjusted, as expected. However when I press "backspace" on a line that is longer than the screen width such that the scroll amount decreases, or at the first character of a row, the property is not adjusted even though the scroll position changes.
A hacky way to fix this is to access the scrollbar directly, get how much of it is being scrolled, and calculate the appropriate value based on that information. I'd prefer not to do this.
Is there an accepted way to solve this problem, or am I misusing the scrollbar information?

Auto increasing JLabel

I want to output text from a .txt file which will have more and more text, however if I do that without modifying the size of the JLabel, the text just goes off screen, is it possible to somehow auto increase a JLabel while somehow detecting how much more is needed to increase to fill all of the text? Or is there any other text holder which is not editable but would fulfill that need?
You can make any Swing Component with a Document non-editable, so you´re free to choose. Choosing something that can be nicely wrapped in JScrollPane, like the mentioned JList or JTextArea, is a good choice.
Although a variable sized JLabel is probably not good UI style, you could achieve the desired functionality by putting the JLabel in an appropriate layout manager and requesting a new layout. How to force a new layout is described here.
SwingUtilities.invokeLater(new Runnable {
public void run() {
somecomponent.repaint();
}
});
Use Text Area if you have long text.
Its not advisable to have auto increasing length of components. Because alignments of your
other components on screen may suffer.
For these type of situations we rely on scrollBar.
So therefore. Use TextArea, set a preferredSize for them. and Then Use ScollBar if text
goes out of Preferred Size.
Another Solution : Decide Min and Max Size for your component. Default display
will show text in Min Size. If text increases, then increase size of text Area.
Stop increasing size till it hits Max size. Show scrollBars if it hits max.
Remember for this you might have to override JTextArea or its UIComponent(preferred).

List of contacts swing

I need to implement ui for list of contacts like in skype. An contact represented by custom class(JContact) which derived from JPanel. I tried to use different layouts but not received expected result. Main frame has next structure.
JFrame -> JPanel(contactsPanel)-> JScrollPane(scrollContacts)->JPanel(contactPanel)
scrollContacts.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollContacts.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
If use FlowLayout for contactPanel (see FlowLayout in image)
Strange behavior i think, because expected what each contacts will one under one because indicate HORIZONTAL_SCROLLBAR_NEVER for scrollContacts.
If use Grid or Box layout. Layout automatically re-size my panels, it's look very ugly. see Grid&Box layouts image.
Expected result see "expected" image
----SEE IMAGE----
I'm not native speaker, so please sorry for my bad English.Thank you for attention!
Quoting the Swing tutorial:
The FlowLayout class puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows
So the result you get is expected.
A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size. If the GridLayoutDemo window is resized, the GridLayout object changes the cell size so that the cells are as large as possible, given the space available to the container.
So the result you get is also expected.
When a BoxLayout lays out components from top to bottom, it tries to size each component at the component's preferred height. If the vertical space of the layout does not match the sum of the preferred heights, then BoxLayout tries to resize the components to fill the space.
So the result you get is also expected.
But, a box layout can contain glue components to avoid that.
I would thus use a vertical box layout, and add a vertical glue as the last component. Read the tutorial.

Change button text without changing button size

I'm using a borderLayout to arrange my components in a JFrame and a Box(BoxLayout.X_AXIS) to put buttons next to each other. But it creates two problems:
I want the buttons to have the same size, but it automatically resizes them to fit the text within them (especially annoying when I change the text inside a button at runtime)
I want the buttons to have a little bit of space between them (let's say 10 px)
Is this possible using the borderLayout, or do I need to use the setLayout to null? And if so, wouldn't this screw up the original placement of the buttons in the frame? Or would this still be dealt with by the Box which is placed with the borderLayout?
A couple of suggestions
Try setting the preferredSize to a suitable Dimension value
If that doesn't work, try also setting the maximumSize and minimumSize to this same Dimension value
If that still doesn't work, change the buttons' layout manager to a GridBagLayout. The advantage of this layout manager is that it lets you control the layout's behaviour in minute detail. The disadvantage is that you usually need to configure a large number of properties on the GridBagLayout in order to get the desired behaviour. I'd advise checking out a GridBagLayout tutorial first, as it's a reasonably complex beast.
If you want them to have the same size then just add the buttons to a GridLayout and they will automatically be sized to the largest text string. You can also specify a gap between components.

Categories

Resources