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
Related
So, I have been having trouble drawing multiple objects on a JFrame, and I know I need to use Layout managers, so I decided to test it with one object before I do multiple, however when I run this code:
fps = 30;
panel = new JPanel();
frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.addKeyListener(new key());
running = true;
update = true;
ball = new Ball(0,0,1);
//panel.setBackground(Color.BLACK);
panel.add(ball);
panel.setVisible(true);
frame.add(panel, BorderLayout.CENTER);
frame.requestFocus();
frame.setVisible(true);
startTime = System.currentTimeMillis();
nothing draws (There is more to the code, i just didnt want to inlcude all of it). However, when I remove the comment and set the background to black, the JFrame turns black. So why is it that it wont draw my Ball object (which i know works) but will change the background? Is there a specific way you need to draw on a JPanel?
panel = new JPanel();
By default a JPanel uses a FlowLayout which respects the size of any component you add to the panel.
ball = new Ball(0,0,1);
I have no idea what the code in your Ball class looks like, but I would guess the preferred size is (0, 0).
You need to override the getPreferredSize() method of your Ball class to return the size of the Ball so that your layout manager can do its job.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.
I know I need to use Layout managers,
The problem with a layout manager in this case is that the layout manager will control the position of the Ball which may or may not be what you want. If you want balls in random positions, then you will need to use a null layout. Then you will need to use the setSize() and setLocation() methods to control each Ball component.
Another option is to do custom painting of all your Balls. In this case you would add an object that you want to paint to an ArrayList. Then the custom painting code would iterate through the object in the list and paint them individually. See Playing With Shapes for ideas on this approach.
I need to create a login interface that opens in full screen(working in all display resolutions) but with undecorated(true).
I could create the expected jframe but how can i position swing components in the center of the jframe?
here is what I need to get
https://drive.google.com/file/d/0B3-1tpHz8_TGU1p3SlA2UGMxekk/edit?usp=sharing
and this is adaptive to all screen sizes
An undecorated frame is still a frame. In fact you could simply use a JWindow.
What this means is, decorated or otherwise, you can layout your components as per normal. Because your pushing for full screen, I might suggest using something like GridBagLayout as a bases, but that's me.
use the setBounds() method
setLayout(null);
setBounds(int x,int y,int width,int height);
setBounds() method will place the components in the desired position of the JFrame window.
Use Border Layout,This layout will help you to assign components to center.
For example:`
Frame.add(GuiFieldsPanel, BorderLayout.CENTER);
Or
Frame.add(GuiButtonsPanel, BorderLayout.CENTER);
Frame.setVisible(true);
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
I am writing a simple game withing which I am using a JFrame which contains a grid and a JPanel.
Here is my pseudo code:
void MyJframeConstructor()
{
// some basic bootstrap logic
// calling repaint to draw grid
repaint();
// Grid is drawn fine.
// Showing user a confirm dialog box on which I add below JPanel.
if(confirmed)
{
// GameInfoPanel extends JPanel.
infoPanel = new GameInfoPanel(new FlowLayout(FlowLayout.LEFT));
infoPanel.setPreferredSize(new Dimension(400, 100));
infoPanel.setLocation(500, 50);
this.add(infoPanel);
infoPanel.validate();
}
}
My problem here is my JFrame or window is 480 x 680.
Within this I am drawing a grid in 480 x 480 area.
Below which I want the JPanel to be located at 500,50 with dimension 400, 100.
However, when I run this code, once the user confirms with OK, the JPanel fills up the entire JFrame.
How can I keep the panel in its location and consistent in size through out the life of the app ?
Any help is highly appreciated and thanks in advance.
Within this I am drawing a grid in 480 x 480 area.
override PreferredSize for JPanel
then call JFrame.pack() and JFrame.setVisible(true) as last code lines
have to read InitialThread
if is there only one JPanel (JPanel filling entire JFrame) then to use built_in BorderLayout in JFrame f.e. myFrame.add(infoPanel, BorderLayout.CENTER) not FlowLayout
don't to extend JFrame create this Object as local variable
I've got a JPanel inside a JScrollPane. I draw things in the JPanel, and at some point I might draw past the width of the JScrollPane. In this case, I'd like the horizontal scroll bar to appear, and I'd like to be able to scroll around to view different parts of the JPanel. However, I end up clearing the JScrollPane.
frame = new JFrame();
frame.setBounds(100, 100, 1000, 800);
localScrollPane = new JScrollPane();
localScrollPane.setBounds(768, 6, 226, 350);
frame.getContentPane().add(localScrollPane);
localView = new JPanel();
localScrollPane.setViewportView(localView);
drawSomeThings(localView.getGraphics());
// wait for user input
int newWidth = drawThingsPastTheWidth(localView.getGraphics());
// these next two lines clear it
localView.setPreferredSize(new Dimension(newWidth, localView.getHeight()));
localView.revalidate();
What am I doing wrong? Thanks!
drawSomeThings(localView.getGraphics());
Don't use the getGraphics() method to do painting. The painting will be lost the next time Swing determines the components needs to be repainted.
Instead custom painting is done by overriding the paintComponent() method of your component.
Do not use setPreferredSize method, here's a related thread.
Do not specify explicetly the size of the JScrollPane with setBounds. Let the LayoutManager of it's parent take care of this.
JScrollPane should use a
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED by default. So
the scrollbar should appear automatically when the preferred size of
the child component is higher than the displayed area. Try to
revalidate the JScrollPane instead of the JPanel.
It would appear that redraw is being called at some point, I've not used swing for a while so I'm not entirely sure what the problem is but try debugging and running through the code step by step to see where redraw is being called would be a good starting place.
you should certainly use repaint instead of revalidate. revalidate only marks all the container upto the top level as invalid.