1

I am making a GUI app in which on click of button we read a binary file and display the data in a TextArea (Data is 327680 bytes).

Now if I use textArea.setLine(true) then it slows down the execution and it takes about 1 minute to execute and dislpays value.

But if I dont use textArea.setLine(true) then code executes in less than 1 sec.

UPDATED CODE.

public class App{

private static final String INPUT_FILE = "E:\\arun\\files\\Capture_Mod1.BIN";
private static final String OUTPUT_FILE = "E:\\arun\\files\\ISO_Write.BIN";
private static final String IMAGE_FILE = "E:\\arun\\files\\Image.jpg";

private JFrame frame;
private JTextArea textArea;
private JButton btnNewButton;
private ISOImageDataWorker worker;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                App window = new App();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


}

/**
 * Create the application.
 */
public App() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
    gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
    frame.getContentPane().setLayout(gridBagLayout);

    textArea = new JTextArea(5, 20); // **HERE IS THE PROBLEM**
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord( true );
    JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    GridBagConstraints gbc_textArea = new GridBagConstraints();
    gbc_textArea.insets = new Insets(0, 0, 5, 0);
    gbc_textArea.gridx = 0;
    gbc_textArea.gridy = 1;
    frame.getContentPane().add(scrollPane, gbc_textArea);

    btnNewButton = new JButton("ISO 19794 Data");
    GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
    gbc_btnNewButton.gridx = 0;
    gbc_btnNewButton.gridy = 2;
    frame.getContentPane().add(btnNewButton, gbc_btnNewButton);

    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            worker = new ISOImageDataWorker();
            worker.execute();
        }
    });

}

private class ISOImageDataWorker extends SwingWorker<String , Object>{

    @Override
    protected String doInBackground() throws Exception {
        String str = processData();
        return str;
    }

    @Override
    protected void done(){
        try {
            System.out.println("cm1");
            textArea.setText(get());
            System.out.println("cm2");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

public String processData(){
    DataReadWrite data = new DataReadWrite();

    //Read data
    byte[] imageData = data.readData(INPUT_FILE);
    System.out.println("Image data length is : "+ imageData.length);

    // Generate Finger Image Data General Header
    FingerImageDataGeneralHeader generateHeader = new FingerImageDataGeneralHeader();
    byte[] resultData = generateHeader.createGeneralHeader(imageData);

    // Write data to binary file
    data.writeData(resultData, OUTPUT_FILE);

    // Create Image from binary data
    CreateTiff createTiff = new CreateTiff();
    createTiff.create(resultData, IMAGE_FILE);

    String str = bytesToHex(resultData);

    return str;
}

public static String bytesToHex(byte[] bytes) {
    StringBuilder sb = new StringBuilder();
    for (byte b : bytes) {
        sb.append(String.format("%02X ", b));
    }
    return sb.toString();
}

}

I don't know what I am doin wrong. Please Help.

code_fish
  • 3,381
  • 5
  • 47
  • 90
  • Binary data usually doesn't have any spaces so there is nothing to wrap on. Don't know if it will make a difference but instead of updating the textarea with the entire file at once try using the append() method and update the text area in chunks of characters, maybe 128(?) at a time. – camickr Jun 05 '13 at 15:52

1 Answers1

3
btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
     >> processData(); <<
        textArea.setText(str);
    }
});

Don't execute time-consuming code on the GUI-thread (EDT). Use SwingWorker.

Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • Thanks for the reply. I have done what you have suggested and it is worth doing for future. Now I am using SwingWorker. But still when I run the code with textArea.setLineWrap(true) it takes about 2 minute to execute and If I set it to false it takes only 1 sec. Why so? – code_fish Jun 05 '13 at 14:11