Warning message in java

Class JOptionPane

JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. For information about using JOptionPane , see How to Make Dialogs, a section in The Java Tutorial.

While the JOptionPane class may appear complex because of the large number of methods, almost all uses of this class are one-line calls to one of the static showXxxDialog methods shown below:

Common JOptionPane method names and their descriptions
Method Name Description
showConfirmDialog Asks a confirming question, like yes/no/cancel.
showInputDialog Prompt for some input.
showMessageDialog Tell the user about something that has happened.
showOptionDialog The Grand Unification of the above three.

Each of these methods also comes in a showInternalXXX flavor, which uses an internal frame to hold the dialog box (see JInternalFrame ). Multiple convenience methods have also been defined — overloaded versions of the basic methods that use different parameter lists.

All dialogs are modal. Each showXxxDialog method blocks the caller until the user’s interaction is complete.

Common dialog
icon message
input value
option buttons

The basic appearance of one of these dialog boxes is generally similar to the picture above, although the various look-and-feels are ultimately responsible for the final result. In particular, the look-and-feels will adjust the layout to accommodate the option pane’s ComponentOrientation property.

Читайте также:  Javascript this text undefined

Parameters:
The parameters to these methods follow consistent patterns:

  • ERROR_MESSAGE
  • INFORMATION_MESSAGE
  • WARNING_MESSAGE
  • QUESTION_MESSAGE
  • PLAIN_MESSAGE
  • DEFAULT_OPTION
  • YES_NO_OPTION
  • YES_NO_CANCEL_OPTION
  • OK_CANCEL_OPTION

When the selection is changed, setValue is invoked, which generates a PropertyChangeEvent .

If a JOptionPane has configured to all input setWantsInput the bound property JOptionPane.INPUT_VALUE_PROPERTY can also be listened to, to determine when the user has input or selected a value.

  • YES_OPTION
  • NO_OPTION
  • CANCEL_OPTION
  • OK_OPTION
  • CLOSED_OPTION
JOptionPane.showInternalMessageDialog(frame, "information", "information", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);

Show an internal information dialog with the options yes/no/cancel and message ‘please choose one’ and title information:

JOptionPane.showInternalConfirmDialog(frame, "please choose one", "information", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

Show a warning dialog with the options OK, CANCEL, title ‘Warning’, and message ‘Click OK to continue’:

Object[] options = < "OK", "CANCEL" >; JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);

Show a dialog asking the user to type in a String: String inputValue = JOptionPane.showInputDialog(«Please input a value»); Show a dialog asking the user to select a String:

Object[] possibleValues = < "First", "Second", "Third" >; 
Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);

Direct Use:
To create and use an JOptionPane directly, the standard pattern is roughly as follows:

JOptionPane pane = new JOptionPane(arguments); pane.set.Xxxx(. ); // Configure JDialog dialog = pane.createDialog(parentComponent, title); dialog.show(); Object selectedValue = pane.getValue(); if(selectedValue == null) return CLOSED_OPTION; //If there is not an array of option buttons: if(options == null) < if(selectedValue instanceof Integer) return ((Integer)selectedValue).intValue(); return CLOSED_OPTION; >//If there is an array of option buttons: for(int counter = 0, maxCounter = options.length; counter < maxCounter; counter++) < if(options[counter].equals(selectedValue)) return counter; >return CLOSED_OPTION;

Warning: Swing is not thread safe. For more information see Swing’s Threading Policy.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see XMLEncoder .

Источник

Swing Examples — Show Warning message Dialog

Following example showcase how to show a warning message alert in swing based application.

We are using the following APIs.

  • JOptionPane − To create a standard dialog box.
  • JOptionPane.showMessageDialog() − To show the message alert.
  • JOptionPane.WARNING_MESSAGE − To mark the alert message as warning.

Example

import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.LayoutManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class SwingTester < public static void main(String[] args) < createWindow(); >private static void createWindow() < JFrame frame = new JFrame("Swing Tester"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); createUI(frame); frame.setSize(560, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); >private static void createUI(final JFrame frame) < JPanel panel = new JPanel(); LayoutManager layout = new FlowLayout(); panel.setLayout(layout); JButton button = new JButton("Click Me!"); button.addActionListener(new ActionListener() < @Override public void actionPerformed(ActionEvent e) < JOptionPane.showMessageDialog(frame, "Please ensure compliance!", "Swing Tester", JOptionPane.WARNING_MESSAGE); >>); panel.add(button); frame.getContentPane().add(panel, BorderLayout.CENTER); > >

Output

Show a warning message alert

Источник

When to suppress warnings in Java

Caution - when to suppress warnings in Java

You may have seen the @SuppressWarnings annotation before. Have you wondered how and why to suppress warnings in Java?

Errors and warnings

The Java compiler is very strict. It generates errors and stops compiling if there’s something seriously wrong with your code. However, sometimes it just warns you of potential problems.

Compiler warning messages are usually helpful. But sometimes warnings can be noisy, especially when you can’t or don’t want to address them.

For example, the compiler will warn you if you use a deprecated class or method. Re-compiling with the -Xlint or the more specific -Xlint:deprecation flag will give you some extra information. But what happens if you can’t or don’t want to re-write the offending code, but do want to get rid of the warning?

Warning types

You can decide to suppress warnings. The purpose of the @SuppressWarnings annotation to disable specific compiler warnings. You can use it to disable or suppress warnings for a specific part of a class. It can be used on types, fields, constructors, methods, parameters and local variables. It allows you to specify which warnings you’d like the compiler to ignore. The annotation takes a single String[] which you use to specify all the warnings you’d like to ignore.

Warning types vary from compiler to compiler, but a few of the most common include:

  • deprecation warns when you’re using a deprecated method or class.
  • unchecked tells you when you’re using raw types instead of parameterized types. An unchecked warning says that a cast may cause a program to throw an exception somewhere else. Suppressing the warning with @SuppressWarnings(«unchecked») tells the compiler that you believe the code is safe and won’t cause unexpected exceptions.
  • rawtypes warns that you are using a raw type instead of a parameterized type. It is like unchecked , but it is used on fields and variables.
  • serial warns you about missing serialVersionUID definitions on serializable classes.

To get a list of the warnings that the compiler you’re using can generate, run javac -X on the command line.

Example of how to suppress warnings

For example, the following class generates four warnings: deprecation , unchecked , rawtypes and serial

public class ClassWithLotsOfWarnings implements Serializable < // no serialVersionUID field // a raw type private List list; public static void main(String args[]) < // deprecated constructor Date date = new Date (100, 11, 07); System.out.println(date); > public void add(String str) < // unchecked operation list.add(str); > > // end of class

To suppress all the warnings, you could do the following:

@SuppressWarnings() public class ClassWithLotsOfWarnings implements Serializable < . >// end of class 

Best practice for suppressing warnings

The best practice is to annotate the closest element to where you need to suppress the warning. For example, if you want to suppress a warning in a specific method, you should annotate the method, not the class.

To suppress the warnings at the most appropriate places in the code, you could do the following:

@SuppressWarnings("serial") public class ClassWithLotsOfWarnings implements Serializable < @SuppressWarnings("rawtypes") private List list; @SuppressWarnings("deprecation") public static void main(String args[]) < Date date = new Date (100, 11, 07); System.out.println(date); >@SuppressWarnings("unchecked") public void add(String str) < list.add(str); >> // end of class 

Now you know how to use @SuppressWarnings !

I’m always interested in your opinion, so please leave a comment. Your feedback helps me write tips that help you.

Источник

When to suppress warnings in Java

Caution - when to suppress warnings in Java

You may have seen the @SuppressWarnings annotation before. Have you wondered how and why to suppress warnings in Java?

Errors and warnings

The Java compiler is very strict. It generates errors and stops compiling if there’s something seriously wrong with your code. However, sometimes it just warns you of potential problems.

Compiler warning messages are usually helpful. But sometimes warnings can be noisy, especially when you can’t or don’t want to address them.

For example, the compiler will warn you if you use a deprecated class or method. Re-compiling with the -Xlint or the more specific -Xlint:deprecation flag will give you some extra information. But what happens if you can’t or don’t want to re-write the offending code, but do want to get rid of the warning?

Warning types

You can decide to suppress warnings. The purpose of the @SuppressWarnings annotation to disable specific compiler warnings. You can use it to disable or suppress warnings for a specific part of a class. It can be used on types, fields, constructors, methods, parameters and local variables. It allows you to specify which warnings you’d like the compiler to ignore. The annotation takes a single String[] which you use to specify all the warnings you’d like to ignore.

Warning types vary from compiler to compiler, but a few of the most common include:

  • deprecation warns when you’re using a deprecated method or class.
  • unchecked tells you when you’re using raw types instead of parameterized types. An unchecked warning says that a cast may cause a program to throw an exception somewhere else. Suppressing the warning with @SuppressWarnings(«unchecked») tells the compiler that you believe the code is safe and won’t cause unexpected exceptions.
  • rawtypes warns that you are using a raw type instead of a parameterized type. It is like unchecked , but it is used on fields and variables.
  • serial warns you about missing serialVersionUID definitions on serializable classes.

To get a list of the warnings that the compiler you’re using can generate, run javac -X on the command line.

Example of how to suppress warnings

For example, the following class generates four warnings: deprecation , unchecked , rawtypes and serial

public class ClassWithLotsOfWarnings implements Serializable < // no serialVersionUID field // a raw type private List list; public static void main(String args[]) < // deprecated constructor Date date = new Date (100, 11, 07); System.out.println(date); > public void add(String str) < // unchecked operation list.add(str); > > // end of class

To suppress all the warnings, you could do the following:

@SuppressWarnings() public class ClassWithLotsOfWarnings implements Serializable < . >// end of class 

Best practice for suppressing warnings

The best practice is to annotate the closest element to where you need to suppress the warning. For example, if you want to suppress a warning in a specific method, you should annotate the method, not the class.

To suppress the warnings at the most appropriate places in the code, you could do the following:

@SuppressWarnings("serial") public class ClassWithLotsOfWarnings implements Serializable < @SuppressWarnings("rawtypes") private List list; @SuppressWarnings("deprecation") public static void main(String args[]) < Date date = new Date (100, 11, 07); System.out.println(date); >@SuppressWarnings("unchecked") public void add(String str) < list.add(str); >> // end of class 

Now you know how to use @SuppressWarnings !

I’m always interested in your opinion, so please leave a comment. Your feedback helps me write tips that help you.

Источник

Оцените статью