Results 1 to 4 of 4
  1. #1
    althair is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default Populating JTable with 2 arrays (Netbeans)

    Hi,

    I have the following issue:

    In Netbeans IDE (6.8) I created JTable - name: tblInput; 2 columns, 0 rows, not resizable, not editable, data type - Float.
    In Inspector panel I see that JTable is included in JScrollPane.

    I also created 2 ArrayLists and converted to arrays:

    Java Code:
    ArrayList<Float> data1 = new ArrayList<Float>();
    ArrayList<Float> data2 = new ArrayList<Float>();
    
    {here code for populating ArratLists}
    
    Object d1[]=data1.toArray();
    Object d2[]=data2.toArray();
    Now I try to populate (and display) jTable where col1->d1[], col2->d2

    My code for this part is:
    Java Code:
       DefaultTableModel model = new DefaultTableModel();
       tblInput=new JTable(model);
       for (int d=0;d<d1.length;d++){
           model.setRowCount(model.getRowCount()+1);
           model.addRow(new Object[]{d1[d],d2[d]});
           }
    When I run the application I do not obtain any errors or Exceptions, but the JTable is still empty. Should I somehow "repaint" JTable component? I am quite new to Java so that I assume it can be the issue of the code above. Or maybe I should do sth with JScrollPane as well?
    Last edited by althair; 12-30-2009 at 12:26 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's hard to tell what is wrong based on the information at hand. My guess is that while it's likely that the jtable object that you're creating with the new default table model and the jtable that is being displayed used the same JTable variable, the JTable objects themselves are different.

    My recommendation to you is to create the simplest program possible that compiles and that reproduces your error. I also recommend that you try to do this without NetBeans generated code so that the code is readable by others should you post it here.

  3. #3
    althair is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Sorry - I really wanted to make a "core" app without IDE builder, but in the meantime I found a solution to my problem and corrected it in NetBeans directly:

    It was enough to change code:
    Java Code:
    DefaultTableModel model = new DefaultTableModel();
    tblInput=new JTable(model);
    for (int d=0;d<d1.length;d++){
      model.setRowCount(model.getRowCount()+1);
      model.addRow(new Object[]{d1[d],d2[d]});
      }
    Into following one:
    Java Code:
    DefaultTableModel model = (DefaultTableModel)tblInput.getModel();
    for (int d=0;d<d1.length;d++)
       model.addRow(new Object[]{d1[d], d2[d]});
    And it works perfectly :).

    I will add entire method (still from Netbeans though) with some description in next post to this thread - maybe somebody will utilize it in future.

  4. #4
    althair is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    As promised:
    The following method is the actionPerformed for clicking a button, which:
    -opens a txt file (which has some format, and contains 3 "columns" of "tokens" in some lines)
    - changes the label lblPath - into name of the file
    - populates JTable (x,2)

    Java Code:
    private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {                                        
    
                chooser = new JFileChooser();
                int returnVal = chooser.showOpenDialog(this);
                File file1 = chooser.getSelectedFile();
                String nextLine = "";
                String FileName = file1.getName();
                
                //lineCount will be used only for cutting some crappy text at the beginning of the text file
                Integer lineCount=0; 
    
                ArrayList<Float> data1 = new ArrayList<Float>();
                ArrayList<Float> data2 = new ArrayList<Float>();
    
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    lblPath.setText(FileName);
    
                   try {
                    FileReader fr = new FileReader(file1);
                    BufferedReader br = new BufferedReader(fr);
                    while((nextLine = br.readLine()) != null) {
                        lineCount++;
    
              //the "if" statement was needed here 'cause in my file I have some unnecessary lines at the beginning and at the end of the file - in normalized file this statement would be skipped
    
                        if ((lineCount>8)&&(!nextLine.trim().startsWith("=")))
                            {
                                    String[] theline = nextLine.trim().split(" +");
                                    data1.add(Float.parseFloat(theline[0]));
                                    data2.add(Float.parseFloat(theline[1]));
         } }
    }
       catch(FileNotFoundException fN) {
        fN.printStackTrace();
       }
       catch(IOException e) {
        System.out.println(e);
    }
    
      //I did not find any possibility to use ArrayList in model.addRow method so I simply converted it to Object[] array
    
       Object d1[]=data1.toArray();
       Object d2[]=data2.toArray();
    
       DefaultTableModel model = (DefaultTableModel)tblInput.getModel();
       for (int d=0;d<d1.length;d++)
        model.addRow(new Object[]{d1[d], d2[d]});
                }      
    }

Similar Threads

  1. Populating a JTable
    By toymachiner62 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 06:56 AM
  2. Problem in resizing JTable in netbeans
    By shahid0627 in forum NetBeans
    Replies: 1
    Last Post: 09-07-2009, 07:33 PM
  3. Netbeans JTable
    By sysout in forum NetBeans
    Replies: 6
    Last Post: 08-26-2009, 03:27 AM
  4. Replies: 3
    Last Post: 03-09-2009, 08:37 AM
  5. Netbeans Jtable
    By Manfizy in forum NetBeans
    Replies: 16
    Last Post: 11-13-2008, 10:15 AM

Tags for this Thread

Posting Permissions

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