How to jump from a JFrame class after a specific time - java

I would like to go to jump from my progress bar (which is not an entirely progress just a gif) to another JFrame that would take me to the Login Frame after 5seconds but i don't want to use splash nor use buttons.
My Loading Frame source code :
public static void main(String[] args) {
JFrame frame = new JFrame("Loading");
frame.getContentPane().setBackground(Color.white);
ImageIcon loading = new ImageIcon("loading.gif");
frame.add(new JLabel("", loading, JLabel.CENTER));
frame.setSize(400,400); // dimensions of the gif.(800x600)
frame.setUndecorated(true); // removing the minimize and shit..
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
So basically after this code is executed for about 10 seconds it would jump to another class that contains another frame called login.java .

In general you can use a Swing Timer to schedule an event.
Read the section from the Swing tutorial on How to Use Swing Timers.
However, this is not the best solution!
i don't want to use splash
Why are you trying to reinvent the wheel? Use Swing the way it was designed to be used.
A splash screen is designed for this purpose and will initially load faster.
The splash screen can be configured to automatically close after the specified time.
Read the section from the Swing tutorial on How to Create a Splash Screen for a working example.

Related

Java GUI Resize form after you create it

public static void main(String[] args) {
JFrame frame = new JFrame("Fantasy Football Toolkit");
frame.setContentPane(new GUI().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 475);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
Above is the main method to my GUI application. I'm wondering if I can change the size of the form after this main method runs. What if I want to change the size when I click a button? The problem is that I can't access frame. This has to be possible one way or another. I have looked all over for a solution but can't find one. Thanks.
You can always access the top level JFrame from any component that it holds via SwingUtilities.getWindowAncestor(yourComponent). This method returns a Window object, but if you are 100% sure that it's a JFrame, you can always cast it (or test it first that this is so).
Once obtained, you can always call pack() on it to resize it to the preferred sizes of the components that it contains. In general you will want to avoid setting sizes directly as that can lead to bugs later on when you modify your program.

How to use JFrame properly?

So I started working on a project, and after some research of JFrame, I tried this to get my window for the project:
ImageIcon hello = new ImageIcon("C:\\Users\\JP\\Desktop\\hello.jpeg");
JFrame frame = new JFrame("Test");
frame.setSize(640,480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setBackground(Color.GREEN);
frame.setIconImage(hello.getImage());
The frame comes up in it's correct size, but does not display the icon or have the correct background color.
If you are looking to change the background of your frame, you should take a look at this Setting background color of JFrame, but either way I recommend you use the design tab of window builder, because that can make your life much easier, and such stuff can be manipulated easily. (How to open a Java form in design view?)

Create responsive User Interface in Java

What is the best way to create an User Interface that is responsive in Java?
For example, I want that buttons resize when I change the size of the window...
I'd suggest using miglayout, as it is a user friendly and powerful layout manager that makes development of responsive interfaces easy.
This minimal example produces a interface with a single button that automatically resizes to fill the size of the window.
JFrame frame = new JFrame("MigLayout");
frame.setLayout(new MigLayout("fill"));
frame.add(new JButton("Button"), "grow");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
Moreover the layour manager is grid based, easy to use and extremely customizable, so I'd recommend it for a wide range of situations.

JFrame changing screens

I'm wondering how to change screens in a JFrame. For example, changing from the starting screen to a different screen. So you have an assortment of buttons, labels, trees, etc on one screen, as the user clicks a button a different layout appears.
Would the 'setVisible(false) and setVisible(true)' do the trick?
You've got it! Create separate JFrame instances for each of your frames:
JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();
//populate your frames with stuff
frame1.setVisible(false);
frame2.setVisible(true);
On a side note, you'll want to make sure to use setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) on any secondary frames to prevent your application from terminating if a user closes a secondary frame.
All that being said, you can also use multiple JPanel instances inside of the same JFrame instead of creating multiple JFrame instances. This way, all the action of your application will take place in one window.
I would strongly recommend giving this a read through: http://docs.oracle.com/javase/tutorial/uiswing/

Java how to make JFrames start off as a maximised window

I want to know how to make a java JFrame start out maximised. I don't want it to be fullscreen (no window around it) I just want it to start out like a normal program such as a web browser.
I already know about using:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
To get the screen size, but when you apply this Dimension to the JFrame it can still be dragged around even though it takes up almost all of the screen. You can press the maximse button to stop this but I would rather that the window started out maximised.
Also, I fear for the effects that maximising the window would have upon the contents of the window.
How do I go about doing this?
Use java.awt.Frame.setExtendedState():
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}

Categories