How to use JDialog in a Swing GUI? - java

I want to extend my JFrame Form ... with one JDialog form (I want to connect with frame, I want in the menu-bar when someone click to the HELP (from the menu) will show the new dialog box. I do not want to use JOptionPane for this use.
I made in to the program menu with Help menu item, now on the actionPerformed will something write.
new help(this, true, ).setVisible(true);
Something this but I don't remember - what is correct?

this from new help(this, true, ).setVisible(true); could be missinterpreted by another this in rest of your code
create local variable for JFrame and JDialog, use title and Modal/ModalityType if is required, e.g. new JDialog(myFrame, ....)
create only one instace of JDialog, change DefaultCloseOperations to HIDE_ON_CLOSE
(then only) call myDialog.setVisisble(true) delayed inside invokeLater() (from JMenuItems event)

Why not using javax.swing.JOptionPane? It provides you all you need to show dialogs and prompt the user for inputs.
JOptionPane p = new JOptionPane();
// init p
p.setVisible(true);

Related

Figure out when a button is pressed in another window

I run my main window in the main method like this:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
NoteSystem MainWindow = new NoteSystem();
MainWindow.initUI();
}
});
And then when a button is pressed on this form, I create a window by instantiating a class I made. I'm having trouble detecting when the second form is closed and what the textboxes/other controls contained.
What is the proper way to:
a) Fire an event in NoteSystem when the second window is closed
b) allow NoteSystem to check all the components/controls in the second window
I considered using a JOptionPane, but I'd like to create the window entirely in my own class. The idea of having the main window frozen while waiting for response from the second window works for my application though, so if I could use JOptionPane with my own class, that would be ideal.
Thanks
The best way is to use a modal dialog, a window like a JFrame, but that halts program flow in the calling code until it is no longer visible. This way, the calling code will know exactly when the dialog window has been dealt with, since its code flow will resume once again, and so often the calling code will extract information from the dialog window code at that point. A JOptionPane is one type of these, and so is a modeal JDialog (of which a JOptionPane is a sub-type). Either of these can display as complex a GUI as any that is displayed within a JFrame, so don't sell them short. You'll notice that the second parameter of most JOptionPane methods is of type Object, meaning anything can go in there, but most often you'll pass in either a String for a simple JOptionPane, or a JPanel that can be chock full of components and other nested JPanels, and in this way the JOptionPane can display the complex GUI if need be.
For examples, please see:
Passing values between JFrames
action listener to JDialog for clicked button
trouble obtaining variable info from another JFrame
How do you return a value from a java swing window closes from a button?

Replacing contents in a frame using the optionpane-GUI

updating a panel
I am designing a program for my assignment using java.
I have made a log on box where user enters name and password and clicks on submit or register. Once clicked, an optionpane is brought
upEX.(JOptionPane.showMessageDialog(null, "Account registered", "Account",
JOptionPane.INFORMATION_MESSAGE);)
I was wondering how can I add an action listener to the OK button in the option pane that would remove all contents (username,pass,2btns) and replaces it with something else? I found this link: updating a panel which basically just says to use frame.remove and then add.
Another question:
My java file looks something like this
class MainFrame extends JFrame {
public MainFrame() {
// All the log in panels buttons are here, etc
}
}
I was wondering if it would be efficient to add the new box details in the same area or make a new class?
I was wondering how can I add an actionlistener to the OK button in the option pane that would remove all contents (username,pass,2btns) and replaces it with something else? I found this link: updating a panel which basically just says to use frame.remove and then add.
Don't. Allow the dialog to close, ascertain the action the user took, show another dialog...
Better yet, create you own JPanel, use a CardLayout, add all your views to it and navigate between them. Place this on an instance of a JFrame...
See How to Use CardLayout for more details
I was wondering if it would be efficent to add the new box details in the same area or make a new class?
It would depend. A Class should be a self contained unit of work, focused on accomplishing it's designed task...if your main class is doing more work then it should, then yes, separate the logic into separate class

Open java Applet or Frame inside a parent Applet method and wait for input

I have a Java applet (lets call it parentApplet) with a public method which must return information regarding the status of the performed actions (let's call it getUserInput()) . This method opens another Applet which needs user button input, by adding it as a child with add(childApplet), and afterwards adding itself (the parent) as an ActionListner of the buttons in the childApplet, being able to run other methods when the user clicks on the buttons in the childApplet.
My question is, how can I halt getUserInput() execution until the user has clicked the childApplet buttons?
I tried to have a static variable that tracks the return information, and spinning on a while(var == null) Thread.Sleep(1000); but it blocks the main thread, as it should.
PS: Having the childApplet as an applet can be changed to anything that could better fulfil the requirement of opening another panel on top of the parent applet.
Details on getUserInput()
That childApplet has a canvas (a Graphics object from a BufferedImage) on which the user can draw and OK/Clear/Cancel buttons. When the user presses OK, I need to get the BufferedImage drawn. Do you know if this can be accomplished by extending a JDialog?
You really need to restructure your app. You can't do it the way you want.
Try creating a JDialog set it up as you like with input fields and an OK/Cancel button.
Then to show the dialog do:
MyDialog dialog = new MyDialog(null, true); //true = modal
//dialog.setModalityType(ModalityType.DOCUMENT_MODAL); //or specify modal here
dialog.setVisible(true); //waits until dialog is closed
if (dialog.wasAccepted()) {
//grab values from dialog
dialog.getCanvas();
}
In the dialog you would have:
private boolean accepted = false;
public boolean wasAccepted() {return accepted;}
public Canvas getCanvas() {return canvas;}
public ? getWhateverElseYouWant() {return ...;}
The OK button would:
accepted=true;
dispose();
The Cancel button would:
accepted=false;
dispose();
The JDialog will pump events while it's visible. So the setVisible() function will halt execution until the dialog is closed.
That should work better, and then you can return many user input fields.
You can even change the JDialog constructor to pass default value(s) in.
That childApplet has a canvas (a Graphics object from a BufferedImage) on which the user can draw and ok/clear/cancel buttons. When the user presses OK, I need to get the BufferedImage drawn.
First of all, it should not be an applet but a JPanel (it is not impossible to do it as an applet, but also not trivial). Then you can show the JPanel in a one of three ways.
JOptionPane.showMessageDialog(..). The idea would be to use the ready made OK/Cancel buttons of the option pane to tell the main app. whether to actually use image drawn. I (as a user) would tend to expect the Clear button to not be in the group of buttons that dismisses the dialog. Put that button in the panel you pass to the option pane.
A modal JDialog. Creating a dialog is more work than using an option pane, but also more versatile. For instance, if the panel you put into the dialog has a 'set size of drawing' option, it is easier to resize a dialog than an option pane.
Another card of a CardLayout. The two previous suggestions have the dis/advantage that they will block the app. and the browser until dismissed. This is good in that you can simply query the state of the drawing immediately after it is shown, confident that the user has finished drawing. But it is bad in that the user might have the dialog or option pane sitting on screen for 30 minutes, and the rest of the browser will be inaccessible to them in that time. By instead flipping to a card that shows the drawing panel, the browser is not blocked, and the app. can query the state of the drawing as soon as the user makes one of the OK/Cancel selections.

Focussing in Java swing

I'm developing an application using java swing. When I click a button, I want another window to be opened. This works fine. But it is possible to alt+tab to the original window and then interact with it even after the new window is open. Is there any way not to let the user focus the original window after the new window appears? By window I'm referring to Jframe/Jdialog.
Assuming the instance of your main JFrame window is called mainWindow:, the following code will prevent switching the focus.
// the second parameter makes the dialog modal and will prevent
// switching the focus to the mainWindow
JDialog dialog = new JDialog(mainWindow, true);
...
dialog.setVisible(true);
Documentation on JDialog: http://docs.oracle.com/javase/6/docs/api/javax/swing/JDialog.html
You may try to use a JDialog instead of JFrame and pass the instance of JFrame to JDialog constructor
You may also try to check
frame.setAlwaysOnTop(true);
or may be like this:-
frame.toFront();
frame.setState(Frame.NORMAL);

How can i display new GUI based on menu click

I am learning java and building one project to test basics.
I have one menu item FILE and then sub menu item like
1)Front
2)Admin
3)Booking
I have separate gui made in separate files but i want that they should be visible in one area , with click on submenus
I am using swing , JmenuBar . Also the other guis are using Jframe
I have separate gui made in separate files but i want that they should be visible in one area
Most applications should only ever have a single JFrame, which indeed is your requirement since you want the separate GUI to be visible in the same area.
Therefore your other GUI, should not extend JFrame but instead should extend JPanel. Then you can just use a CardLayout on your real GUI to swap the panels in/out depending on which panel is selected from your menu. All these basic are covered in the Swing tutorial. I guess you would start with the section on:
How to Use Card Layout
How to Use Menus
Other people have already talked about ActionListeners and stuff, so that's half of the problem. The other half is how to actually deal with the multiple windows. I would probably not use one JFrame per different GUI, given that the spirit of the JFrame suggests you should only have one instance of it per application. Instead, I would look at using either JDialog or JInternalFrame. I'm not sure what you mean by
...should be visible in one area...
but JInternalFrame will allow you to implement something like a multiple document interface, where all the sub-GUIs would be contained within the frame of the main UI. JDialog would be give you independent windows like JFrame does.
If with "they should be visible in one area" you mean modal, then you should change all your JFrames to JDialogs and leave only the JFrame that contains your main-menu.
To do this, you need an ActionListener for each of the menu items. Then have each listener pass the instance of the JFrame you want to a method that controls where you want to position the window and show it.
//Make menu items
JMenuItem font = new JMenuItem();
font.addActionListener(new ActionListener() {
showWindow(new FontFrame());
});
JMenuItem admin = new JMenuItem();
admin.addActionListener(new ActionListener() {
showWindow(new AdminFrame());
});
...
//define frame handling method
void showWindow(JFrame f) {
...
f.setVistible(true);
}

Categories