18

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()?
Community
  • 1
  • 1
eleven81
  • 6,301
  • 11
  • 37
  • 48

2 Answers2

24
  • 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 )

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • I can Alt+Tab to it in XP also; that's nothing new. Sometimes I forget it's there and launch it again, only to find a few unused instances of it when I minimize everything else. – eleven81 Feb 12 '09 at 20:13
  • I was wondering if you couldn't ( which would be very strange ). So, what do you think about using the frame as shown? If you pack that on function, you can pretty much have the same one line behavior you expect ( aflter all that's exactly what "showInputDialog does" pack a bunch of calls into one. – OscarRyz Feb 12 '09 at 20:20
  • I find that your code compiles perfectly without importing java.awt.* by the way. – eleven81 Feb 12 '09 at 20:21
  • ahh yes. Probably I initally use showXX( ( Component ) null , etc, etc ) and didn't remove the awt stuff – OscarRyz Feb 12 '09 at 20:22
  • Adding the frame and having it be the parent of the JOptionPane dialogs works great for what I am doing. – eleven81 Feb 12 '09 at 20:24
  • What did you do there with the double braces and method calls? Is that called an inner class? Javac creates a file OptionTest$1.class. I eliminated that by using frame.setUndecorated(true); frame.setVisible(true); frame.setLocationRelativeTo(null); and eliminating that block. – eleven81 Feb 12 '09 at 20:25
  • Ahh yes, that's better. I was trying to create an "online" instance like: showXXXDialog( new JFrame("t"){{setUndec.. setVisi. etc}}, "dialog", "title" ); ] But then I realize the reference is needed for the "dispose". So I create the variable but just didn't feel like correcting the double brace :) – OscarRyz Feb 12 '09 at 20:36
  • And yes that's an "anonymous inner class" for you cannot reference it by name. The compiler created a .class with a name like the one you've described. – OscarRyz Feb 12 '09 at 20:45
1

This question is platform dependent. On my system, Debian Linux with OpenBox, JOptionPanes always show up on taskbar. I can't prevent any JDialog from appearing on taskbar.

Jarekczek
  • 7,456
  • 3
  • 46
  • 66
  • 1
    I just made another test. Fired a one-line java app: javax.swing.JOptionPane.showMessageDialog(null, "test"). XP and my Linux behave differently with it. That's no surprise, as taskbar is operating system element, not mentioned in java documentation. As such it's platform dependent. Why do people with 11k reputation make such newbie-style, hasty and confusing comments? – Jarekczek Oct 17 '11 at 16:21
  • The comment I was talking about has been deleted. – Jarekczek Oct 18 '11 at 14:46