Resizing Behaviour of JFrame - java

I have a JFrame and I would like to change its resizing behaviour.
More specifically, when I drag the JFrame to the right I want it to resize as if I was dragging it from the diagonal. So when I drag it to the right, the default behavior is that only the width increases and when I drag it from the diagonal both the width and the height increase. How can I override the behaviour so that when dragging to the right behaves as I was dragging it from the diagonal.
Many thanks in advanced.

You could add a ComponentListener to the JFrame and then manually resize the height to the same proportion as the width in the componentResized method.
Don't know if it is the best way, but that would work.

What I think you would want to do is make a component listener or a component adapter. When the frame is moved you could increase the size both vertically or horizontal depending on which direction the frame moved.

Related

JFrame larger than it's window

I've been using Java Swing for quite some time now and I never found a solution to this problem. When I create a JFrame the window surrounding it is actually smaller than the frame. In the included picture below my JFrame size is 800x600. The 2 white lines crosses at the center of the frame, 400,300. As you can see they are not at the center of the window. If I stretch the window right and down I can see some of the black background of the frame was hidden. When the black background is revealed you can see the the lines do indeed cross at the center (2nd picture).
Why is it working like that? Anything I can do to solve this problem? Im making a game where the playable character is in the center of the screen so this causes me a lot of problem. The 1st image is larger because i've left the code in the background. As we can see it's a standard JFrame creation.
Not centered because part of the frame is hidden:
centered when frame is fully revealed:
my JFrame size is 800x600
You are doing things backward.
The frame has decorations (ie. the title bar and borders). The panel where you do the painting is added to the frame, so therefore it will be less than the size of the frame.
The proper approach is to override the getPreferredSize() method of the JPanel where you do the custom painting to return the desired size of the panel.
Then you add the panel to the frame you invoke the pack() method on the frame. Now the frame will be sized slightly larger (to fit the complete panel and the frame decorations) and your painting will be accurate.

Java - Multiple components above the other (GUI)

I want to make a JFrame with multiple components above the other - it should look like this:
Everything centered
GridLayout scrollable (if x is a huge number)
What Layout should I use? How do I keep it as minimal as possible?
Thanks in advance!
You don't really give enough information, but what you give looks like BorderLayout to me -- image in the NORTH section, GridLayout in the CENTER, and a panel with your Label, TextField, and Button in the SOUTH. The CENTER will shrink and grow with the size of the frame. The bottom panel appears to have a BoxLayout with y-axis, and you can set x-axis centering on each component.

Limiting a JScrollPane's scroll range

I have a JTextArea wrapped in a JScrollPane, which I use to log my application's output. I'm using the default, plain font with a size of 9 for the text area, and the scroll pane's height is 48 px. This results in an even distribution of lines in the scroll pane view, but there's a problem: if you scroll all the way up or all the way down, this happens:
As you can see, the top line got cut off, which is why I'm wondering if there's a way to limit the scroll pane's scroll range so it, for example, can't reach the top or bottom 6 pixels. Alternative solutions are also welcome.
You could change the margin (top/bottom) of your JTextArea by setting a custom Border using the method setBorder inherited from JComponent. The documentation for JComponent suggests the following:
Although technically you can set the border on any object that
inherits from JComponent, the look and feel implementation of many
standard Swing components doesn't work well with user-set borders. In
general, when you want to set a border on a standard Swing component
other than JPanel or JLabel, we recommend that you put the component
in a JPanel and set the border on the JPanel.
That would yield the same result as limiting the scroll range, while being more straight forward.
EDIT:
OP reported that the following solution worked for him:
textAreaLog.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 6));
Place the JTextArea in a JPanel with empty borders where top and bottom insets are 6 pixels?

Make a JPanel scale to fit width but keep a fixed height

I have a JPanel which I'm placing inside of a JScrollPane. I am manually painting it using paintComponent(), since it's being used as a canvas, and I want the panel to automatically fit to the width of the scroll pane. I can then use getWidth() in the painting code to automatically scale to fit the container. However, I want to be able to manually set the preferred height so the I make use of the scroll pane's vertical scrolling capabilities.
What's the best way to do this? This would obviously still work if I was just able to get the width of the scroll pane in the painting code, but I don't want to break encapsulation too much with hacky code like getParent().
Let your panel implement Scrollable: the getScrollableTracksViewportWidth/Height control how the viewport handles the sizing in the horizontal/vertical dimension, respectively. A value of true indicates that the component is forced to the same size as viewport (that is, no scrolling in that direction), so getWidth() can be consistently used for scaling.
I would get your paint code to use the horiz width of itself as the basis for painting.
Then set the scroll pane so it never scrolls horizontally, only vertically.
Is this good enough ? Seems too simple now I've typed it out !

what will happen to its components if I setOpaque(false) for JPanel

I add several JButtons to JPanel, and setOpaque(false) for JPanel. When I move mouse onto the JButton, here appears a square under the button,and quickly disappears,which makes me feel awful.Does anyone know what is wrong?
(sorry for my poor English)
False is the default value for setOpaque as stated in the JComponent javadoc.
This is related to the effort it made to draw the components on the screen, when the property is set to true, then all the pixels of the component are drawn onscreen.

Categories