The following example demonstrates the close, mark, markSupported, read, ready, and reset methods of
the BufferedReader class.
// bufferedreader_overview.jsl
import java.io.*; public class Program { public static void main(String[] args) { try { // Create a new instance of a BufferedReader object using // a StringReader. String s = "This is the internal StringReader buffer."; StringReader stringReader = new StringReader(s); BufferedReader bufReader = new BufferedReader(stringReader);
// Read from the underlying StringReader. char[] arr = new char[s.length()]; if (bufReader.ready()) { bufReader.read(arr, 0, arr.length - 10); } // Call mark after having read all but the last 10 // characters from the StringReader. if (bufReader.markSupported()) { bufReader.mark(s.length()); } // Read the rest of the data from the underlying // StringReader. if (bufReader.ready()) { bufReader.read(arr, arr.length - 10, 10); } System.out.println(arr);
// Call reset and then re-read from the underlying // StringReader. bufReader.reset(); char[] arr2 = new char[s.length()]; bufReader.read(arr2); System.out.println(arr2);
// Close the BufferedReader object and the underlying // StringReader object. stringReader.close(); bufReader.close(); } catch (IOException ex) { System.out.println(ex.toString()); } } } /* Output: This is the internal StringReader buffer. er buffer. */ JOptionPane Java Swing Tutorial Explaining the JOptionPane Component. JOptionPane Contains classes used for a graphical user interface (GUI) Facilitates data entry and data output. class JOptionPane contains methods that display a dialog box. The JOptionPane class provides static methods to display each type of dialog box. Swing JOptionPane allows you to Allows you to create a dialog box Display information Request information Give the user a choice with buttons JOptionPnae Example Dialogs String showInputDialog prompt for input return a string void showMessageDialog display a message wait for an acknowledgement int showConfirmDialog ask a yes/no question wait for a response
JOptionPane Source Code The JOptionPane class is not automatically available to your Java programs.The following statement must be before the programs class header: This statement tells the compiler where to find the JOptionPane class import javax.swing.*; import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; import java.util.StringTokenizer;
public class JOptionPaneDemo extends JFrame implements ActionListener {
Container contentPane = null; private JButton jbnDialog; String ButtonLabels; private JRadioButton[] dialogTypeButtons; private JRadioButton[] messageTypeButtons; private int[] messageTypes = { JOptionPane.PLAIN_MESSAGE, JOptionPane.INFORMATION_MESSAGE, JOptionPane.QUESTION_MESSAGE, JOptionPane.WARNING_MESSAGE, JOptionPane.ERROR_MESSAGE }; private ButtonGroup messageTypeButtonGroup, buttonTypeButtonGroup, dialogTypeButtonGroup; private JRadioButton[] optionTypeButtons; private int[] OptionTypes = { JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.OK_CANCEL_OPTION }; public static void main(String[] args) { new JOptionPaneDemo(); } public JOptionPaneDemo() { super("JOptionPane Source Demo"); addWindowListener(new WindowListener()); contentPane = getContentPane(); contentPane.setLayout(new GridLayout(0, 1)); JPanel jplButton = new JPanel(); jbnDialog = new JButton("Show an Option Pane"); jbnDialog.addActionListener(this); jplButton.add(jbnDialog); contentPane.add(jplButton); createRadioButtonGroupings(); ButtonLabels = "Button1 Button2 Button3"; pack(); setVisible(true); } public void createRadioButtonGroupings() { JPanel jplDialogType = new JPanel(); dialogTypeButtonGroup = new ButtonGroup(); dialogTypeButtons = new JRadioButton[] { new JRadioButton("Show Message", true), new JRadioButton("Get Confirmation"), new JRadioButton("Collect Input"), new JRadioButton("Present Options") }; for (int i = 0; i < dialogTypeButtons.length; i++) { dialogTypeButtonGroup.add(dialogTypeButtons[i]); jplDialogType.add(dialogTypeButtons[i]); } contentPane.add(jplDialogType); JPanel jplMessageType = new JPanel(); messageTypeButtonGroup = new ButtonGroup(); messageTypeButtons = new JRadioButton[] { new JRadioButton("Plain"), new JRadioButton("Information", true), new JRadioButton("Question"), new JRadioButton("Warning"), new JRadioButton("Error") }; for (int i = 0; i < messageTypeButtons.length; i++) { messageTypeButtonGroup.add(messageTypeButtons[i]); jplMessageType.add(messageTypeButtons[i]); } contentPane.add(jplMessageType); JPanel jplButtonType = new JPanel(); buttonTypeButtonGroup = new ButtonGroup(); optionTypeButtons = new JRadioButton[] { new JRadioButton("Default", true), new JRadioButton("Yes/No"), new JRadioButton("Yes/No/Cancel"), new JRadioButton("OK/Cancel") }; for (int i = 0; i < optionTypeButtons.length; i++) { buttonTypeButtonGroup.add(optionTypeButtons[i]); jplButtonType.add(optionTypeButtons[i]); } contentPane.add(jplButtonType); } // Windows Listener for Window Closing public class WindowListener extends WindowAdapter {
public void windowClosing(WindowEvent event) { System.exit(0); } } public void actionPerformed(ActionEvent event) { /* * dialogTypeButtons = * * new JRadioButton[] { new JRadioButton("Show Message", true), * * new JRadioButton("Get Confirmation"), * * new JRadioButton("Collect Input"), * * new JRadioButton("Present Options") }; */ if (dialogTypeButtons[0].isSelected()) { JOptionPane.showMessageDialog(this, "Show Message", "Simple Dialog", getMessageType()); } else if (dialogTypeButtons[1].isSelected()) { JOptionPane.showConfirmDialog(this, "Get Confirmation", "Simple Dialog", getButtonType(), getMessageType()); } else if (dialogTypeButtons[2].isSelected()) { JOptionPane.showInputDialog(this, "Collect Input", "Simple Dialog", getMessageType(), null, null, null); } else if (dialogTypeButtons[3].isSelected()) { JOptionPane.showOptionDialog(this, "Present Options", "Simple Dialog", getButtonType(), getMessageType(), null, substrings(ButtonLabels), null); } } private int getAssociatedType(AbstractButton[] buttons, int[] types) { for (int i = 0; i < buttons.length; i++) { if (buttons[i].isSelected()) { return (types[i]); } } return (types[0]); } private int getMessageType() { return (getAssociatedType(messageTypeButtons, messageTypes)); } private int getButtonType() { return (getAssociatedType(optionTypeButtons, OptionTypes)); } private String[] substrings(String string) { StringTokenizer tok = new StringTokenizer(string); String[] substrings = new String[tok.countTokens()]; for (int i = 0; i < substrings.length; i++) substrings[i] = tok.nextToken(); return (substrings); } } Output