How to use JFrame properly? - java

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?)

Related

How to jump from a JFrame class after a specific time

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.

JFormDesigner Full Screen Design

I have been working with JFormDesigner Since last 4 hours and its a quite a difficult to make it work for all size screen. What do I need is I need to make a design JFrame window size to be full screen so that my design wont get distracted when I run it in different sized computer.Please some one help me in this regard.Thanks.
You need to get size of screen and the resize the frame.
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
hope it helps:)

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/

How to hide the default minimize/maximize and close buttons on JFrame window in Java?

I would like to know if it is possible to create a JFrame window which has no default maximize/minimize(-) and close(x) buttons! I have added custom buttons on each frame so that the user does not have to mess around with the default ones on the top right corner of the window!
You can use JWindow because is by default un_decorated, but you can setUndecorated() for JFrame/JDialog
another ways are
implements WindowListener
setDefaultCloseOperations
Use JFrame.setDefaultLookAndFeelDecorated. It may not be the exact thing you need but doc says,
Provides a hint as to whether or not newly created JFrames should have
their Window decorations (such as borders, widgets to close the
window, title...) provided by the current look and feel.
Try this code:
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 100);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
frame.setVisible(true);
This will remove the entire titlebar. Also take a look at this thread.
Otherwise use JWindows.
JFrame.setDefaultCloseOperation(frame.DO_NOTHING_ON_CLOSE);
Will make the 'X' button no functioning. It's works for me.
If you are using Netbean then just unselect the resizable option in properties.
It will only disable Minimize/Maximize Button.
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);

Creating a superior JFrame over another

I have two JFrames. Both are visible at same time.
One JFrame takes the whole screen..its just plain white. (it is acting as a background). And other JFrame is a small box with buttons/texts and other swing components.
The problem I get is when I click the big JFrame area, the JFrame box minimizes. So how do I specify java to make sure the JFrame box is always on top of the JFrame background?
Use a JInternalFrame
Make the JFrame box a JPanel box.
Your application should only have one JFrame.
JFrame is a TopLevel Component and therefore usually you don't put a JFrame into another. If you want to put your smaller jframe into your bigger I would subclass either JDialog or a JPanel.
In general, an application should only have a single JFrame. Other windows should be dialogs.
The problem I get is when I click the big JFrame area, the JFrame box minimizes.
When you use the dialog make sure you specify the frame as the owner of the dialog:
JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame);
If the main frame is ever minimized, the dialog will also be minimized. When the frame is restored the dialog will always display on top of the frame.
use Jdialog with setModal(false) for your small window ,
probably you want something similar to gimp
look at gimp toolbox , only X at title , means its a Dialog.
hope that's help

Categories