Home > Software engineering >  Is there any way to set all result values in the textfield? It sets only last value as expected but
Is there any way to set all result values in the textfield? It sets only last value as expected but

Time:05-26

I am creating a program which converts a string input to binary/octal string and shows the result in a textfield, but only last value (binary/octal string of 1 individual character ) gets shown as expected. But is there any other way to show all values?

    resultTextField = new JTextField();
    resultTextField.setBounds(10,170,200,100);
    resultTextField.setForeground(Color.green);
    resultTextField.setBackground(Color.black);
    resultTextField.setOpaque(true);
    resultTextField.setVisible(visibilty);


@Override
public void actionPerformed(ActionEvent e) {    
    if (e.getSource()==binaryButton) {
    convertToBinary();//to convert the string to binary string
    }
    
    if(e.getSource()== octaButton) {
    convertToOcta();//to convert to octal string
    }       
}   

public void convertToBinary() {
visibilty = true;

converting input text (maintextField (String)) to char array

char[] textArray = mainTextField.getText().toCharArray(); 
for(int ascii : textArray)//this will convert individual characters to their ascii value
{
    String binaryString = Integer.toBinaryString(ascii);//converts ascii value to binary string
    resultTextField.setText(binaryString); //to get result
    resultTextField.setVisible(visibilty);
    //System.out.println(binaryString);
    }
}

same thing but as in converToBinary but input gets converted octal string

public void convertToOcta() {
visibilty = true;
String text = mainTextField.getText();
char[] textArray = text.toCharArray();
for(int ascii : textArray)
{
String octalString = Integer.toOctalString(ascii);
    resultTextField.setText(octalString);
    //System.out.println(octalString); 
    }
resultTextField.setVisible(visibilty);

    }
  }

CodePudding user response:

You just need to include the contents that were already there

String binaryString = Integer.toBinaryString(ascii);//converts ascii value to binary string
resultTextField.setText(resultTextField.getText()   binaryString); //to get result

CodePudding user response:

Whenever you add/remove text from a text component a DocumentEvent is generated. Of course, for this simple task, you probably don't care about the DocumentEvents, but it is something to consider for the future when working with text components.

You could use simple code like:

resultTextField.setText( resultTextField.getText()   octalString );

This approach will result in multiple removeUpdate() and insertUpdate() events, when in reality you probably only want to generate a single insertUpdate() event. Also this is relatively inefficient since every time you set the text you need to clear all the text in the Document and then rebuild the Document with the new text.

Another option is to update the Document of the text field:

Document doc = resultTextField().getDocument().
doc.insertString(...).

This will result in multiple insertUpdate() events. Also, note that you will need to use try/catch logic to handle an Exception on the insertString(...) method.

A simpler approach might be to use a single line JTextArea, then you can just use the append(...) method to append new text. It updates the Document as shown above, but it already handles the Exception logic so you don't need to worry about it.

//resultTextField.setText(octalString);
resultTextArea.append(octalString);

This will result in multiple insertUpdate() events being generated.

Or if you really want to use a JTextField then you can first use a StringBuffer and append each String as it is converted. Then outside your loop you can set the text once. Something like:

StringBuffer buffer = new StringBuffer();

for(int ascii : textArray)
{
    String octalString = Integer.toOctalString(ascii);
    //resultTextField.setText(octalString);
    buffer.append(octalString);
}

resultTextField.setText(buffer.toString());

Only a single insertUpdate() event will be generated.

  • Related