I'm making a program that requires this jdialog box to always be focused and on top, ie: make the ding sound when i click on the parent window. here's what i have so far:
JDialog dialog = new JDialog();
// dialog.setAlwaysOnTop(true);
dialog.setSize(400, 220);
dialog.setLocationRelativeTo(relativeTo); //relativeTo is the name of parent frame
dialog.setVisible(true);
// dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
// dialog.setModal(true);
(the commented things are those i have tried unsuccessfully...)
how would i make this dialog box ontop of the parent window? any help would be great! thanks
You need to set the dialog's owner and make the dialog modal:
JDialog dialog = new JDialog(parentFrame, true); // owner, modal
Related
I'm trying to open up a pop-up window with 4 buttons on it that will dismiss when a button is pressed or when the user clicks outside of the pop-up window. I would just make an alert dialogue, but that will only support 3 buttons.
There have been a lot of questions about this same thing, and I can't find any consistent answer or any answer that works for me (including the deprecated Bitmap Drawable). I've put all of the suggestions I've seen into my code, but to no avail.
Here's everything I've used so far:
//to create new popup window
LayoutInflater chooseMealInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View chooseMealLayout = chooseMealInflater.inflate(R.layout.choose_meal_dialog, null);
PopupWindow chooseMealPopup = new PopupWindow(chooseMealLayout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
//to make popup dismiss on touch outside
chooseMealPopup.setOutsideTouchable(true);
chooseMealPopup.setFocusable(true);
chooseMealPopup.setContentView(chooseMealLayout);
chooseMealPopup.showAtLocation (chooseMealLayout, Gravity.CENTER,0,0);
chooseMealPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
I've tried to find everything I can, like keeping setFocusable before showAtLocation, but when I run the app, nothing happens when I click. Figured it might be something individual to my code, since I'm new and don't really know what I'm doing.
don't know what you really want to do...
if you want show dialog when click button
YourBtn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//what you want to do like show dialog
}
});
I have a controller, which contains buttons, if I press one button an Alert shows, with Yes and No options, and I want to write a test for this controller class testing the result of this Alert, for example if I press the Yes button what happens. I could get the Window/Stage, and the Scene, but how can I get the Alert from the controller which popped up. Is that the primaryStage or I don't really understand how can I get it. I had a look at the hierarchy of the Alert and it doesn't have any relation with the Stage or Scene or just I didn't observed it. Is there a possibility to get a reference of the Alert or of its buttons?
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Look, a Confirmation Dialog");
alert.setContentText("Are you ok with this?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
// ... user chose OK
} else {
// ... user chose CANCEL or closed the dialog
}
This code above does what you need.
Look here for more
And you can always consult java docs.
I want to make applcation where you can add students and then assign to each one of them 20 books.
I have 3 windows:
Main windows (JFrame)
List of books (JDialog)
Add a book (JDialog)
I have JFrame where I can see list of all students, then I click on "List of books" where I can see the list of all books in database (.txt file). So when I click on that button in JFrame, I open JDialog, that works fine. But now I want to add some books to the list, so I click on button "Add a book" in "List of Books" JDialog. So I just want to open another JDialog a top of previous JDialog.
So I the window "List of books" (which I opened from Main window) I want to open windows "Add a book".
Now when I do it by the same method as I open JDialog from JFrame, it shows error :
private void pridatKnihuJButtonActionPerformed(java.awt.event.ActionEvent evt) {
addBookJDialog newwindow = new addBookJDialog(this, true);
newwindow.setLocationRelativeTo(null);
newwindow.setVisible(true);
}
It shows :
Incompatible types: addBookJDialog cannot be converted to Frame.
Is there a simple way to do this ?
I'm creating those windows in NetBeans design function.
I found a few topics about opening some JDialog on another JDialog, but I didnt understand how to do it :/ There are 3 lines of code that opens another JDialog. Is there a way to just simple open it ?
Thanks.
Ok. I found the answer... or more likely, I finally understood the code everyone was posting :D 2 hours of looking for this :D
If someone have same question. The answer is:
When you click on button in your dialog button, then head to the ActionPerformed section of that button (double-click on that button in NetBeans design section).
and write this :
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);
<target dialog> <optional name> = new <target dialog>(frame,true);
<optional name (but same as above)>.setLocationRelativeTo(null);
<optional name (but same as above)>pridatzaznam.setVisible(true);
Import what is needs to be imported and there you go.
It'll probably need these two:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
Example :
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);
dialogIWantToOpen openthis = new dialogIWantToOpen(frame,true);
openthis.setLocationRelativeTo(null);
openthis.setVisible(true);
Will open a dialog atop of your current dialog, but only if your current dialog was opened from JFrame.
If you want to open a dialog atop a dialog atop a dialog etc... it will probably need some editation after (JFrame) where
SwingUtilities.getWindowAncestor(this);
will probably have to be something like
SwingUtilities.getWindowsAncestor(SwingUtilities.getWindowAncestor(this));
So this will open a dialog, that was opened from a dialog that was opened from a dialog that was opened from a JFrame. But I'm not sure if it will work. Didnt tried that.
How do you change the button text of FileDialog (not JFileChooser)? The two modes specify the button text:
FileDialog.LOAD -> "Open"
FileDialog.SAVE -> "Save"
But I want the button to say something else. Is it possible to change this text?
You cant change button text only with FileDialog methods, the only text you can edit in it is window's title which opens when you press for example "Open".
FileDialog fd = new FileDialog(FdExample.this, "select File", FileDialog.LOAD);
fd.setTitle("any new title");
If you want custom text in button, you can use JButton and ActionListener to activate FileDialog.
Probably this will be the best way.
FileDialog dialog = new FileDialog(Display.getDefault().getActiveShell(),SWT.SAVE);
Help how hide all system menu from JInternalFrame. (not super("title", false, false, false, false);) but completely remove button (in Numbs LookandFeel).
Try this:
JInternalFrame.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
Make sure that close button is invisible:
JInternalFrame.setClosable(false);