Change jFrame to jDialog? - java

I've been trying to change a jFrame to a jDialog so it inherits the icon of the main window but I don't have a clue how to do that. I tried setting it's code from public class jSemestriala extends javax.swing.JFrameto public class jSemestriala extends javax.swing.JDialog but that didn't change the icon of the window. Any ideas? I'm using NetBeans 7.0.1

The JDialog takes its icon from the owner frame.
You have two options:
Create an invisible JFrame, set your icon to it and set that frame as owner of the dialog.
Create the Dialog, get the owner, and set the icon to it.
I would choose the first option, it seems saver to me. The second one makes use of the (shared) owner of the dialog. This could cause side effects.
For further reading.
But if you already have an main frame, you simply need to set it as owner in the constructor of the dialog.

You need to specify the "main window" frame as the owner of the JDialog:
// ownerframe is a JFrame;
JFrame ownerframe = new JFrame();
JDialog dlg = new JDialog(ownerframe);
JDialogs have owner frames. The frame is either created for you if you call the constructor new JDialog(), in which case the frame is invisible; or you supply it to the dialog in its constructor using new JDialog(ownerframe).

create a new jdialog
hard copy the components from design view of jframe(you can use navigator window to copy all in clearly. ofcourse just copy the components under jframe)
paste it on jdialog(again use navigator window . ofcourse paste under jdialog)
hard copy the source code from jframe's source code window
paste it on jdialog's source code
do not touch the automaticly created codes
if you need it, add them again by using design window.
fix the errors in source code window of new jdialog by using your eyes and hands :)
remember all the time: be careful about choosing the type of class form.
sorry for my english.
it could take a long time but it will work.

Related

JAVA - How do I make the user be able to utilize only the frame on top?

When two JFrames are enabled, I want the user to be able to use only one in the top (think of that like an error pop up on your screen, when you can't press anything but the popup itself). I am aware of the class JInternalFrame and I chose not to use it for my program. Thanks in advance :)
Use JDialog, you can set your main frame as the JDialog's parent frame, so that whenever your main frame and JDialog will display, you will be able to click only the JDialog, not your main frame.
You want a modal behaviour, then. I think you can try using JDialog instead of JFrame, something like:
JDialog dialog = new JDialog(parentFrame, title, true); //parameters: owner, title and modal
dialog.getContentPane().add(somePanel);
dialog.pack();
dialog.setVisible(true);
You can read more about it here: http://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html

Convert JDialog to JPanel in Netbeans

I spent a long time creating a nice Dialog using Netbeans (Matisse), and now I realise that I want it as JPanel instead.
Is there any way of converting the Dialog to Panel in Netbeans. The blasted GUI editor does not allow me to modify any of the code.
Just open navigator in design mode in jdialog, copy components and paste in jpanel view.
Like in below image.
As always #MadProgrammer recommends, if you don't add directly to the jdialog and instead of a custom container (like a jpanel), then it's easy to put in another components :D
You can play a trick on Netbeans to convert it, with all your code:
Assuming your dialog class is called MyDialog:
In Netbeans UI Editor, create a new empty JPanel form (say, e.g.
MyDialogPanel), and save it.
Quit Netbeans, and open your favorite
file browser, navigate to the folder where your classes reside. You
will find MyDialog.java and MyDialog.from - as well as
MyDialogPanel.java and .form.
Now open MyDialog.java, and replace JDialog by JPanel in the class definition, and save as MyDialogPanel.java (overwriting the empty panel you have created).
Now open MyDialog.form, replace
JDialogFormInfo by JPanelFormInfo, and save as
MyDialogPanel.form (overwriting the empty panel you have created).
Open Netbeans, and open MyDialogPanel. The UI editor will be suprised and probably hickup a warning. Choose to allow editing, and save the dialog. Now look through your code, you probably need to fix a few things (like usages of getContentPane() or dispose() or the like, which are not there in a JPanel. After that, voila! Everything there, in your JPanel!
Ah. Figured it out. Just need to create a JPanel Netbeans class. Then I can just copy and paste the components across from the JDialog by going into the navigator view.

how to restrict more than one java application icon on taskbar?

I have created an application in java which have several forms.
During application start getting open new form on button click event,On windows's taskbar the number of icons of that form getting increases.
what I want is only applicatoin icon should be displayed on task bar whether one form is open or more than one.
The problem happens because each JFrame gets a task-bar icon. See The Use of Multiple JFrames, Good/Bad Practice? for links to a multitude of solutions.
I think this tutorial will help you to solve your task.
Multiple Document Interfaces with JDesktopPane and JInternalFrame
If you already have your new window as a JDialog and are still facing the problem of having two icons in the taskbar, it may be that you are creating your modal JDialog like this:
JDialog dialog = new JDialog((JFrame) null, true);
With owner (first) argument set to null, the application creates a new icon in the taskbar for the dialog. So, to avoid this, just pass the reference to your frame to the dialog constructor when opening the dialog (e.g. by clicking on a button). Like this:
public class MyBrandNewDialog {
public MyBrandNewDialog(JFrame owner) {
// create new modal dialog (the second argument is for modality)
JDialog dialog = new JDialog(owner, true);
// ...
}

Question about JFrames

I am running Windows. When you run an application on Windows, you get a button task bar where you can click it to maximize and minimize it. Is it possible to create a JFrame without this or some other component that has the functionality of a JFrame but without adding it to the task bar.
Use a JDialog instead of a JFrame. On a JDialog, you can set the 'modal' property, which means no 'upper bar' or anything is displayed.
Do make sure the JDialog has no parent frame or anything though: a modal JDialog will block the GUI of any parent GUI component. But if you just use it as your main component there is no problem :)

Keep JFrame on top of others

I am in a situation where I create a frame FrameB from my main frame FrameA. When the user is working on FrameB I would like it to be on top of FrameA even when the user accidentally clicks on FrameA.
Do you have to use a JFrame?
If you use a JDialog instead of a JFrame and assign FrameA as the owner of the dialog through the constructor it will always remain on top of the frame. (Example: How to set the JFrame as a parent to the JDialog)
Otherwise you can use setAlwaysOnTop() from the window class, but this can be dependent on the operating system/window manager.
You could consider making FrameB a JDialog instead of JFrame, and set it modal.

Categories

Resources