JDialog cannot be converted to frame - java

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.

Related

Switch tab on button clicked

I create one activity(Home activity)inside home activity I add frame layout & some buttons. in frame layout I load fragment(Home fragment)inside home fragment I create Tab Layout now my question is when we select button(from home activity)so change regarding tab is their any possible way??
Note: I can not use viewpager because of some reason
my flow is
(1) Activity->frame layout && buttons
(2) frame layout-> load fragment(Home fragment)-> tab layout-> new frame layout which contains diff. fragment regarding tabs.
when click on activity's button so change tab
Thanks in advance
Try this :
yourTabLayout.getTabAt(tabPosition).select();
tabPosition is an int that determinants which tab will be selected
For example : 0 will select the first tab, 1 will select the second tab, 2 will select the third tab and so on

Java Swing Menu Click

I have some problem swing menu click event.
I want to click 'My Status' how can I handle this please help me.
For example about this image, I want to add click event for system tools.
This is a short example for my application.
List<Menu> menuItems = new ArrayList<>();
for (File file : files) {
Menu menuItem = new Menu();
menuItem.setLabel(file.getName());
menuItem.setName(file.getPath());
menuItems.add(menuItem);
menuItem.addActionListener(newMenuItemClickActions(menuItem));
}

How to change the button text of FileDialog?

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

how i can get menu item of tray icon which is added by some other class

// TrayUtilitiesDemo is a call which is returning me tray icon create by current java process.
MyMenu.setLabel("MyMenu");
TrayUtilitiesDemo.addPopupMenu(MyMenu);
TrayIcon trayIcon = TrayUtilitiesDemo.getTrayIcon();
System.out.println("TrayIcons are: "+trayIcon);
when i am doing
trayIcon.getPopupMenu().countItems();
it is retrun only 1 menuitem. which is added by me that is MyMenu.
there are other 4 menuitems are there added by some other class which is creating this tray icon.
and not able to get the ActionListener also.
basically i want to right click on tray icon click and click on menuitem in PopupMenu added by other class(or by other process) for automation.
using windows 7 machine.
please help.

Making a JDialog always on top of application

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

Categories

Resources