Home > Software design >  How to modify a txt file using jframe in java
How to modify a txt file using jframe in java

Time:12-13

I have data saved in txt file. I have loaded them in a jtable in one of my forms and I want to be able to edit the txt file using buttons in the GUI form. Also if you can tell me why I have empty lines appearing in the jtable that will be very helpful.

    private void delbtnActionPerformed(java.awt.event.ActionEvent evt) {                                       
    
}                                      

private void modifybtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
}                                         

private void newpassActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
}                                       

private void newusernameActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
} 

here is the view button code

    private void viewbtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    
    try{
        BufferedReader br = new BufferedReader(new FileReader("logindata.txt"));
        String firstLine = br.readLine().trim();
        String[] columnsName = firstLine.split("/");
        DefaultTableModel model = (DefaultTableModel)datatable.getModel();
        model.setColumnIdentifiers(columnsName);
       
        Object[] tableLines = br.lines().toArray();
        
        for(int i = 0; i < tableLines.length; i  )
        {
            String line = tableLines[i].toString().trim();
            String[] dataRow = line.split(",");
            model.addRow(dataRow);
        }
    }
    catch(Exception ep){
        Logger.getLogger(people_info.class.getName()).log(Level.SEVERE, null, ep);
    }
} 

here is how I saved data in txt file

    private void submitbtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
    String user = usertxt.getText();
    String pass = passwordtxt.getText();
    
    try
    {
        FileOutputStream ldata = new FileOutputStream("logindata.txt", true);
        PrintWriter printer = new PrintWriter(ldata);
        
        printer.println(user "," pass "," "," ",");
        JOptionPane.showMessageDialog(null,"success, please login");
        printer.close();
        
        this.setVisible(false);
        login lg = new login();
        lg.setVisible(true);
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null,"Error, please retry");
    }        
} 

here is how the GUI looks like

CodePudding user response:

after model.setColumnIdentifiers(columnsName); add model.setRowCount(0);

  • Related