Monday, 15 July 2013

java - Store contents from JTable into a text file -


I have written a swing form with 5 text box and a button. When I click the button save, the data entered is being displayed in the table. Now I want to store the data shown in the table in a text file. Anyone can help me with this code I am still a Java startup and have very little idea about Java coding. After a lot of surfing, I was able to run the table. I am attaching the code done so far. If I have made some silly mistakes, please excuse me. In fact your help is needed in advance ..

  import javax.swing *; Import java.awt. *; Import java.awt.event. *; Class ReadTextFile JFrame {JButton save; Zeppel panel; JLabel label 1, label 2, label 3, label 4, label 5; The last JTextField text1, lesson 2, lesson 3, lesson 4, lesson 5; ReadTextFile () {label 1 = new jlab (); Label1.setText ("English word:"); Text1 = new JTextField (20); Label 2 = new jlab (); Label2.setText ("English Nickname:"); Text2 = new JTextField (20); Label 3 = new jlab (); Label3.setText ("Kannada word:"); Text3 = new JTextField (20); Label 4 = new jlab (); Label4.setText ("Kannada alias:"); Text4 = new JTextField (20); Label 5 = new jlab (); Label5.setText ("Part of speech:"); Text5 = new JTextField (20); Save = New Pocket ("Save"); Panel = new zpinal (new grid layout (6,2)); Panel.add (label1); Panel.add (text1); Panel.add (LABEL2); Panel.add (text2); Panel.add (Label3); Panel.add (text3); Panel.add (label4); Panel.add (text4); Panel.add (label5); Panel.add (text5); Panel.add (save); Add (panel, BorderLayout.CENTER); SetTitle ("Word Base"); save.addActionListener (new ActionListener () {public void actionPerformed (ActionEvent ae) {string 1 = text1.getText (); String Value 2 = text2.getText (); string value3 = text3.getText (); string VALUE4 = text4 .getText (); string value5 = text5.getText (); object rowData [] [] = {{value 1, value 2, value3, VALUE4, value5}}; object COLUMNNAMES [] = { "English Word", "English synonym "" Kannada Word "," Kannada equivalents "," position "}; JFrame f = new JFrame (); JTable table = new JTable (rowData, COLUMNNAMES); jScrollPane Scrollpane = new JScrollPane (table); JPanel p = new JPanel (); p.add (Scrollpane); f.add (P); f.setVisible (right); //f.setDefaultLookAndFeelDecorated(tru E); f.setSize (400,200);}}); } Public static void main (String AGR []) {tryTextFile frame = new Reedtekstfail (); Frame.setSize (300,200); Frame.setVisible (true); } Hold (exception e) {}}}    

In your button action listener, you You can obtain the data table from the table and type in the file like this.

  stringbuffer sbTableData = new StringBuffer (); For {{int column = 0; column & lt; table.getColumnCount (); column ++} {sbTableData.append} for the line & quot; row.getRowCount (); (Table.getValueAt (row, column)). Attachment ("\ t"); } SbTableData.append ("\ n"); } // In a file, type `sbTableData.toString ()`    

No comments:

Post a Comment