I need help updating my jtextarea.
I am reading rows from an excel file and after it is done writing to the jtextarea, the excel file is rewritten with the old rows plus new rows and then again, the jtextarea is recreated with the rows from the excel file.
What i would like to do is keep the content in the jtextarea and only add to the bottom with the newest rows in the excel file. The excel file is always rewritten, but they are sorted by time so the newest rows are always at the bottom.
Below is the code that i have that creates the gui, along with the text area.
Expand|Select|Wrap|Line Numbers
- public JFrame baseFrame;
- private JButton highButton = new JButton("HIGH");
- private JButton medButton = new JButton("MEDIUM");
- private JButton lowButton = new JButton("LOW");
- private ButtonGroup group;
- public JTextArea area = new JTextArea(17, 47);
- DesktopSHMTranslator translator;
- public DesktopSHMGUI() throws Exception
- {
- baseFrame = new JFrame("SHM Alarms");
- baseFrame.setSize(700,500);
- baseFrame.getContentPane().setLayout(new BorderLayout());
- // area.setLineWrap(true);
- area.setWrapStyleWord(true);
- area.setBorder(BorderFactory.createEmptyBorder(45, 45, 45, 45));
- JScrollPane scrollPane = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
- JPanel textPanel = new JPanel();
- textPanel.setLayout(new FlowLayout());
- textPanel.add(scrollPane, BorderLayout.CENTER);
- // panel.add(highButton);
- // panel.add(medButton);
- // panel.add(lowButton);
- JPanel buttonPanel = new JPanel();
- buttonPanel.add(highButton, BorderLayout.WEST);
- buttonPanel.add(medButton, BorderLayout.CENTER);
- buttonPanel.add(lowButton, BorderLayout.EAST);
- highButton.addActionListener(this);
- highButton.setActionCommand("high");
- medButton.addActionListener(this);
- medButton.setActionCommand("med");
- lowButton.addActionListener(this);
- lowButton.setActionCommand("low");
- baseFrame.getContentPane().add(textPanel, BorderLayout.CENTER);
- baseFrame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
- baseFrame.addWindowListener(new CloseWindow());
- translator = DesktopSHMTranslatorHome.createDesktopSHMTranslator(this);
- baseFrame.setVisible(true);
- }
Expand|Select|Wrap|Line Numbers
- private void contentReading(FileInputStream fileInputStream) {
- // TODO Auto-generated method stub
- WorkbookSettings ws = null;
- Workbook workbook = null;
- Sheet s = null;
- Cell rowData[] = null;
- int rowCount = '0';
- int columnCount = '0';
- DateCell dc = null;
- int totalSheet = 0;
- try {
- ws = new WorkbookSettings();
- ws.setLocale(new Locale("en", "EN"));
- workbook = Workbook.getWorkbook(fileInputStream, ws);
- totalSheet = workbook.getNumberOfSheets();
- //Getting Default Sheet i.e. 0
- s = workbook.getSheet(0);
- //Reading Individual Cell
- // getHeadingFromXlsFile(s);
- rowCount = s.getRows();
- columnCount = s.getColumns();
- for (int i = 0; i < rowCount; i++) {
- // for (int i = DesktopSHMTranslator.startOfRow; i < rowCount; i++) {
- //Get Individual Row
- rowData = s.getRow(i);
- if (rowData[0].getContents().length() != 0) { // the first data column must not null
- for (int j = 0; j < columnCount; j++) {
- if(j==0){
- area.setFont(new Font("sansserif", Font.BOLD, 12));
- area.scrollRectToVisible(new Rectangle(0,area.getHeight()-2,1,1));
- area.append(rowData[j].getContents() + " --- ");
- }
- else if(j==1)
- area.append(rowData[j].getContents() + " --- ");
- else if(j==2)
- area.append(rowData[j].getContents() + " --- ");
- else if(j==3)
- area.append(rowData[j].getContents() + " --- ");
- else if(j==4)
- area.append(rowData[j].getContents() + " --- ");
- else if(j==5)
- area.append(rowData[j].getContents() + " --- ");
- else if(j==6)
- area.append(rowData[j].getContents() + " --- ");
- else if(j==7)
- area.append(rowData[j].getContents() + "\n");
- }
- }
- }
- workbook.close();
- } catch (IOException e) {
- System.out.println("YOU ARE IN IOEXCETPION in CONTENTREADING");
- e.printStackTrace();
- } catch (BiffException e) {
- System.out.println("YOU ARE IN BIFFEXCEPTION in CONTENTREADING");
- e.printStackTrace();
- }
- }
Basically right now, every time the excel file is read and the jtextarea is written to, it rewrites the whole excel file which takes some time and it shows each line by line being written to. I want it so that the content in the jtextarea is still there and the only lines that are added are lines that are new to the excel file that are not already in the jtextarea.
thanks for the help guys!!