Home > Software engineering >  JTextField is not outputting all of the desired outputs
JTextField is not outputting all of the desired outputs

Time:12-27

I am new in Java and currently studying and trying out GUI. I have this problem that converts multiple integer into separated with comma. example is: 12333 = 1,2,3,3,3. It works in System.out.print but it doesn't work in setText in JTextField. I tried many various types on how to convert but it doesn't seem to work when it comes to JTextField and setText

JButton btnNewButton = new JButton("CONVERT");
btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        int ans, input = 0;
        try {
            input = Integer.parseInt(Input.getText());
        }catch(Exception e1) {
            JOptionPane.showMessageDialog(null, "Please enter a valid Number or Integer");
        }
        String toString = String.valueOf(input);
        char[] toArray = toString.toCharArray();
        for (int i = 0; i < toArray.length; i  ) {
            if (i == 0) {
                String s = (Character.toString(toArray[i]));
                /*String output = Character.toString(toArray[i]);*/
                Converted.setText(Character.toString(toArray[i]));
            } else {
                String s = (", "   toArray[i]);
                /*String output = Character.toString(toArray[i]);*/
                Converted.setText(", "   toArray[i]);
            }
        }
    }
});
btnNewButton.setBounds(174, 119, 89, 23);
contentPane.add(btnNewButton);
Converted = new JTextField();
Converted.setColumns(10);
Converted.setBounds(34, 153, 143, 20);
contentPane.add(Converted);

This outputs only the last 2 digits. ex is 12345 = ,5

CodePudding user response:

Try appending and not setting. You are setting the value in the else statement. That's why you are getting the last value only.

  • Related