jTable in JBuilder

volavy
volavy used Ask the Experts™
on
Hi!

I am somewhat a beginner in java programming and I have a question concerning managing jTables in JBuilder (v6).

Question:
A have lets say three string variables (s1, s2, s3) and I "simply" want to add this strings in a iterative way into different rows i a jTable on a button click. Like this:

jTable1:

Kolumn-name1  Kolumn-name2  Kolumn-name1
s11           s21           s31
s12           s22           s32
  ...and so on ...


Could someone help me with this?

Regards
/Peter
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
This is the principle involved. You, of course, would substitute TableModel's setValueAt():

public class SetTable {

  final static String[]  strings = { "a","b","c","d","e","f" };
  static int ROW_COUNT = 2;
  static int COL_COUNT = 3;

  public static void main(String[] args){
    int stringIndex = 0;
    for(int col = 0;col < COL_COUNT;col++){
      for(int row = 0;row < ROW_COUNT;row++){
        setValueAt(row,col,strings[stringIndex]);
        stringIndex++;
      }
    }
  }

  static void setValueAt(int row,int col,Object o){
    System.out.println("value at " + row + "," + col + " = " + (String)o);
  }

}
Hi,
first add JScrollPane to your JFrame/JDialog so u can see the headers.
second use jTable.setModel() method to add your table model (do it in the jbInit() method after initializing the table). use your own TableModel class (this is the best way) - ie implements TableModel interface or extend AbstractTableModel (both in javax.swing.table package).
add addRow(<arguments>) to your model and use it in order to add rows to the table.
this is a small code example:
the table model class:

import javax.swing.tabl.*;

public class MyTableModel extends AbstractTableModel {

  protected Vector data;

  protected int colCount = 3;

  public MyTableModel() {
    data = new Vector();
  }

  public int getRowCount() {
    return data.size();
  }

  public int getColumnCount() {
    return colCount;
  }

  public Object getValueAt(int r, int c) {
    return ((Object[])data.elementAt(r))[c];
  }

  public void addRow(Object[] newRow) {
    data.addElement(newRow);
    this.fireTableDataChanged();
  }

} // MyTableModel

in your jbinit() method:

private void jbInit() throws Exception {
...
  jTable1.setModel(new MyTableModel()); // after initializing jTable1
...
}

when adding new row:
...
Object[] newRow = new Object[3]; // an example data row
newRow[0] = "Value 1";
newRow[1] = "Value 2";
newRow[2] = "Value 3";
((MyTableModel)jTable1.getModel()).addRow(newRow);
...


-gkern
Top Expert 2016

Commented:
Incidentally, you've double-posted the question. I wonder if you could ask Community Support to delete it? http://www.experts-exchange.com/Community_Support/

I'll put a note in the duplicate to answer here.
for my first recommendation:
add your JTable instance to the JScrollPane.

-gkern

Author

Commented:
Thanks a lot
/Peter

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial