Adjust layout for frame - java

I want to set a specific layout for this frame, for 'accuracy, I would like the frame to be displayed in the center of the screen, I tried to enter the GridLayout (x, y), with specific coordinates, but it gives me warning in eclipse the only thing that i can do is to set the null layout,as below.
class Login extends JFrame {
/**
*
*/
setTitle("Title");
setLayout(null);
.
.
.

First, a warning shouldn't forbid you from compiling in most cases.
Second, you're misusing the constructor. Read the GridLayout docs - http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html

If you want the frame to be displayed at the center of the screen, use setLocationRelativeTo(null);
GridLayout is not used to enter coordinates in terms of pixels but in terms of the grids you have created. If you want accurate positioning in pixels, set the layout to null, and then use setBounds() on any GUI component you wish.
For example:
JLabel aLabel = new JLabel("Title");
aLabel.setBounds(50, 50, 30, 10); //x, y, width, height

have you tried the "setLocationRelativeTo(null);"? this should center the frame
http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setLocationRelativeTo%28java.awt.Component%29

Related

JFrame absolute positioning - Use ContentPane as position reference

With setLayout(null), I can tell my components to be at e.g. x 5 and y 60. However, this references to the window's x and y position, borders included.
Can I, somehow, tell my components that 0,0 is contentPane's upper left (visible) corner?
This is especially annoying since different OS have different sized window borders.
Code initialising the JFrame:
JFrameAdd addFrame;
addFrame = new JFrameAdd();
addFrame.setVisible(true);
addFrame.setTitle("Vokabeltrainer");
addFrame.setResizable(false);
addFrame.setLocationRelativeTo(null);
addFrame.setBounds(100, 100, 825, 585);
addFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inside JFrameAdd.java:
JButton btnNewButton = new JButton(new ImageIcon(settingsIcon));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFrameGenerated.settingsFrame.setVisible(true);
}
});
btnNewButton.setBounds(0, 510, 36, 36);
contentPane.add(btnNewButton);
All code is here: https://github.com/MisterSkilly/Vokabeltrainer/tree/LoadSave/Latein%20asterisk/src and yes I know I shouldn't run two JFrames simultaneously, but changing that messed things up and I don't have the time to change that ATM.
However, this references to the window's x and y position, borders included
No, it references the content pane, which does not include the titlebar and borders.
Can I somehow tell my components that 0,0 is contentPane's upper left (visible) corner?
This is the default behaviour.
If you believe your application is behaving differently then post your SSCCE that demonstrates the problem.
The general rule is DON'T use a null layout. Let the layout manager worry about positioning the components.

Any way to center the center of my JFrame?

I have my own main Frame and two JDialogs that pop up when someone chooses menu items.What i'd like is to have a Help dialog with some rules,suggestions etc.
setLayout(null) and the Toolkit/Dimension do NOT help of course because they center the upper left corner of the frame/dialog in the screen .
How could have the center of both the frame and dialog centered in a screen?
Thanks in advance !
Do these in order:
1) Call pack() or setSize(...) on the frame/dialog to be shown (it should already have its components added at this point).
2) Then, call setLocationRelativeTo(null) on that container. Alternatively, you could call setLocationRelativeTo(parent) if the parent is offset and you want the dialog to be centered within the parent at its current location.
3) Then, call setVisible(true)
setLocationRelativeTo(null) will only center the upper left corner if you Dialog has no size yet, e.g. if you call the .pack() method after the setLocationRelativeTo.
Small example:
JFrame testFrame = new JFrame();
JButton testButton = new JButton();
testButton.setPreferredSize(new Dimension(500, 500));
testFrame.add(testButton);
testFrame.pack();
testFrame.setLocationRelativeTo(null);
testFrame.setVisible(true);
This will show an Frame which center is centered on the screen.
I think that will help:
frame.setLocationRelativeTo(null);
Usually we put a null as an argument, but you can put an argument of type Component, I prefer JOptionPane as it's always shows in center of the screen.
frame.setLocationRelativeTo(new JOptionPane());
//OR
frame.setLocationRelativeTo(JOptionPane.getRootFrame());
And one hint for your, don't use null layout(absolute
positioning), always use LayoutManagers.
Use this helper method to center any JFrame or JDialog:
public static Window centerFrame(Window win) {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
win.setLocation((int) ((dim.getWidth() - win.getWidth()) / 2), (int) ((dim.getHeight() - win.getHeight()) / 2));
return win;
}
Try to add this to your code:
frameName.setLocationRelativeTo(null);
This will center the frame in your screen. One thing, make sure that you declare the size of your frame BEFORE this line of code. This line should be right before your setVisible declaration

Get Resource Image and Use As JFrame Background [duplicate]

This question has been asked a lot but everywhere the answers fall short. I can get a JFrame to display a background image just fine by extending JPanel and overriding paintComponent, like so:
class BackgroundPanel extends JPanel {
private ImageIcon imageIcon;
public BackgroundPanel() {
this.imageIcon = Icons.getIcon("foo");
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(imageIcon.getImage(), 0,0,imageIcon.getIconWidth(),imageIcon.getIconHeight(),this);
}
}
But now, how do you add a component on top of that background?
When I go
JFrame w = new JFrame() ;
Container cp = w.getContentPane();
cp.setLayout(null);
BackgroundPanel bg = new BackgroundPanel();
cp.add(bg);
JPanel b = new JPanel();
b.setSize(new Dimension(30, 40));
b.setBackground(Color.red);
cp.add(b);
w.pack()
w.setVisible(true)
It shows the little red square (or any other component) and not the background, but when I remove cp.setLayout(null);, the background shows up but not my other component. I'm guessing this has something to do with the paintComponent not being called by the null LayoutManager, but I'm not at all familiar with how LayoutManagers work (this is a project for college and the assignment specifically says not to use a LayoutManager).
When i make the image the background has to display null (and so, transparant (??)) the red square shows up so it might be that the background is actually above my other components.
Does anyone anyone have any ideas?
Thanks
When using null layout (and you almost never should) you have to supply a bounds for every component, otherwise it defaults to (0 x,0 y,0 width,0 height) and the component won't display.
BackgroundPanel bg = new BackgroundPanel();
cp.add(bg);
isn't supplying a bounds. You'll need to do something like:
BackgroundPanel bg = new BackgroundPanel();
bg.setBounds(100, 100, 100, 100);
cp.add(bg);
Which would make bg size 100 x 100 and place it at 100 x, 100 y on the frame.
Look in the documentation on the Root Pane for all the information you need. Note the availability of the layered pane and the glass pane as well as the content pane.
By default all components have a 0 size. Just because you do some painting on a component doesn't give the component a size. You are still responsible for setting the size. That is why you should always use a layout manager. It looks after all this size stuff for you so you don't have to worry.
I don't know why newbies always think they can't use a layout manager. Yes it takes a couple of more minutes to learn, but it saves you a lot of grief in the long run.
Background Panel shows a couple of approaches. Again they both assume you use a layout manager, so you may need to set the size manually.

Java Swing JPanel Sizes

I would like to be able to have a JPanel within my JFrame of a fixed size 400x400.
I would also like the to be a 20px wide border all around it.
The main problem is the following code doesnt stick it its size.` JScrollPane runningAni = new JScrollPane(new views.cRunningAnimation(
model));
runningAni.setMaximumSize(new Dimension(400,400));
this.setSize(new Dimension(600,600));
this.add(runningAni,BorderLayout.CENTER);`
When doing this the runningAni panel just strethces accross the whole frame.
public void paint(Graphics g) {
this.setBackground(new Color(0,255,0));
}
I know this because my full frame paints itself green rather than just the JPanel (The above paint code is for my panel not the frame)
How would i create the panel so it always stays the same size and so there is always a 20px colored border around it?
BorderLayout ignores the size. You need to set a LayoutManager that either allows you to set the size to a fixed size or one that cares for the sizes set. There are different layout managers that allow this (e.g. GrindBagLayout or no layout manager at all). Some are not that easy to use (e.g. GridBagLayout). What to use depends on the rest of the layout.
You could probably use a layout panel that contains your custom panel. The layout panel needs an appropriate layout manager and could be put into the center of the BorderLayout. This would mean nearly no modifications to existing layout code.
The whole point of BorderLayout is to fill the center with the center component.
Don't override the paint() method to set the color of the panel. Use:
panel.setBackground(...);
When you create the panel.
How would i be able to set a border around my Jpanel
See How to Use Borders.
Just set your layout to null, to what ever class your adding your JPanel.
Then use the setBounds() method to set your location and size!
For example:
public class Main extends JFrame{
YourPanelClass panel = new YourPanelClass();
public Main(){
// I didn't want to put all the, everyday JFrame methods...
setLayout(null);
/*
First two coordinates indicate the location of JPanel inside JFrame.
The seconds set of coordinates set the size of your JPanel.
(The first two coordinates, 0 and 0, tell the JPanel to start at the
top left of your JFrame.)
*/
panel.setBounds(0, 0, 100, 100);
add(panel);
}
}
And i would GREATLY recommend using the paintComponent() method.
For instance:
(Obviously you put this in your JPanel's class.)
public void paintComponent(Graphics g){
super.paintComponent(g); // don't forget this if you are going to use this method.
//Basically this makes your JPanel's background green(I did it this way because I like doing it this way better.)
g.setColor(new Color(0, 255, 0));
g.fillRect(0, 0, getWidth(), getHeight());
}
Please don't forget to thumbs up if this helped!
setPreferredSize()
setMinimumSize()
setMaximumSize()
should do the trick

How to set coordinates for components?

I'm a new user on Swing, and I have problem with drawing components by coordinates. Please, look at this code:
JFrame frame=new JFrame();
frame.setBounds(new Rectangle(0, 0, 700, 600));
frame.getContentPane().setBackground(Color.yellow);
frame.setVisible(true);
JPanel graph=new JPanel();
graph.setBounds(new Rectangle(0, 0, 700, 300));
graph.setBackground(Color.white);
graph.setOpaque(true);
frame.getContentPane().add(graph);
I need that JPanel closes 50% from JFrame, but now it closes 100%, and JFrame has white color for background. How should I fix the mistake? Also, is there any mean for setting width and height using percents? For example, 50% for width. Or may be exists any containers for my question? Thank you
don't use whatever#setBound(), use Standard LayoutManager, in this case is GridLayout(2, 0) best of ways how to do it, your JFrame and its JPanels will be resizable on both directions
If you really want absolute positioning you could try disabling the default layout manager, as shown in Doing Without a Layout Manager. However, most of the time it's best to (depending what your container is intended to show) choose an appropriate layout manager.

Categories

Resources