What is the difference between joptionpane and jframe




















JFrame is basically a window used for stand-alone applications, examples of which include an alert window, or a notification window, that generally appears as pop out on our screen. JFrame make use of a windows listener that starts execution whenever you perform different operations on a window.

Another listener called mouse listener is used to make your framework in accordance with mouse actions. JFrame can have multiple inner frames, but they are completely dependent on the parent frame. JFrame is generally used as a window for hosting stand-alone applications, like an alert window or notification window.

Operations Support JFrame has different inner frames for performing different purposes. In the case of JPanel, one pane can hold multiple operations. Weight JPanel a lightweight container generally used to organize Graphical user interface components. JFrame is a heavyweight container used as the top-level window. How to Use Graphical user interface components are added on one or more JPanels. A fourth method, showInputDialog , is designed to display a modal dialog that gets a string from the user, using either a text field, an uneditable combo box or a list.

Here are some examples, taken from DialogDemo. For more example code, see DialogDemo. The arguments to all of the show Xxx Dialog methods and JOptionPane constructors are standardized, though the number of arguments for each method and constructor varies.

The following list describes each argument. The JOptionPane constructors do not include this argument. You can either let the option pane display its default icon or specify the icon using the message type or icon argument.

By default, an option pane created with showMessageDialog displays the information icon, one created with showConfirmDialog or showInputDialog displays the question icon, and one created with a JOptionPane constructor displays no icon. To specify that the dialog display a standard icon or no icon, specify the message type corresponding to the icon you desire.

To specify a custom icon, use the icon argument. The icon argument takes precedence over the message type; as long as the icon argument has a non-null value, the dialog displays the specified icon.

When you use JOptionPane to create a dialog, you can either use the standard button text which might vary by look and feel and locale or specify different text.

By default, the option pane type determines how many buttons appear. The following code, taken from DialogDemo. The first dialog is implemented with showConfirmDialog , which uses the look-and-feel wording for the two buttons.

The second dialog uses showOptionDialog so it can customize the wording. With the exception of wording changes, the dialogs are identical. As the previous code snippets showed, the showMessageDialog , showConfirmDialog , and showOptionDialog methods return an integer indicating the user's choice. Even if you change the strings that the standard dialog buttons display, the return value is still one of the pre-defined integers. The only form of show Xxx Dialog that does not return an integer is showInputDialog , which returns an Object instead.

This Object is generally a String reflecting the user's choice. Here is an example of using showInputDialog to create a dialog that lets the user choose one of three strings:.

If you do not care to limit the user's choices, you can either use a form of the showInputDialog method that takes fewer arguments or specify null for the array of objects. In the Java look and feel, substituting null for possibilities results in a dialog that has a text field and looks like this:. Because the user can type anything into the text field, you might want to check the returned value and ask the user to try again if it is invalid.

Another approach is to create a custom dialog that validates the user-entered data before it returns. See CustomDialog. If you're designing a custom dialog, you need to design your dialog's API so that you can query the dialog about what the user chose. For example, CustomDialog has a getValidatedText method that returns the text the user entered. I'd use the newer ModalityType constructs in lieu of the boolean modal APIs -- finer grain control and clearer intent in your code.

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. Linked 0. JFrames never block access. Campbell Ritchie wrote: Yes, but you will have a very peculiar interface. SCJP Visit my download page.

JFrame vs JOptionPane. Alan Smith. I like



0コメント

  • 1000 / 1000