How do I open a JInternalFrame centered in a JDesktopPane? - java

I am adding a bunch of JInternalFrames into a JDesktopPane, as the user selects to open various features through the menus. But I would like the internal frames to open centered in the desktop pane, as opposed to the upper left, where they seem to default.
How can I specify that the JInternalFrames open centered, or move them to the center after opening?
jDesktopPane.add(jInternalFrame); // jInternalFrame is not centered!

For reference, here is the solution I used, based on dogbane's advice:
Dimension desktopSize = desktopPane.getSize();
Dimension jInternalFrameSize = jInternalFrame.getSize();
jInternalFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2,
(desktopSize.height- jInternalFrameSize.height)/2);

Work out the top-left corner of the new location (based on the size of the JDesktopPane and JInternalFrame) and then call JInternalFrame.setLocation.

If you are using Netbeans (which is recommended for desktop apps) you just need to:
Select the form, right click and then properties;
Go to code tab;
Change "Form size policy" from "Generate Pack()" to "Generate Resize
Code";
Form Position (option above Form size policy) will be available.
Now you can set the for position as you wish :)

I would suggest the Window.setLocationRelativeTo(Component) method, which will center the window relative to a specified component. Instead of passing in a JDesktopPane, you might want to obtain the parent frame for a component, since otherwise, your JInternalFrame will be centered according to whichever component you pass in.
Here is a code sample:
private void showDialog(Dialog dialogToCenter, Component button) {
Frame frame = JOptionPane.getFrameForComponent(button);
dialogToCenter.setLocationRelativeTo(frame);
dialogToCenter.setVisible(true);
}

Add this void
public void addCentered(Component jif) {
desktopPane.add(jif);
jif.setLocation((desktopPane.getWidth()-jif.getWidth())/2, (desktopPane.getHeight()-jif.getHeight())/2);
jif.setVisible(true);
}
and when adding the jInternalFrame call:
addCentered(jifName);

Related

Jave Swing Eclipse - JPanel and components auto resize

I am maintaining a system which requires me to make components in the dialog resizable, the dialog box calls out a java class Panel.
What is supposed to happen:
What is currently happening:
Note: The image on the bottom layer represents the resized one. While the image at the top layer is the dialog box which is not yet resized.
As you can see, the component JPanel(the one with the black border) is not resized. I am trying to achieve what happend to the bottom layer image of the first attachment.
I tried to apply the answer in How to dynamically control auto-resize components in Java Swing and pattern it in current code but since my panel is only called in a dialog box so there are limitations. The problem is that the components and its hierarchy have been already implemented, I just have to make it auto-resize.
Here is my current outline:
If you want a simple solution you can use a layout manager as described here.
Or, if you wish to avoid a layout manager(like me) then you can have your program resize your elements every time there is a resize event.
Here is some sudo-code
frameOrPanel.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent componentEvent) {
element.setLocation(frameOrPanel.getWidth()*1/4, frameOrPanel.getHeight*1/4);
element.setSize(frameOrPanel.getWidth()*1/2, frameOrPanel.getHeight()*1/2);
}
});
You will have to add unimplemented methods.
Note: the 1/4 and 1/2 is merely a ratio you can change those to fit your application.

Resize code does not correspond with mouse movement

I have some code to resize a chatpanel dynamically, but it does not move according to the mouse. What happens is the mouse moves at a faster rate than the panel gets resized. For example, how I want it to be, is in any application, when you click on the border between two windows, and drag it, the mouse stays along with the piece you are clicking on, and currently this is not happening. here is my code. please let me know if you need more
public void mouseDragged(MouseEvent e) {
if(getCursor().getType() == Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR).getType()) {
owner.adjust((int)MouseInfo.getPointerInfo().getLocation().getY());
System.out.println("vertpanel: "+(int)MouseInfo.getPointerInfo().getLocation().getY());
}
}
public void adjust(int adjustment) {
int height = this.getHeight();
System.out.println((((double)(adjustment))/height)+":"+(((double)(height-adjustment))/height));
output.setHeightPercent((((double)(adjustment))/height));
output.componentResized(new ComponentEvent(this, 1));
input.setHeightPercent((((double)(height-adjustment))/height));
input.componentResized(new ComponentEvent(this, 2));
}
there is one main panel, a chatpanel, and within it, there are two smaller panels, a chat input and a chat output
Can't tell exactly what you are doing based on your code.
I would suggest that you should NOT be manually setting the dimensions of the output and input coponents. You should let the layout manager determine how to resize each component as the parent container is resized.
So in your resize code you would need to invoke revalidate() on the parent container as it is resized.
Check out Resizing Components. You should be able to use the ComponentResizer class as long as you use setAutoLayout(true).

change size and shape of icon in jframe

I have created a jframe in java with following code . this is creating the custom icon of the image passed in the given url . but its size is very small can i change the shape of it from rectangular to circular pls help
class newframe extends JFrame
{
Container cp;
newframe()
{
cp=this.getContentPane();
cp.setLayout(null);
}
public static void main(String args[])
{
newframe frm= new newframe();
frm.setbounds(0,0,1000,800);
frm.setVisible(true);
ImageIcon im1= new ImageIcon("path upto image");
frm.setIconImage(im1.getImage());
}
}
you can't change the size of Icon for JFrame, because properties of Top-Level Container came from Native OS, AFAIK and because
Icon can
a) fill all available area
b) smaller than available area
c) only part of Icon is visible, because is larger than available area
size of Icon is platform sensitive (WinXP == 16 x 16, Win7 depends of current theme 16 x 16 or 32 x 32 )
there are two Custom Look and Feels that implementing own injections to the properties for Top-Level Container came from Native OS, one of them is Substance L&F, 2nd isn't important to mentioned, because touched shadowed area, and required to remove toolbar and returns modified back (dirty hacks)
for JFrame based on JSR296 in Netbeans isn't possible to change Icon for Top-Level Containers, because this Swing Framework to override and protected some important methods, have to override RootPane (please don't do that, required tons of hacks and result will be only Borders (transparent window without contens) without ContentPane, no way back)
ONLY BY WINDOWS 7!
When you set the Width on 32x32 pixels, the icon of the JFrame Window in the the superbar is bigger. It bust be this Size, because when it isn't 32x32 it shows only 16x16 pixels.

How to disable the minimize button in JFrame?

Can I disable the minimize button in JFrame?
I have already tried setUndecorated() and setResizable() but both did not work.
I'm trying to add images to a panel at a random location (which works) but when the JFrame is minimized by clicking at the minimize button (not when frame minimizes by clicking the background window) images assemble at the top in a row.
Can someone help?
Thanks!
If you also want to disable the maximize button then you can use a JDialog instead of a JFrame... as far as I know you cannot disable the minimize button in a JFrame. JDialog only has a close button. Hope this helps
i m trying to add imags to a panel at
a random location which i m able to
do) bt wen frame is minimized by
clicking at the minimize button (not
wen frame minimizes by clicking at the
background window) images assemble at
the top in a row.
Well, it sounds like you are adding labels to a panel and using the setLocation() method to position the labels.
The problem is that by default a JPanel uses a FlowLayout so whenever you do anything to the frame like minimize, maximize, iconify or resize the frame the layout manger is invoked and the labels are arranged according the the rules of the layout manager.
If your requirement is to have random positioning, then you need to use a "null layout".
Read the section from the Swing tutorial that explains how Absolute Positioning works for more information and a working example.
Use JDialog instead of JFrame it has only the Close button on the top.

Question about GridBagLayout in Java

When I am using GridBagLayout , whenever the window is resized the component locations change to the fit new size ..
Now I want to change the location of a component after an event ( like a mouse click on any button ) until the component goes out of the window (it's like the properties or toolbox window in Visual Studio 2008) , I do that, but when I change the size of th window , this component moves to the last location , which I don't want .
when I put the compoents on the JFrame by GridBagLayout , I used gridx and gridy . What can I use to change this location after creating window ?
I have no idea what Visual Studio does. But it sounds as if you want to take control of the position of a component away from the layout manager in some circumstance.
To do this, I suggest using layered pane. You panel become a normal layer. On top of that is a glass pane, without a layout manager (or with a specialist one). When you start moving the component remove it from the bottom layer and add it to the glass pane.
A variation is to add all the components to an upper layer. For each component add a proxy component on to a lower layer with the grid bag layout. Usually the proxy updates the location of the real component in response to a change, but allow this to be broken.

Categories

Resources