Java jTable compute each row - java

can anybody here help me on how to compute rows in jtable?
assuming that i have a table containing fields date, description, aCCount, SCount and lost , i have loaded the four field from database except lost field because i want to compute it on runtime, can anybody help me? here is image attach for clarification
public void cha(){
ArrayList<Chalsim> list = getChalsim();
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
Object[] row = new Object[4];
for(int i =0; i<list.size(); i++){
row[0] = list.get(i).getDate();
row[1] = list.get(i).getDesc();
row[2] = list.get(i).getAc();
row[3] = list.get(i).getSc();
model.addRow(row);
}
}

You can extend AbstractTableModel,and then assign it to JTable
new AbstractTableModel() {
public String getColumnName(int col) {
return columnNames[col].toString();
}
public int getRowCount() { return rowData.length; }
public int getColumnCount() { return columnNames.length; }
public Object getValueAt(int row, int col) {
return rowData[row][col];
}
public boolean isCellEditable(int row, int col)
{ return true; }
public void setValueAt(Object value, int row, int col) {
rowData[row][col] = value;
fireTableCellUpdated(row, col);
}
}
and then put it into JTable
JTable table = new JTable(new MyTableModel());

Related

JTable with varying column length

I'm trying to read a csv file and get it setup to convert into another format to save some time at work, but the JTable i'm loading it into throws an exception when a row has a length less than the expected column. Is there a method to create an empty cell if row length < column length?
here is the table model:
private class CSVTableModel extends AbstractTableModel{
private ArrayList<String[]> list;
private String[] columns;
public CSVTableModel() {
this.list = p.getData();
this.columns = p.getHeaders();
}
#Override
public String getColumnName(int col) {
return columns[col];
}
#Override
public int getRowCount() {
return list.size();
}
#Override
public int getColumnCount() {
return columns.length;
}
#Override
public Object getValueAt(int row, int col) {
return list.get(row)[col];
}
#Override
public void setValueAt(Object value, int row, int col) {
list.get(row)[col] = (String) value;
this.fireTableCellUpdated(row, col);
}
}
So you can see with the getValueAt(int row, int col) method it will cause an error if the col exceeds the String[].length.
I literally solved it after posting. Rubber duck debugging.
#Override
public Object getValueAt(int row, int col) {
if ( col < list.get(row).length ) {
return list.get(row)[col];
} else {
return "";
}
}
added a condition and works fine now. Should've seen it before.

Make a JCheckBox in a JTable editable

I need some help with my JTable. I am writing a program, wich extracts data from a database into a JTable. The first column should be a editable JCheckBox so I am able to work with the checked (true or false) rows and the data.
I am using a AbstractTableModel(with class extends AbstractTableModel) and override these five methods:
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 0;
}
#Override
public Class<?> getColumnClass(int col) {
if (col == 0) {
return Boolean.class;
}
return super.getColumnClass(col);
}
#Override
public int getColumnCount() {
return header.length;
}
#Override
public int getRowCount() {
return data.length;
}
#Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
To display the JTable I use:
JTable table = new JTable();
JScrollPane scrollpane = new JScrollPane();
.
.
.
table = new JTable(data, header);
table.setModel(this);
scrollpane = new JScrollPane(table);
I read the data with a for loop into the data array. The header array I defined.
Basically I need the checked rows to send a mail with the right data in it.
EDIT:
package test;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class TestCode extends AbstractTableModel {
private static final long serialVersionUID = -7051817393770003705L;
String[] header = {"", "header", "header", "header"};
Object[][] data = {{new Boolean(false), "Text", "Text", "Text"}, {new Boolean(false), "Text", "Text", "Text"}, {new Boolean(false), "Text", "Text", "Text"}};
public TestCode() {
JFrame frame = new JFrame();
JTable table = new JTable(data, header);
table.setModel(this);
JScrollPane scrollpane = new JScrollPane(table);
frame.add(scrollpane);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
#Override
public boolean isCellEditable(int row, int col) {
return col == 0;
}
#Override
public Class<?> getColumnClass(int col) {
if (col == 0) {
return Boolean.class;
}
return super.getColumnClass(col);
}
#Override
public int getColumnCount() {
return header.length;
}
#Override
public int getRowCount() {
return data.length;
}
#Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
public static void main(String[] args) {
TestCode code = new TestCode();
}
}
This is a short snippet of my code to execute to make it easier for you. I want be able to check the JCheckBoxes at the firt column so I am able to read a true or false boolen from that column.
Thank you for help!
You have to override AbstractTableModel#setValueAt
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
// super.setValueAt(aValue, rowIndex, columnIndex); by default empty implementation is not necesary if direct parent is AbstractTableModel
data[rowIndex][columnIndex] = aValue;
fireTableCellUpdated(rowIndex, columnIndex);// notify listeners
}
Result.
BTW : Don't use new Boolean(false) instead use Boolean.FALSE
Okay leaving aside the structure of the code (I agree that the creation of the table should not be done in the model and things should be separated out better) the reason this is not working is that your table model does not implement setValueAt(Object value, int row, int column).
So when you click on a cell, that method is called, but your data array is not updated so the value for the cell is always false.
Adding
#Override
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
}
to your model means the table behaves as you would expect
Simply add implementation of value setter
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
data[rowIndex][columnIndex] = aValue;
}

How do I make a JTable editable in java

I'm using a JTable in java, but it won't let me edit the cells.
private final TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return 5;
}
public int getRowCount() {
return 10;
}
public Object getValueAt(int row, int col) {
return new Integer(row*col);
}
};
private final JTable table = new JTable(dataModel);
add the follwoing code
public boolean isCellEditable(int row, int col)
{ return true; }
public void setValueAt(Object value, int row, int col) {
rowData[row][col] = value;
fireTableCellUpdated(row, col);
}
you should have a array where you will save the changes
Add isCellEditable() function inside the anonymous inner class AbstractTableModel
public boolean isCellEditable(int row, int col) {
return true;
}
Try
private final TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return 5;
}
public int getRowCount() {
return 10;
}
public Object getValueAt(int row, int col) {
return new Integer(row*col);
}
public boolean isCellEditable(int row, int col) {
return true;
}
};
Add isCellEditable() to the rows and columns you want them to be editable, example if you don't want some columns like ID to be editable return false. Keep in mind that you need to save the editit data some where
public boolean isCellEditable(int row, int col) {
return true; // or false for none editable columns
}
public void setValueAt(Object value, int row, int col) {
rowData[row][col] = value; // save edits some where
fireTableCellUpdated(row, col); // informe any object about changes
}

Java JTable create row of checkboxes to create subtable

I have a JTable that uses an AbstractTableModel. I'm trying to make the first row of the table a row of JCheckboxes.
EDIT: The goal is to take the columns with checked checkboxes and create a new table. This is my first time trying something like this, so I'm open to suggestions.
Here is the code I'm trying in NetBeans 7.1.1 :
private void selectSourceCBActionPerformed(java.awt.event.ActionEvent evt) {
int sourceNum = selectSourceCB.getSelectedIndex();
DataSource currentDS = datSourceArrList.get(sourceNum);
final ArrayList<Object[]> workArrLst1 = currentDS.getSampSet();
sourceDetailTable.setAutoResizeMode(sourceDetailTable.AUTO_RESIZE_OFF);
sourceDetailTable.setColumnSelectionAllowed(true);
JTableHeader header = sourceDetailTable.getTableHeader();
AbstractTableModel mytable1 = new AbstractTableModel() {
Object colNames[] = workArrLst1.get(0);
#Override
public int getRowCount() {
return workArrLst1.size();
}
#Override
public int getColumnCount() {
return workArrLst1.get(1).length;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
return workArrLst1.get(rowIndex+1)[columnIndex];
}
#Override
public void setValueAt(Object value, int row, int col) {
if(row == 1){
workArrLst1.get(row)[col] = Boolean(false);
fireTableCellUpdated(row, col);
}
workArrLst1.get(row)[col] = (String) value;
fireTableCellUpdated(row, col);
}
#Override
public String getColumnName(int column) {
return (String) colNames[column];
}
};
}
Is there anything obvious I'm missing here?

Making JTable cells uneditable

I am trying to make all the cells of a JTable uneditable when double clicked by the user. I have read a lot of forum posts and the general consensus is to create a new table model class, extend DefaultTableModel and then override method isCellEditable(int row, int column). I did all of this and now when I run my program (applet) Nothing shows up in the cells. NOTE I have a prof this semester that does not think applets are outdated...
Code for the table model:
public class MyTableModel extends DefaultTableModel
{
public boolean isCellEditable(int row, int column) //override isCellEditable
//PRE: row > 0, column > 0
//POST: FCTVAL == false always
{
return false;
}
}
Code in my class: **NOTE** this class extends JPanel
private JScrollPane storesPane;
private JTable storesTable;
Code in the Constructor:
storesTable = new JTable(tableData, COL_NAMES); //tableData and COL_NAMES are passed in
storesTable.setModel(new MyTableModel());
storesPane = new JScrollPane(storesTable);
storesTable.setFillsViewportHeight(true);
add(storesPane, BorderLayout.CENTER);
Hopefully some of you Java Gurus can find my error :)
This line creates a new JTable and implicitly creates a DefaultTableModel behind the scenes, one that holds all the correct data needed for the JTable:
storesTable = new JTable(tableData, COL_NAMES);
And this line effectively removes the table model created implicitly above, the one that holds all of the table's data and replaces it with a table model that holds no data whatsoever:
storesTable.setModel(new MyTableModel());
You need to give your MyTableModel class a constructor and in that constructor call the super constructor and pass in the data that you're currently passing to the table in its constructor.
e.g.,
public class MyTableModel extends DefaultTableModel {
public MyTableModel(Object[][] tableData, Object[] colNames) {
super(tableData, colNames);
}
public boolean isCellEditable(int row, int column) {
return false;
}
}
Then you can use it like so:
MyTableModel model = new MyTableModel(tableData, COL_NAMES);
storesTable = new JTable(model);
Earlier today I had the same problem. This solved it for me.
JTable table = new JTable( data, headers ){
public boolean isCellEditable(int row, int column){
return false;
}
};
works great!
Just override the isCellEditable method of the DefaultTableModel class. The quick way to do this:
JTable table = new JTable();
DefaultTableModel dtm = new DefaultTableModel(0, 0) {
public boolean isCellEditable(int row, int column) {
return false;
}
};
table.setModel(dtm);
Hello Friend am also working on table please try my code
import javax.swing.table.AbstractTableModel;
public class Table extends AbstractTableModel {
private boolean DEBUG = false;
private String[] columnNames = {" Date Time", " Parameter",
" Barometric Pressure (hPa)", " Temperature (°C)", " Battery Voltage (V)"};
public static Object[][] data = {};
public TableControllerViewdataTabTableModel() {
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public int getRowCount() {
return data.length;
}
#Override
public String getColumnName(int col) {
return columnNames[col];
}
#Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
#Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
#Override
public boolean isCellEditable(int row, int col) {
return false;
}
#Override
/**
* The setValueAt.
*/
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
printDebugData();
}
}
/**
* The printDebugData.
*/
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
}
}
}
}

Categories