简体   繁体   中英

Getting input from JTextArea in a JOptionPane

I was writing an editor that lets the user design a selection/bidding table. Part of this program was the ability to add sub categories, which have a name and description. I have done more sophisticated things like this before but because i needed this done in a few days I decided to try and go with using a JOptionPane and passing in a JTextField for the name and a JTextArea for the possibility of a lengthy description. Here is the code I have currently.

    JPanel mainPanel = new JPanel( new GridBagLayout());
    mainPanel.setBorder( new TitledBorder("Set Creation Pane") );

    JTextField setNameField = new JTextField( 20 );
    JLabel dialogLabel1, dialogLabel2;
    dialogLabel1 = new JLabel("Create a new set called");

    if(possibleSuperSetName == null || possibleSuperSetName.length() == 0)
    {   
        dialogLabel2 = new JLabel("at a global level");
    }
    else
    {
        dialogLabel2 = new JLabel("that is a subset to "+possibleSuperSetName);
    }

    JLabel description = new JLabel("Description for set");
    JTextArea textArea = new JTextArea("Testing Testing 123");
    textArea.setColumns(80);
    textArea.setRows(10);
    textArea.setFont( new Font( Font.MONOSPACED, Font.PLAIN, textArea.getFont().getSize() ) );
    textArea.setEditable( true );
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord( true );

    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.CENTER;
    c.gridx = 0;
    c.gridy = 0;
    mainPanel.add( dialogLabel1, c );
    c.gridx = 1;
    mainPanel.add( setNameField, c );
    c.gridx = 2;
    mainPanel.add( dialogLabel2, c );
    c.gridx = 0;
    c.gridy = 1;
    mainPanel.add(description, c);
    c.gridy = 2;
    c.gridwidth = 3;
    mainPanel.add(description, c);

    int option = JOptionPane.showConfirmDialog( null,
                                              mainPanel,
                                              "Set Creation",
                                              JOptionPane.OK_CANCEL_OPTION );

    if( option == JOptionPane.OK_OPTION )
    {
        ItemSet newSet = new ItemSet();
        newSet.setSetName(setNameField.getText());
        newSet.setDescription(textArea.getText());
        caller.addSet(newSet);
    }       

When i run this code, the OptionPane opens correctly and works fine with the JTextField, but the JTextArea will not show up at all. Is there any reason why this is?

You don't add your textArea in any panel/dialog etc. that's why It doesn't show up on the screen.

    c.gridx = 0;
    c.gridy = 1;
    mainPanel.add(description, c);
    c.gridy = 2;
    c.gridwidth = 3;
    //change it mainPanel.add(description, c);
    mainPanel.add(textArea, c);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM