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).
Related
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));
I have a JFrame with JPanels in it, one of which is a JTabbedPane. It is created with Intellij Ideea form designer and the formPanel has prefferedSize set to something like 1200x800 which fits the content well.
The user can increase fonts and all the element fonts are increased. When this happens some elements do not fit in the window anymore. I tried calling frame.pack() but the frame keeps the same size.
What would be the standard approach to this?
Avoid forcing the preferred size of components should solve your problem. Forcing preferred size always leads to those kind of issues.
For the basic Swing components (such as JLabel, JTextfield, JButton, etc...), the preferred size is dynamically computed (by the UI of the component) and will take into account the font that is used.
So my program creates a MigLayout GUI and has a bunch of JLabels that are given images on creation. Here you can see what the final result is right now:
The images are too big and go over the size of the JPanel they're contained in. It is simply creating it to be the size that the original image was and then display that, however, it's obviously an issue. Is there a way I can fix the size so it will try to fit every component in the GUI?
For starters, I want to ensure that the blue portion does not get shifted like that, I want it to keep to the size it was set as (half the height). I also want the images to take up the maximum size they can with keeping the aspect ratio of the original image.
I've tried setting the JLabel's size and preferred size, I've tried some attempts with MigLayout giving it paramaters when I tell it to add the JLabel, neither have worked for me. I'm entertaining the idea of extending JLabel and writing something to fix it, but not entirely sure how to go about that. I know I could overwrite paint but not sure what I'd have to actually do.
My JPanel is set as follows:
JPanel jpCards = new JPanel(new MigLayout("insets 0, gapx 0, gapy 0, wrap 7", "grow"));
My cards are being added without additional parameters and created just by giving it pictures of the size they were displayed.
Just to clarify, I need the actual JLabel's size to be changed, not just what's contained inside, because they will have mouse listeners later on. If only the image changed, I'll have problems with the mouse listener detecting itself inside a JLabel's that it doesn't look as if it should
I solved this by instead changing the size of the icon before making the JLabel.
I've found that in Java, when you create a JLabel given some sort of image, it will fit the size of the JLabel to be that of the image you give it. So instead of creating the JLabel given the original image, I scale it first using the dimensions of the screen, then create a JLabel out of it.
There's still some work to be done since I want the user to be able to resize the screen and have the image resize. I'll have to spend some time figuring out how I can get it to resize, but it's not a major concern.
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.
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.