Results 1 to 2 of 2

Thread: searching in -files- Jtable

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    1

    searching in -files- Jtable

    hey
    i have this code for writing and reading from files in table( in java)\
    plz help me to write a code that can search this file by giving a word that may be a maybe not exist in this file
    plz make that code at different class
    Code:
    package jojo;
    import java.io.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    import java.util.Vector;
    
    public class NewJFrame extends javax.swing.JFrame {
         
         /** Creates new form NewJFrame */
         public NewJFrame() {
             initComponents();
         }
         
            // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
         private void initComponents() {
             jScrollPane1 = new javax.swing.JScrollPane();
             Sup_list = new javax.swing.JTable();
    
             setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
             jScrollPane1.addInputMethodListener(new java.awt.event.InputMethodListener() {
                 public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                 }
                 public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                     jScrollPane1InputMethodTextChanged(evt);
                 }
             });
    
             Sup_list.setModel(new javax.swing.table.DefaultTableModel(
                 new Object [][] {
                     {null, null, null, null},
                     {null, null, null, null},
                     {null, null, null, null},
                     {null, null, null, null}
                 },
                 new String [] {
                     "Title 1", "Title 2", "Title 3", "Title 4"
                 }
             ));
             jScrollPane1.setViewportView(Sup_list);
             
             btnSaveData = new JButton("Save table data");
             btnSaveData.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent ae) {
                     try{
                         writer = new PrintWriter("file.txt", "UTF-8");
                         for (int i = 0; i < Sup_list.getRowCount(); i++) {
                             for (int j = 0; j < Sup_list.getColumnCount(); j++) {
                                 Object value = Sup_list.getValueAt(i, j);
                                 if (value != null)
                                     writer.println(value.toString());
                             }
                         }
                         JOptionPane.showMessageDialog(NewJFrame.this, "Data saved");
                     } catch (IOException ioe) {
                         ioe.printStackTrace();
                     } finally {
                         if (writer != null)
                             writer.close();
                     }
                 }
             });
             btnLoadData = new JButton("Load data");
             btnLoadData.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent ae) {
                     try{
                         reader = new BufferedReader(new InputStreamReader(new FileInputStream("file.txt"), "UTF-8"));
                         String value;
                         int x = 0;
                         while ((value = reader.readLine()) != null) {
                             int row = x/Sup_list.getColumnCount();
                             int col = x - (row*Sup_list.getColumnCount());
                             Sup_list.setValueAt(value, row, col);
                             x++;
                         }
                         JOptionPane.showMessageDialog(NewJFrame.this, "Data loaded");
                     } catch (IOException ioe) {
                         ioe.printStackTrace();
                     } finally {
                         if (reader != null)
                             try {
                                 reader.close();
                             } catch (IOException ioe) {
                                 ioe.printStackTrace();
                             }
                     }
                 }
             });
             JPanel pnButtons = new JPanel();
             pnButtons.add(btnSaveData);
             pnButtons.add(btnLoadData);
             getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
             getContentPane().add(pnButtons, java.awt.BorderLayout.SOUTH);
    
             pack();
         }// </editor-fold>
    
         private void jScrollPane1InputMethodTextChanged(java.awt.event.InputMethodEvent evt) {                                                    
    //TODO add your handling code here:
         }                                                  
         
             public static void main(String args[]) {
             java.awt.EventQueue.invokeLater(new Runnable() {
                 public void run() {
                     new NewJFrame().setVisible(true);
                 }
             });
         }
         
         // Variables declaration
         private javax.swing.JTable Sup_list;
         private javax.swing.JScrollPane jScrollPane1;
         private JButton btnSaveData, btnLoadData;
         private PrintWriter writer;
         private BufferedReader reader;
         // End of variables declaration
         
    }

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: searching in -files- Jtable

    You didn't even write this code, it was generated by Netbeans...
    I can understand it when you try and need help, but doing it all for you is a little ridiculous.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width