Results 1 to 3 of 3
  1. #1
    johnt is offline Super Moderator
    Join Date
    Apr 2007
    Posts
    30
    Rep Power
    0

    Default Multiple Line Input Dialog Box

    I've got a simple dialog box that I'm creating by calling

    Java Code:
    JOptionPane.showInputDialog(...)
    I get a little edit box where the user can enter a simple string, and this works fine.

    I'd like to change this so instead of a 1 line edit box, I get a multiple line edit box (I guess this would be a JTextArea). How can I do this easily?

  2. #2
    levent Guest

    Default

    Here's something to play around with. It's a bit rough, but it might be enough to get you started.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    class Testing extends JFrame
    {
      JLabel lbl = new JLabel("Text entered = ");
      public Testing()
      {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600,300);
        setLocation(200,100);
        JPanel main = new JPanel(new BorderLayout());
        main.add(lbl,BorderLayout.CENTER);
        JButton btn = new JButton("Get some text");
        btn.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
            lbl.setText("<html>Text entered = <br>"+
            new MyJOptionPane().showInputDialog("Enter some text").replaceAll("\\n","<br>")+
            "</html>");}});
        main.add(btn,BorderLayout.SOUTH);
        getContentPane().add(main);
      }
      public static void main(String[] args){new Testing().setVisible(true);}
    }
    class MyJOptionPane extends JOptionPane
    {
      public static String showInputDialog(final String message)
      {
        String data = null;
        class GetData extends JDialog implements ActionListener
        {
          JTextArea ta = new JTextArea(5,10);
          JButton btnOK = new JButton("   OK   ");
          JButton btnCancel = new JButton("Cancel");
          String str = null;
          public GetData()
          {
            setModal(true);
            getContentPane().setLayout(new BorderLayout());
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            setLocation(400,300);
            getContentPane().add(new JLabel(message),BorderLayout.NORTH);
            getContentPane().add(ta,BorderLayout.CENTER);
            JPanel jp = new JPanel();
            btnOK.addActionListener(this);
            btnCancel.addActionListener(this);
            jp.add(btnOK);
            jp.add(btnCancel);
            getContentPane().add(jp,BorderLayout.SOUTH);
            pack();
            setVisible(true);
          }
          public void actionPerformed(ActionEvent ae)
          {
            if(ae.getSource() == btnOK) str = ta.getText();
            dispose();
          }
          public String getData(){return str;}
        }
        data = new GetData().getData();
        return data;
      }
    }

  3. #3
    johnt is offline Super Moderator
    Join Date
    Apr 2007
    Posts
    30
    Rep Power
    0

Similar Threads

  1. Creating a dialog to input user/password
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 08:03 AM
  2. Example of SWT Dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 01:01 PM
  3. JOptionPane - input dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 10:09 AM
  4. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 01:19 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •