Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make JOptionPane dialogs show up as a task on the taskbar?

Edit:

The question follows the horizontal rule; my own answer precedes it.


Based on help from Oscar Reyes, I crafted this solution:

import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class MyApp extends JFrame {
    public static void main(String [] args) {
        new MyApp();
    }

    public MyApp() {
        super("MyApp");
        setUndecorated(true);
        setVisible(true);
        setLocationRelativeTo(null);
        String i = JOptionPane.showInputDialog(this, "Enter your name:", getTitle(), JOptionPane.QUESTION_MESSAGE);
        if(null != i) {
            JOptionPane.showInputDialog(this, "Your name is:", getTitle(), JOptionPane.INFORMATION_MESSAGE, null, null, i.concat(i));
        }
        dispose();
    }
}

Notice I display my output in a JOptionPane.showInputDialog also. This way the output is highlighted in a text field so I can simply press CTRL+C to copy the output to the system clipboard and them press ESC to dismiss the application.


I've created a trivial GUI for my trivial application. My application prompts for a single input with a JOptionPane.showInputDialog, performs a calculation, and then displays a single output with a JOptionPane.showMessageDialog. I sometimes switch to a maximized browser window or something else to copy from, and then want to switch back to my JOptionPane dialog to paste into.

I want to have my JOptionPane dialog show up as a task on the taskbar so I could switch to it like nearly any other running program. I prefer JOptionPane's simplicity, rather than having to create a JFrame, a FlowLayout, an Icon, a JTextField, a JButton, an ActionListener, and so on, and so on.

  • Can JOptionPane show up as a task on the taskbar?
  • If so, how do I get it to show up?
  • If not, is there anything else that's a one-liner like JOptionPane.show[whatever]dialog()?
like image 453
eleven81 Avatar asked Feb 12 '09 19:02

eleven81


People also ask

How do you add a button in JOptionPane?

showOptionDialog( panel, "Choose one of the following options for Category " + category + ". \n" + "If skip is available, you may choose it to skip this category.", "Select option", optionType, JOptionPane. INFORMATION_MESSAGE, null, options, options[0]);

Are all dialogs created by JOptionPane modal?

All the dialogs that JOptionPane provides are modal. To create a non-modal dialog, you must use the JDialog class directly. class. It adds to Dialog a root pane and support for a default close operation.


1 Answers

  • Can JOptionPane show up as a task on the taskbar?

No

  • If not, is there anything else that's a one-liner like JOptionPane.show[whatever]dialog()?

Not exactly one liner ( but some extra 6 lines :P )

I bet you cam put all this in a utlity method pretty easy and call it whenever you need it with a single call.

The following code would add a taskbar for your app.

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class OptionTest { 
    public static void main ( String [] args ) { 

        JFrame frame = new JFrame("My dialog asks....");

        frame.setUndecorated( true );
        frame.setVisible( true );
        frame.setLocationRelativeTo( null );


        String message = JOptionPane.showInputDialog(frame,
            "Would this be enough?.", 
            "My dialog asks....", 
            JOptionPane.INFORMATION_MESSAGE);

        System.out.println( "Got " + message );

        frame.dispose();
    }
}

By the way in Windows Vista, I can switch to the OptionPane using Alt+Tab without anything else ( although I cannot see it in the taskbar as you said )

like image 63
OscarRyz Avatar answered Sep 20 '22 10:09

OscarRyz