//ScrollPaneTest.java //more showMessageDialog, showInputDialog //showConfirmDialog demo //JTextArea, JScrollPane demo import java.awt.*; import javax.swing.*; //public class ScrollPaneTest extends JApplet { public class ScrollPaneTest { //public void init() { public static void main(String[] argv) { /* Gussying-up showMessageDialog: 4 argument version of it: JOptionPane.showMessageDialog(null, String message, String title, JOptionPane.message_type); the message_type is: ERROR_MESSAGE INFORMATION_MESSAGE is the default for showMessageDialog WARNING_MESSAGE QUESTION_MESSAGE PLAIN_MESSAGE you get different icons with the different message types (except PLAIN_MESSAGE has no icon). Examples: */ //2 argument version that we're familiar with: JOptionPane.showMessageDialog(null, "whatever message here"); //4 arguments: JOptionPane.showMessageDialog(null, "some message here", "some title here", JOptionPane.PLAIN_MESSAGE); JOptionPane.showMessageDialog(null, "I'm sorry but that is no good", "Invalid input", JOptionPane.ERROR_MESSAGE); String input; //1 argument showInputDialog that we're familiar with: input = JOptionPane.showInputDialog("Enter your name"); //showInputDialog has the 4 argument version also: input = JOptionPane.showInputDialog( null, "Enter your name", "Name Entry Window", JOptionPane.PLAIN_MESSAGE); //QUESTION_MESSAGE is default //showConfirmDialog only allows user to click Yes or No int userAnswer; //note it's an int variable. userAnswer = JOptionPane.showConfirmDialog(null, "Do you want to do X", "some title message", JOptionPane.YES_NO_OPTION); //check value returned by showConfirmDialog if (userAnswer == JOptionPane.YES_OPTION) JOptionPane.showMessageDialog(null,"user clicked Yes"); else JOptionPane.showMessageDialog(null,"user clicked No"); /* custom image in JOptionPane: ImageIcon myimageicon; myimageicon = new ImageIcon(getClass().getResource("trees.gif")); JOptionPane.showMessageDialog(null, "whatever message or textarea/scrollpane", "title bar message", JOptionPane.PLAIN_MESSAGE, myimageicon); //5th parameter is icon image */ //Adding a JTextArea //declare and instantiate a JTextArea object: JTextArea mytextarea = new JTextArea(10,60); //optional: rows,cols //optional: allow line wrap: mytextarea.setLineWrap( true ); //optional: different font: mytextarea.setFont( new Font("Courier",Font.BOLD,18) ); //setText method puts string into the textarea object: //wipes out any already there mytextarea.setText("hello"); //append method adds more text into the textarea object: for (int i=1; i<25; i++) mytextarea.append ("\nasdfasdfasdf "+i); //the JTextArea object can then be put into a showMessageDialog JOptionPane.showMessageDialog(null, mytextarea, "JTextArea in a JOptionPane", JOptionPane.PLAIN_MESSAGE); //Add a scrollbar for the textarea: JScrollPane myscrollpane = new JScrollPane(mytextarea); //do the same stuff with the JTextArea object as before... //But, attach the JScrollPane object to showMessageDialog: JOptionPane.showMessageDialog(null, myscrollpane, "JTextArea in a JScrollPane in a JOptionPane", JOptionPane.PLAIN_MESSAGE); } }