Search code examples
javaswingdialogawtjoptionpane

Display both comments on dialogbox(Java)


comment 1 comment 2

How do i make both comments display on the dialog box?

Below is my code for reference.

private class HandleTextField implements ActionListener
{
    @Override
    public void actionPerformed (ActionEvent e)
    {
        String string  = "";

        if (e.getSource () == textFieldArray [0])
        {
            string = String.format("1. %s", e.getActionCommand());
        }
        else if (e.getSource () == textFieldArray [1])
        {
            string = String.format("2. %s", e.getActionCommand());
        }

        Object [] fields ={
            "Summary of my changes" , string
        };

        JOptionPane.showMessageDialog(null, fields, "My sugestion to the course", JOptionPane.WARNING_MESSAGE);
    }

}

}


Solution

  • The following untested code will put the contents of both TextFields in your dialog when either of them trigger an action.

    private class HandleTextField implements ActionListener {
      @Override
      public void actionPerformed (ActionEvent e) {
        StringBuilder string  = new StringBuilder();
    
        if (e.getSource () == textFieldArray[0] || 
            e.getSource () == textFieldArray[1]){
          string.append(String.format(
              "1. %s", textFieldArray[0].getText())
            );
          string.append(String.format(
              "2. %s", textFieldArray[1].getText())
            );
        }
    
        String[] fields = {"Summary of my changes" , string.toString()};
    
        JOptionPane.showMessageDialog(null, fields, "My suggestion to the course", JOptionPane.WARNING_MESSAGE);
      }
    }