简体   繁体   中英

how to make a pop-up window using java

I wanted a button to show a pop-up window, I tried using the

JOptionPane.showMessageDialog(null,"");

But I can't put my desired object such as the Table and List. Is it possible?

Start by taking a closer look at the JOptionPane JavaDocs , showMessageDialog clearly states that it accepts a Object as the message parameter

One of the nice features of this, is if the Object is Component , it will be added to the dialog.

For example: JOptionPane displaying HTML problems in Java and How do i make the output come in different columns?

This is really poorly documented in the Java Docs , because all that tells you is that the "message" parameter is an Object, which can be anything - but does not go into specifics about what happens with different types of objects that may warrant special case handling.

As far as I have gathered from experimenting with it, the "message" can be a subtype of Component - then it will just place the component in the message area of the dialog box as-is, like:

JOptionPane.showMessageDialog(null, new JCheckBox("I'm a checkbox!"));

Otherwise, it will just call the toString method on the object, converting it into a string, which it then will just wrap in a label and place that in the dialog as the message.

But you can also pass in an Array of Objects, in which case it will just place each element in a separate row in the message area:

JOptionPane.showMessageDialog(null, new Object[] {
    new JCheckBox("check"),
    new JRadioButton("radio"),
    "plain text"});

There might be other special cases, but I haven't found them yet.

That said, if what you want to display is a subclass of Component (or JComponent), just passing it in as the message parameter should work. If it doesn't, you might want to edit your question to describe whatever problems you are encountering in more detail, perhaps also providing some sample code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM