How to setVisible a JinternalFrame in the center of the JdesktopPane? - java

Actually i wanna show the JinternalFrame in the center of the JDesktopPane and i used this methode as i use it in Jframes but it didn't work :
Extraction ex=new Extraction();//Extraction is the name of the JintenalFrame
jDesktopPane1.add(ex);
ex.setLocationRelativeTo(this);
ex.setVisible(true);
So i am asking if there is another methode so i can display the JinternalFrame in the center of the JdesktoPane.
Thank you

Try something like :
JDesktopPane mainPanel;
JInternalFrame jif_test = new JInternalFrame();
public void centerJIF(JInternalFrame jif) {
Dimension desktopSize = mainPanel.getSize();
Dimension jInternalFrameSize = jif.getSize();
int width = (desktopSize.width - jInternalFrameSize.width) / 2;
int height = (desktopSize.height - jInternalFrameSize.height) / 2;
jif.setLocation(width, height);
jif.setVisible(true);
}
And
centerJIF(jif_test);

A javax.swing.JInternalFrame lacks the convenient setLocationRelativeTo(null) implementation found in java.awt.Window, but you can use a similar approach. Just subtract half the width and height of the internal frame from the corresponding center coordinates of the desktop pane. Use the new coordinates in your call to setLocation().
You'll also want to adjust the internal frame's dimensions if necessary.

Related

Java swing GUI absolute positioning

I know that absolute positioning is not recommended, but I need to show my labels randomly scattered as well as randomly changing their positions.
I have researched how to use setBounds but it doesn't seem to work. The following code shows the labels in a Flow Layout, and when I use setLayout(null) it shows a blank frame.
public class GUI extends JFrame{
device mobiles[];
device station;
JPanel pane= new JPanel();
public GUI()
{
setTitle("communication is Key");
setSize(1000, 1000);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pane.setBackground(Color.WHITE);
int x=0; int y=0;
mobiles= new device[10];
for (int i = 0; i < 10; i++) {
x=randInt();
y=randInt();
mobiles[i]= new device(1,x,y);
pane.add(mobiles[i]);
}
x=randInt();
y=randInt();
station = new device(0,x,y);
pane.add(station);
this.add(pane);
}
and this is class "devices" that extends JLabel
public class device extends JLabel{
ImageIcon mob = new ImageIcon("mob.png");
ImageIcon tow = new ImageIcon("tower.png");
public device(int num, int x, int y)
{ if(num==1)
this.setIcon(mob);
else this.setIcon(tow);
this.setBounds(x, y, 3, 7);
}
}
any help in finding out what the problem is, would be be appreciated.
The following code shows the labels in a Flow Layout, and when I use setLayout(null) it shows a blank frame.
The layout manager sets the size and location of the component.
If you don't use the layout manager, then you are responsible for set the size and location of each component.
Typically I would set the size to equal to the components preferred size.
Also, did you display the x/y value that are randomly generated? Maybe the values are larger than the size of the panel.
and when I use setLayout(null) it shows a blank frame.
What layout is set to null? The panel of the frame. Your code doesn't use the above method. Post the code that you use to cause the problem. We don't want to guess what you may or may not be doing.
thanks to #CasparNoree ... the answer suggested was to initialize the Japnel from the start:
JPanel pane = new JPanel(null);
When you set the layout to null you can set the bounds manually with coordinates.
JFrame jf = new JFrame();
JPanel p = new JPanel();
JButton jb = new JButton();
// this is where you make it so setting the bounds actually does something
p.setLayout(null);
jb.setBounds(100,100,100,100);
p.add(jb);
jf.add(p);
jf.setVisible(true);

Put panel and text box in the same frame

How do i put Jlabel and Jtext in the same frame?
if i add the text last, then only the text are showen, thes is my code:
public MatrixFrame(String framname, int width, int height) {
width =7;
height = 6;
JFrame fram = new JFrame(framname);
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fram.setSize(500,500);
JTextArea text = new JTextArea("Here come Text");
valMatrixPanel = new ValMatrixPanel(height,width,Color.GRAY, Color.black);
JPanel pan = valMatrixPanel.getPan(); // pan is 6*7 panels lock the picture
fram.add(pan);
fram.add(text);
fram.setVisible(true);
}
}
The key to solving this is your understanding how to use layout managers, because this is how Swing decides where to put what and how to size things. First off for a quick easy fix, put all your components into a JPanel, which uses a FlowLayout by default, and then add the JPanel to your JFrame. Don't set the JPanel's or the JFrame's size, do call pack() on the JFrame after adding everything and then finally call setVisible(true).
The better long term answer: read the layout manager tutorials which you can find, among the other Swing tutorials: here.
Try this
you will have to add a import for grid layout
check that
all you need to do is add a grid layout because the textbox overlaps the panel.
so add the line
fame.getContentPane().setLayout(new GridLayout(1,1));
JPanel pan = valMatrixPanel.getPan(); // pan is 6*7 panels lock the picture
fame.getContentPane().setLayout(new GridLayout(1,1));
fram.add(pan);
fram.add(text);
fram.setVisible(true);
use BorderLayout in fram.add()
like this
public MatrixFrame(String framname, int width, int height) {
width =7;
height = 6;
JFrame fram = new JFrame(framname);
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fram.setSize(500,500);
JTextArea text = new JTextArea("Here come Text");
valMatrixPanel = new ValMatrixPanel(height,width,Color.GRAY, Color.black);
JPanel pan = valMatrixPanel.getPan(); // pan is 6*7 panels lock the picture
fram.add(pan,BorderLayout.WEST);
fram.add(text,BorderLayout.NORTH);
fram.setVisible(true);
}

Java Mouse Position couting area that was scrolled

I didnt know how to make a good title for this.. but here we go:
I am trying to get the accurate mouse position when I click on a label, using a MouseAdapter I added to my the JViewPort of the JScrollPane
My panel itself is added inside the JScrollPane.
And when I try to get the coordinates of the mouse position is always relative to the area of the panel that is visible.. its not counting the area that is not visible, because of the scroll..
I dont know If I made myself clear, I hope so..
Already tryed using getMousePosition from JViewPort and from JScrollPane and also did not work..
Thanks alot in advance!!
Here is some code:
The construction of the frame that has the panel inside it..
public GraphViewer(ArrayList<TimeSlot> graph) throws HeadlessException {
final MyCustomPanel panel = new MyCustomPanel(graph);
panel.setPreferredSize(panel.getLargestSize());
scroll = new JScrollPane(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(scroll, BorderLayout.CENTER);
JViewport v = scroll.getViewport();
ViewportDragScrollListener l = new ViewportDragScrollListener(panel);
v.addMouseMotionListener(l);
v.addMouseListener(l);
}
What about adding JViewport.getViewPosition() to the coordinates obtained from getMousePosition()?
evento mouseClicked...
x= event.getX();
luego:
Point pos = scrollImagen.getViewport().getViewPosition();
pos.translate(x, y);
...
double nuevaPosx = pos.getX();

JAVA: size of frame and centering graphical objects

Is there anything obvious wrong with this line of code? I want rectangle to stay centered regardless the size of the window. But this donĀ“t work for some reason, the rectangle stays the same place.
public void run() {
setSize(800, 800);
createEntireFigure();
}
private void createEntireFigure(){
int centerOfWindowWidth = getWidth() / 2;
int centerOfWindowHeight = getHeight() / 2;
GRectWithGLabel ("A String",centerOfWindowWidth, centerOfWindowHeight);
}
Your rectangle size code is only called on rectangle creation, and so it makes sense that the rectangle's position will not change if the GUI is re-sized. You need to somehow listen for size changes in your GUI and call code to re-position the rectangle then for this to work. What graphics library are you using?

Java Swing set "actual" Frame size (inside)

I have a JFrame which contains just one JPanel.
I have tried setting the Panel's size and packing the frame, but that has no effect.
If I set the JFrame's size, it will change the size so it includes the title bar and borders.
How do I set the "actual size" so it doesn't include the title bar and borders?
Example:
Thanks in advance, guys
You could set the contentPane's preferredSize and call pack on the JFrame, but in general it's usually best to let components size themselves based on their preferred sizes as determined by their layout managers.
This is an example of setting JPanel's size and packing the frame:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(200, 200));
frame.add(panel);
frame.pack();
frame.setVisible(true);
Did you try something like this?
If you set the preferred size of the JPanel then the JFrame's pack() method will respect it.
Use the .getInsets method on JFrame which gives you the dimensions of the sizes around the non-client area.
Then add it up to your wanted size, and set the size using setSize.
If you want to get Frame border then first use pack on your frame and after that get Frame Insets like in this code bellow:
frame.pack();
Insets insets = frame.getInsets();
int frameLeftBorder = insets.left;
int frameRightBorder = insets.right;
int frameTopBorder = insets.top;
int frameBottomBorder = insets.bottom;
Is better to use the pack method right after your setResizable(boolean) method, because resizable for some reason changes your frame borders, but if you dont use resize method then use pack method right on the start of frame constructor.
Setting the actual size of the usable space in JFrame could be done through setting the preferred size of the container in
createComponents(Container container) method. Done in this way, you skip setting the size of
the whole JFrame through setPreferredSize(new Dimension (width, length) in the run() method. So, the code will look like this:
#Override
public void run() {
JFrame frame = new JFrame();
// other code ...
this.createCompoents(frame.getContentPane());
}
private void createCompoents(Container container) {
// other code ...
container.setPreferredSize(new Dimension(width, length));
}

Categories