is it possible in java or other programing language to open 2 popup windows with one click and one of them to be closed automatically after a certain time?
Thank you in advance!
yes in thing in the programing can be done
i show the 2 popup menu and you can close one of them after time by using timer or thread and you can change their size by using size method
i am not going to write all code because you ask if you can open 2 popup windows with one click or not
i iust open the 2 popup windows with one click
package experiments;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class CreateDialogFromOptionPane {
public static void main(final String[] args) {
final JFrame parent = new JFrame();
JButton button = new JButton();
button.setText("Click me to show dialog!");
parent.add(button);
parent.pack();
parent.setVisible(true);
button.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
new JFrame().setVisible(true);
new JFrame().setVisible(true);
}
});
}
}
Related
I have a JDialog which can generate another one. The two JDialogs have the property setAlwaysOnTop(true) and aren't modal. The second Jdialog generated from the first one appears always behind. I would like it to appear in front.
I tried several things : toFront(), requestFocus(), etc..
Here a short example to reproduce the problem:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
public class SwingTester {
public static void main(String[] args) {
createWindow();
}
private static void createWindow() {
final JDialog modelDialog = createDialog();
modelDialog.setVisible(true);
}
private static JDialog createDialog(){
final JDialog modelDialog = new JDialog();
modelDialog.setBounds(132, 132, 300, 200);
Container dialogContainer = modelDialog.getContentPane();
dialogContainer.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
JButton okButton = new JButton("Ok");
okButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
final JDialog modelDialog = createDialog();
modelDialog.setVisible(true);
}
});
panel1.add(okButton);
dialogContainer.add(panel1, BorderLayout.SOUTH);
modelDialog.setAlwaysOnTop(true);
return modelDialog;
}
After a click on the OK button, we see another dialog appearing behind the current one. The new dialog has the focus but is still behind
Actually, I tried your code, and it is no problem appears. If you want to see it too clearly, create dialog with random location. In every click, new dialog is random place on the top.
modelDialog.setBounds(new Random().nextInt(400), new Random().nextInt(400), 300, 200);
But my suggestion is create dialog as modal, in addition to set old one as parent.
Search doesn't turn anything up for this problem.
I'm using simple code to display a JFileChooser dialog with customized title and accept button:
JFileChooser fc = new JFileChooser();
fc.showDialog(null,"MyText");
On Windows 7 this works as expected: a Save Dialog is displayed, with "Save" replaced by "MyText" on the Accept button and dialog title.
However, on Mac OS X, only the Accept button text is changed - the dialog title is blank. I'm using Java SE 7 and MacOS 10.8.5.
By inserting this line between the two above:
fc.setDialogTitle("MyText");
The correct title is displayed. Is this a known issue, and/or can anyone else reproduce this behavior?
What you experience on Windows is not the expected behaviour (as it is not documented), it is just an implementation side effect.
The showDialog() is used to display a custom dialog (e.g. not an Open nor Save dialog). It has a parameter to specify the text of the Approve button. If the title has not been set with the setDialogTitle() method, the implementation arbitrarily chooses to use the approve button's text as the title on Windows OS, however this is not documented anywhere and you should not count on this to work.
If you want a custom title, use setDialogTitle(). If you want a custom approve button text, use setApproveButtonText(). Obviously showDialog() also takes the approve button's text in which case you do not need to call setApproveButtonText() prior.
If you want an Open dialog, use the showOpenDialog() method. If you want a Save dialog, use the showSaveDialog(). Only use showDialog() if you want a custom dialog.
here are all accesible keys for UIManager (part of them aren't accessible by looping in UIManager, for all supported Native OS's and standard LaFs), with notice that success is volatille, depends of Native OS and used LaF
you can to get parent from JFileChooser, to cast to JDialog
add JFileChooser to your own JDialog (simple without any special settings, e.g. myDialog.add(myFileChooser); + myDialog.pack();)
there is possible to play with components tree
all accesible keys for UIManager (part of them ....
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Test extends JDialog {
private JFileChooser fc = null;
private JFrame bfcParent = null;
public Test(JFrame parent, boolean modal) {
super(parent, modal);
this.bfcParent = parent;
if (fc == null) {
fc = new JFileChooser();
fc.setAcceptAllFileFilterUsed(false);
fc.setLocale(Locale.ENGLISH);
UIManager.put("FileChooser.openDialogTitleText", "Open Dialog");
UIManager.put("FileChooser.saveDialogTitleText", "Save Dialog");
UIManager.put("FileChooser.lookInLabelText", "LookIn");
UIManager.put("FileChooser.saveInLabelText", "SaveIn");
UIManager.put("FileChooser.upFolderToolTipText", "UpFolder");
UIManager.put("FileChooser.homeFolderToolTipText", "HomeFolder");
UIManager.put("FileChooser.newFolderToolTipText", "New FOlder");
UIManager.put("FileChooser.listViewButtonToolTipText", "View");
UIManager.put("FileChooser.detailsViewButtonToolTipText", "Details");
UIManager.put("FileChooser.fileNameHeaderText", "Name");
UIManager.put("FileChooser.fileSizeHeaderText", "Size");
UIManager.put("FileChooser.fileTypeHeaderText", "Type");
UIManager.put("FileChooser.fileDateHeaderText", "Date");
UIManager.put("FileChooser.fileAttrHeaderText", "Attr");
UIManager.put("FileChooser.fileNameLabelText", "Label");
UIManager.put("FileChooser.filesOfTypeLabelText", "filesOfType");
UIManager.put("FileChooser.openButtonText", "Open");
UIManager.put("FileChooser.openButtonToolTipText", "Open");
UIManager.put("FileChooser.saveButtonText", "Save");
UIManager.put("FileChooser.saveButtonToolTipText", "Save");
UIManager.put("FileChooser.directoryOpenButtonText", "Open Directory");
UIManager.put("FileChooser.directoryOpenButtonToolTipText", "Open Directory");
UIManager.put("FileChooser.cancelButtonText", "Cancel");
UIManager.put("FileChooser.cancelButtonToolTipText", "CanMMcel");
UIManager.put("FileChooser.newFolderErrorText", "newFolder");
UIManager.put("FileChooser.acceptAllFileFilterText", "Accept");
fc.updateUI();
SwingUtilities.updateComponentTreeUI(fc);
}
}
public int openFileChooser() {
//fc.setDialogTitle("Load File);
fc.resetChoosableFileFilters();
int returnVal = 0;
fc.setDialogType(JFileChooser.OPEN_DIALOG);
returnVal = fc.showDialog(this.bfcParent, "Load File");
if (returnVal == JFileChooser.APPROVE_OPTION) { //Process the results.
System.out.println("Opened");
} else {
System.out.println("Failed");
}
return returnVal;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("FileChooser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp = new JPanel(new BorderLayout());
JButton openButton = new JButton("Open File");
final Test test = new Test(frame, true);
openButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
test.openFileChooser();
}
});
openButton.setEnabled(true);
jp.add(openButton, BorderLayout.AFTER_LAST_LINE);
frame.add(jp); //Add content to the window.
frame.pack();//Display the window.
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//Turn off metal's use of bold fonts
createAndShowGUI();
}
});
}
}
I am using JWindow in my project to display a UI that is undecorated and also doesn't appear in the task bar. But, the JWindow always seems to be on top of all other windows. I tried setting the setAlwaysOnTop to false, but it didn't seem to help.
Here's the code that can reproduce the problem :
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JWindow;
public class Test extends JWindow implements ActionListener {
public Test() {
setSize(300, 300);
setLocationRelativeTo(null);
setAlwaysOnTop(false);
JButton myButton = new JButton("Click Here");
myButton.addActionListener(this);
getContentPane().add(myButton);
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Click Here"))
JOptionPane.showMessageDialog(this, "This dialog box appears behind the JWindow!");
}
}
My OS is Linux and I'm using the Oracle JDK 6. Also, while I was testing my app on Windows, I was using JDialog for the UI and it was working fine. But, in Linux JDialog seems to appear in the task bar.
Any help as to how to solve this?
After you set the visibility of the window to True, you send it to the back like this:
setVisible(true);
toBack();
If, later, you want to bring it to the top of the stacking order, you simply call:
toFront();
More details here:
http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#toBack()
http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#toFront()
i have three problems with a JPopupmenu on Mac, all can be reproduced by the enclosed java
program or e.g. the netbeans java application.
The first thing is that Java applications don't block the dock when a popup menu is shown.
So when i right click in my java application to open a popup menu, i can still
move the mouse over the dock area and the dock appears.
In non java applications (Outlook, Textwrangler, Finder...) the dock won't
appear if a context menu is shown in these applications.
Is there a way to make a java application behave like a 'native' OS X application, so
the dock will not be shown in this context?
The next problem is more annoying.
if the context menu is shown by the java application and now the user switches (cmd-TAB or by
the dock) to another application lets say Outlook, the context menu of the java application is still
visible on top of the other application window.
Is there a way to hide the popup menu of the java application if another application has the focus?
And the last problem.
Lets say an application is in front of netbeans and now you right click into the netbeans window,
a popup menu from netbeans is shown, but if you move the mouse over the menu items, no menu item
will be highlighted. You're able to select a menu item by pressing the mouse, but by moving the
mouse over the menu items they are not highlighted.
Why are the menu items not highlighted, is there a workaround?
Mac OS X 10.6.8
Java: 1.6.0_35
package popupmenu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
public class PopupMenuApp {
private JPopupMenu popup;
private class PopupListener extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
#Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}
private void start() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
popup = new JPopupMenu();
popup.add(new JMenuItem("A popup menu item"));
frame.addMouseListener(new PopupListener());
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setAlwaysOnTop(true);
}
});
}
public static void main(String[] args) {
PopupMenuApp app = new PopupMenuApp();
app.start();
}
}
For the second issue, adding a WindowFocusListener does the trick:
popupMenu.show(button, 0, bounds.height + 1);
frame.addWindowFocusListener(new WindowFocusListener() {
#Override public void windowGainedFocus(WindowEvent arg0) {}
#Override public void windowLostFocus(WindowEvent arg0) {
popupMenu.setVisible(false);
frame.removeWindowFocusListener(this);
}
});
Launch a file = launch program associated with given file and automatically open that file on start of the program.
Let's say I run IntelliJ IDEA, I run my code and main window (modal) of my program shows up. My program is in the foreground.
Then I launch a .pdf file (for now it means AcroReader will be executed) from my program. AA will show up in front of IntelliJ but behind my program.
Question
I would like AA (it is just an example here of course) to be shown in front of my program, not behind. How to do it?
Please note, this does not mean I would like to move my program to the background!
For launching files I use
java.awt.Desktop.getDesktop().open(new java.io.File(filepath));
My GUI is done in Swing.
Update 1
To rule out, any influence of custom widgets, events, and so on, I put simply JButton at the bottom of my window (JDialog) -- it is in Scala, but this piece is similar to Java syntax:
var dlg = new javax.swing.JDialog(null,"test",Dialog.ModalityType.DOCUMENT_MODAL);
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
var button = new JButton("Select Me");
var actionListener = new ActionListener() {
def actionPerformed( actionEvent : ActionEvent) = {
java.awt.Desktop.getDesktop().open(new java.io.File("test.pdf"))
}
};
button.addActionListener(actionListener);
dlg.add(button, BorderLayout.SOUTH);
dlg.setSize(300, 100);
dlg.setVisible(true);
Once clicked, AA is shown behind my app. Since it takes several seconds to run AA I also tried to click the button, and move the mouse away from my window. Exactly the same behaviour.
I also noted that AA is shown at the same relative position to my window, top left corner of AA is near bottom right corner of my app.
You may try something like this. On my machine (Ubuntu 10.4 LTS with Gnome2) it gives the evince (pdf-viewer) in the front, and if I close/hide evince - JDialog is placed back to the front.
On windows it may be very different, since actually without "dlg.toBack();" invocation behavior is the same.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class OpenFileTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
final JDialog dlg = new javax.swing.JDialog(null, "test", JDialog.ModalityType.DOCUMENT_MODAL);
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JButton button = new JButton("Select Me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
java.awt.Desktop.getDesktop().open(
new java.io.File("/home/user/Downloads/jfreechart-1.0.13-US.pdf"));
dlg.toBack();
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}
});
dlg.add(button);
dlg.setVisible(true);
}
});
}
}