How do I keep a dialog box always on top? - java

I created a dialog box like this:
String response =
JOptionPane.showInputDialog(null,"message","title",JOptionPane.PLAIN_MESSAGE);
I'd like to keep it always on top of all windows.
Do you have any idea?
Thanks!

In fact, using Java, having a system modal dialog is not possible. The best you can have is a toolkit modal option pane. That's to say an option pane that stay in front of all Java windows.
This example explains how Java6 allows you to do that.

Maybe I don't get the question, but I quickly created desktop app with code you posted and it actually is modal ...

Related

Prevent JOption from blocking child JFrame

Is there a possibility to prevent a JOptionPane dialog from blocking the interaction with the rest of the program, especially child JFrames? In my GUI, I launch a JFrame and want a message dialog to pop up after the child is closed to remind the user of something, but they launch parallel and the reminder blocks the child frame from being used.
Like here:
popupObjMan newPopup1 = new popupObjMan(gatewayAbstract, gatewayAbstractID);
JOptionPane.showInternalMessageDialog(this, "REMINDER: DO REFRESH");
I've tried to set the popup always on top, but this doesn't quite do the job.
I have no problem with them launching parallel (I'd even prefer it), but I could not work my head around it yet.
I just started Java programming ,so sorry in case that'd be something obvious.
A JOptionPane normally need to be modal. It shows something important and waits till the user answers with whatever option you give him (e.g. ok-button, yes/no-buttons, ...)
But there are several ways to reach your target.
(a)
Normally a JOptionPane creates a modal window.
You need a modeless window which does not block other windows.
https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html
(b)
You can start different threads to work for your different windows. They can have windows which are shown whenever the responsible thread commands them to. This is a bit difficult and can lead to memory-troubles.
(c)
You can write your own message-panels (e.g. notificaton) which are shown when and how long you like.
Bigger projects use different of these ways to achieve their goals.
A JOptionPane is a component, just like a JPanel. As a component it can be added to any other panel.
The JOptionPane API provides static methods to create a show the JOptionPane on a modal JDialog by default. You can't change this behaviour.
However, you can manually add the JOptionPane to a non-modal JDialog that you create. This is extra work as you now need to handle the closing of the dialog and processing the clicked button.
If you really want to do this then read the JOptionPane API. There is a section on Direct Use which demonstrates the basic code needed to add the JOptionPane to a JDialog.

How to create popup hint window like Quick Doc in Intellij Idea?

I am developing a simple swing application and I would like to have something which can help user with it.
As example a little popup window will appear over "Start" button when user runs app first time and say "Hey, click here to start playing with me!"
Do you know the way to create something like quick doc in Intellij Idea?
Could you please put me on the right way to sources, examples, source codes or anything else which could be useful?
Below is example of how it can look like
PS. I have updated the picture.
As I said in the comment use javax.swing.PopupFactory to show popup for any component (which is probably not pointed by the mouse)
Popup p = PopupFactory.getSharedInstance().getPopup(component, new JLabel("It's a hint!"), 5, 5);
p.show();
component is the widget for which the popup must be shown.
You can also use the javax.swing.Timer to hide this popup automatically.
Tooltips are used in Swing to give the hover-over text that you are looking for. It is very easy to use a tooltip, all you have to do is set the tooltip text on your button
btnStart.setToolTipText("Hey, click here to start playing with me!");
Here is a pretty useful guide that explains the topic in more depth http://docs.oracle.com/javase/tutorial/uiswing/components/tooltip.html

JOptionPane vs. JDialog

This is a crosspost to the thread in Javaranch (includes some images): http://www.coderanch.com/t/567472/GUI/java/Optimal-solution-creating-multiple-dialog
I'm trying to develop a simple swing desktop application where I imagine alot of different dialog's jumping around to fetch user input. Would need to present labels, textfields, passwordfields, combobxes, checkboxes etc in various dialog windows.
For example: creating the database firsthand, creating the first admin account, adding users, changing user accounts etc.
I have an understanding that JOptionPane is used to create simple quick & easy modal dialog's. I would really like to know why one would choose one over another in this case. Which one is more preferable to use: JOptionPane vs. JDialog
Also I could use some pointers how one should appropriately design and implement this.
Thank you.
Here's a statement I found on the Java website that says one key point about the difference between the two.
How to make Dialogs
A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program. JOptionPane creates JDialogs that are modal. To create a non-modal Dialog, you must use the JDialog class directly.
So it sounds like you would use JOptionPane if you want a user to have to make a choice and close the box before returning to the main screen. If you use a JDialog box, then they can just click around it and get back to the main screen without making a choice. For example, say you wanted to make a user choose the number of results before clicking submit, you wouldn't want them to be able to click around that window and click submit. You would use JOptionPane to force them to select a value first before going back to submit.
Check out http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html it pretty much has everything you would need.
As i understand it, JOptionPane is great for what it can do, but you can't really change the functionality beyond that (not easily). JDialog is better to inherit from if you want to create your own custom Dialogs.

How to copy text from a browser with Java?

Is there a way to copy text from a browser to my Java app ?
For example, at the left side of my screen I open a browser to point to a URL and shows the content of that page, it might be in a frame or CSS or simple html, on the right side of the screen I open a Java Swing application. I'm interested in certain parts of the browser window that shows some text, and I want my Java app [ without me doing anything ] to copy and paste the text into itself, can it be done ?
I know I can use JEditorPane or JTextPane and set it with an HTMLEditorKit, then load the text into the pane, but if the page uses Frames or some other complex ways, the text I get from the Pane is not what I see on the page, so I don't want to do it by loading the URL into my Java app, instead, I wonder if it can be done in the way I mentioned above ?
I think you're looking at the problem from the wrong angle. If what you want is to harvest a website, I suggest you have a look at the awesome library web-harvest. With a little Xpath wizardry you can get everything you want.
Doing what you describe would imply inter-process communication that seems like an overkill. There are more ways to download a web-page content than the browser.
You may try the following depending on your needs.
With java.awt.Robot you can either 1) Take an screenshot if what you neeed is the content ( without the text, just an image of the browser content ) or 2) Move your self into the browser and programatically press: CTRL-A + CTRL-C and return back to your swing app focus and programatically press: CTRL-V ( or CMD or whatever makes sense in your OS )
But again, this might or not work, depending on what you need.
I know a tool but i am not sure it meet your needs. Have you heard about selenium? http://seleniumhq.org/ It can replicate actions taken by the user in a browser and then manipulate them ussing java code. Have a look at the link it may be handy.
Using java.awt.Robot & a TextField will get the job done, not sure if there is any other way. Have robot press ctrl+a then ctrl+c, bring TextField into focus, and finally have robot press ctrl+v. Now from here you can create a button.setOnAction to save the TextField text into a string. Or you can use a change listener on the TextField setOnKeyReleased to do the same.

Disable publishing JOptionPane on application

Greetings everyone. I have a problem which i can solve. I need that JOptionPane does not showing in my application maybe there some way to make this. Best regards Alejandro Del Rio.
Normally a JOptionPane is shown by using one the the showXXX static methods. Using this approach you don't have a reference to the actual dialog so you can't just hide the option pane.
Read the JOptionPane API documentation. There you will find a "Direct Use" example of using a JOptionPane. In this case you are responsible for more code to handle the showing of the dialog and for handling the selected option button. But you do have a reference to the actual dialog so you can use setVisible( false ) as required.
Of course option panes are modal so you still need to somehow schedule the closing of the dialog, maybe by starting a Swing Timer before the option pane is displayed.
Have you tried setVisible() ?
JOptionPane optionPane = new JOptionPane();
...
...
...
optionPane.setVisible(false);
I just cant to setVisiable();
I have a class which i does not have a source code and there are many not needed Joptionpane shows i want not one of them is not showing in application.

Categories