2

I have a button and on click I want it to load a text field (like a pop up). Is this possible?

I've looked into

this.setVisible(false);
new className().setVisible(true);

But this means I'd need a new JFrame. I'm not against the idea - I'm just asking if there is a better way of doing it. If possible, I want it to load JUST a text field instead of a the frame that is around it.

I've just created JOptionPane as suggested below - But i'm still having problems. The pop up appears, but nothing is included in the box.

When mouse is clicked on class 1:

JOptionPane.showMessageDialog(this, MetaData.getTags());

class 2 (I'm aware this isn't the best way to throw exceptions. This is only a rough program). Using metadata extractor for those who are interested.

public String getTags() {
        try {
            Metadata metadata = ImageMetadataReader.readMetadata(jpegFile);

            for (Directory directory : metadata.getDirectories()) {
                for (Tag allTags : directory.getTags()) {
                    System.out.println("MetaData Class: " + allTags);
                }
            }
        } catch (ImageProcessingException ex) {
            Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MetaData.class.getName()).log(Level.SEVERE, null, ex);
        }
        return allTags;
    }
Steven Mclaren
  • 275
  • 2
  • 6
  • 13

1 Answers1

2

If you want to get user input with a popup, you may use JOptionPane.showInputDialog. It will essentially instantiate a JDialog with an input text field and OK/Cancel buttons.

If you don't like this "pre-made" library method, you have to roll your own JDialog: make it modular and generic, so that if in the future you need the same tool you won't have to make it from scratch or copy/paste.
Please note that you can have a dialog without the external frame (if you are looking for something less intrusive than a window, like a tooltip). See http://www.stupidjavatricks.com/?p=4.

I don't see other options.

gd1
  • 11,300
  • 7
  • 49
  • 88
  • I've read the update, but I just don't get what you are trying to do. Weren't you just trying to get user input from a popup/tooltip? – gd1 Oct 09 '12 at 17:33
  • Nope. Data is already retrieved but not printed. I wanted this button to create a report of some type. I did try writing it to the document but it came back as "null". This would suggest there's nothing there, but it prints in NetBean output screen fine. – Steven Mclaren Oct 10 '12 at 19:44