Swing Application Window - Yash-777/LearnJava GitHub Wiki
Creates a JOptionPane with the specified buttons, icons, message, title, and so on. You must then add the option pane to a JDialog, register a property-change listener on the option pane, and show the dialog. See Stopping Automatic Dialog Closing for details.
public class UserConformation {
public static void main(String[] argv) throws Exception {
int i = okcancel("User Conformation", "Are you sure you want to quit?");
System.out.println("User Action : OK/YES[0], NO[1], Cancel[2], [X][-1] Value: "+i);
printerSelection();
}
public static int okcancel(String title, String msg) {
int result = JOptionPane.showConfirmDialog(null, msg, title,
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
return result;
}
public static void printerSelection() {
String bigList[] = new String[10];
for (int i = 0; i < bigList.length; i++) {
bigList[i] = Integer.toString(i);
}
Object showInputDialog = JOptionPane.showInputDialog(null, "Pick a printer", "Input",
JOptionPane.QUESTION_MESSAGE, null, bigList, "Titan");
System.out.println("User Choosen Vlaue : "+ showInputDialog);
}
}
The table that follows lists every example in the Using Swing Components lesson, with links to required files and to where each example is discussed. The first column of the table has links to JNLP files that let you run the examples using Java Web Start.
Example to Shutdown system in X Seconds: Stackpost
public class UserConformation extends JFrame {
static Container contentPane;
public UserConformation() {
contentPane = getContentPane();
contentPane.setBackground(Color.DARK_GRAY);
setTitle("Confirm Dialog in Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
setSize(400, 300);
contentPane.setLayout(null);
}
static boolean isToShutDown = true;
public static void main(String[] argv) throws Exception {
shutDownSys(10);
int i = okcancel("User Conformation", "Are you sure to shutdown the System?");
System.out.println("User Action : OK/YES[0], NO[1], Cancel[2], [X][-1] Value: "+i);
if (i == 2) {
isToShutDown = false;
}
}
public static int okcancel(String title, String msg) {
int result = JOptionPane.showConfirmDialog(new UserConformation(), msg, title,
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
return result;
}
public static void shutDownSys(int sec) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (isToShutDown) {
/*ProcessBuilder processBuilder = new ProcessBuilder("shutdown","/s");
processBuilder.start();*/
System.err.println("Time out: Shutdown the system");
} else {
System.out.println("Time out: User canceld for System Shutdown");
}
System.exit(0);
}
}, sec * 1000);
System.out.println(" Shutting down in " + sec + " seconds");
}
}