I'm using a JPanel containing a JLabel with an icon. I'm using a ComponentAdapter on the JLabel to request a correctly sized thumbnail from the controller (using MVC pattern) when the JLabel is resized. This works fine when the JLabel is resized to be bigger than before, so then it's filled with an ImageIcon the size of the JLabel. However, when resizing the window to be smaller, it simply doesn't resize the JLabel at all (because of the icon's size, I'm assuming).
Is there possibly some layout manager or setting to make the JLabel disregard its content (the ImageIcon) and resize itself anyways? I mean, it can truncate text, so it ought to be able to just show part of the image when resized smaller.
Try experimenting with setPreferredSize() and setMinimumSize() to ensure the JLabel is allowing itself to shrink to the required size. You also need to double-check what kind of LayoutManager you are using and what constraints, if any, you use when adding your component to the panel. Try a different layout manager if you can. If none of that works, you will need to provide some example code.
Related
I have a frame with a border layout that includes a panel with a border layout at its center that has a label in its center that presents images.
Another thread is accessing that label to portray images that can be seen as a video - this works.
However, the frame itself is sized this way every time:
https://prnt.sc/116grry
I have to resize it manually to show the image every time I run the program.
SetPreferredSize doesn't change or do anything to achieve what I want.
The code:
https://gist.github.com/IshayKom/d06f40980fe42f96e28acdf1422b9e4f
Is there any way to initiate the frame at a specific size before it gets the images?
You might be looking for .setMinimumSize instead. setPreferredSize does not provide constraints and can thus be changed by other components such as the container that is used to show the image.
I have a problem with Java Swing JLabel.
The text i want to display on the JLabel exceeds the bounds of the JLabel. I want to display it via a Marqueeeffect. I already implemented the effect but when there is a string that exceeds the bounds of the JLabel it gets cut off and the rest gets replaced with "...".
My question is, if there is any opportunity to set the textlength for a JLabel individually, not depending on the bounds, that it doesnt get cut off?
Hope somebody got an answer for me.
I dont use any LayoutManagers and i dont want the JLabel to get resized, it should only can contain text longer than the bounds of it.
I want to display it via a Marqueeeffect.
Check out the Marquee Panel.
In this LayoutTest, you can see how the label's UI delegate uses layoutCompoundLabel() to elide the text when label's size falls below the preferred size.
In this MarqueeTest, MarqueePanel has a default FlowLayout, which adopts the display label's preferred size.
The Swing JLabel was not designed to do marquee scrolling.
Here's the source code for JLabel. You can modify the text handling routines to do a marquee scroll rather than compressing the text with an ellipsis.
Oh, you'd better use a layout manager. Your marquee JLabel won't layout correctly without a layout manager.
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.
How does a JLabel or JButton notify a JScrollPane that the view size has changed (for example when an icon has been set) so it can determine whether showing scrollbars are necessary?
How could I implement similar behaviour to display an image with a simple JPanel without resorting to the aforementionned components?
P.S: I've looked through the source code and so far all I see is that a Component is referred to as "view" and is passed on to a JView or JViewport which registers some listeners. From there on things seem unclear.
As noted in the JScrollPane API, unless you change the policy, "both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view." Once pack() has sized the Window "to fit the preferred size and layouts of its subcomponents," any subsequent changes are seen by the scroll pane when the container is validated and repainted. See Painting in AWT and Swing for more.
My current problem is that I have a JFrame with a 2x2 GridLayout. And inside one of the squares, I have a JPanel that is to display a grid. I am having a field day with the java swing library... take a look
Image
Java is automatically expanding each JLabel to fit the screen. I want it to just be those blue squares (water) and the black border and not that gray space. Is there a way I can just set the size of that JPanel permanently so that I don't have to go through changing the size of the JFrame a million times before I get the exact dimension so that the gray space disappears?
I also would like to set the size of those buttons so they are not so huge (BorderLayout is being used for the buttons and TextField)
GridBagLayout is what you really want to use. The GridLayout will force the same size for each component in the layout no matter what size constraints you put on them. GridBagLayout is a lot more powerful and a lot more complicated. Study up on the API page for it. Using GridBagLayout, the components won't fill the whole grid space if you don't want them to and can even stay the size that you ask it to be. To keep a component's size from changing, I would set all three available size constraints:
water.setPreferredSize(new Dimension(20, 20));
water.setMinimumSize(new Dimension(20, 20));
water.setMaximumSize(new Dimension(20, 20));
For your buttons, I would definitely use an inner panel as Bryan mentions. You could use either a GridLayout like he suggests or a FlowLayout if you don't want all the buttons to be the same size. Add all your buttons to that inner panel instead of the main one.
If you want the two checkerboards to stay the same size, then you'll need to have them each contained in their own JPanel. Set each of those parent JPanel's to have a layout type of GridBagLayout. Set the preferedSize for each checkerboard component and then add them to their respective containers. GridBagLayout should by default lay each board out in the center of the parent JPanel. So as the window is resized, the JPanel parent area will get larger or smaller, but the checkerboard components inside will remain the same size.
Alternatively, you could have your blue squares scale to the right size as the window is resized by having each checkboard square be a JPanel with a BorderLayout layout manager and adding the JLabel (with a blue background color) to its BorderLayout.CENTER location.
As for your buttons, try something like this:
JPanel theButtonPanel = new JPanel(new BorderLayout());
JButton button1 = new JButton("Fire");
JButton button2 = new JButton("Pass");
JButton button3 = new JButton("Forfiet");
JPanel innerButtonContainer = new JPanel(new Grid(1, 3, 8, 8));
innerButtonContainer.add(button1);
innerButtonContainer.add(button2);
innerButtonContainer.add(button3);
theButtonPanel.add(innterButtonContainer);
Lastly, consider using a design tool for your Swing user interface. Netbeans has an excellent UI designer built into it. Download Netbeans here.
If you can setResizeable( false ) on the top level frame you can then set your layout manager to null and hard code each location and size via setBounds. This is how I would do it (contingent on resizing of course).
I have had success solving problems like these using TableLayout which is a third party layout manager. You will need to download it and read the tutorial but the key would be to set the justification to CENTER when adding the JButtons to their positions in the layout.