Results 1 to 4 of 4
- 12-30-2009, 11:21 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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:
Now I try to populate (and display) jTable where col1->d1[], col2->d2Java 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();
My code for this part is:
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?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]}); }Last edited by althair; 12-30-2009 at 11:26 AM.
-
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.
- 12-30-2009, 01:54 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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:
Into following one: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]}); }
And it works perfectly :).Java Code:DefaultTableModel model = (DefaultTableModel)tblInput.getModel(); for (int d=0;d<d1.length;d++) model.addRow(new Object[]{d1[d], d2[d]});
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.
- 12-30-2009, 02:05 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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
-
Populating a JTable
By toymachiner62 in forum New To JavaReplies: 2Last Post: 10-13-2009, 05:56 AM -
Problem in resizing JTable in netbeans
By shahid0627 in forum NetBeansReplies: 1Last Post: 09-07-2009, 06:33 PM -
Netbeans JTable
By sysout in forum NetBeansReplies: 6Last Post: 08-26-2009, 02:27 AM -
How to add new row from JTable comp in netbeans GUI?
By zam in forum NetBeansReplies: 3Last Post: 03-09-2009, 07:37 AM -
Netbeans Jtable
By Manfizy in forum NetBeansReplies: 16Last Post: 11-13-2008, 09:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks