Appending column to List or Vector - java

I have stored in a List a dataset from the Database. (It contains data of a single column).
In my inner class which implements my own TableDataModel I simply convert that List to a Vector to feed the JTable. In a second column I would like to display for each row a JCheckBox (rows with ticked Checkboxes will be stored afterwards in the database).
How can I extend/append the List/Vector with the "Boolean.TRUE" or "Boolean.False" as the value for the second column?
Sorry I am bloody newbie... thanks a lot
class SetVehicleCategoryTableModel extends AbstractTableModel {
private List<Category> all = null;
Vector<Object> tableData;
Vector<String> tableHeaders;
public SetVehicleCategoryTableModel(){
initModel();
}
public void initModel(){
tableData = new Vector<Object>();
// TableModel's column names
tableHeaders = new Vector<String>();
tableHeaders.add("Category");
tableHeaders.add("Selection");
// Database call
all = new ReadCategory().allCategories();
// TableModel's data
// converting List<Category> -> Vector<Object> tableData
for(Object o : all) {
tableData.addElement(o);
//tableData.add(,o);
// debug:
System.out.println("item added");
}
//debug:
System.out.println(tableData.size());
for(Object obj : tableData){
System.out.println(obj.toString());
}
}
#Override
public int getRowCount() {
return tableData.size();
}
#Override
public int getColumnCount() {
return tableHeaders.size();
}
#Override
public String getColumnName(int column){
return tableHeaders.elementAt(column);
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
return tableData.elementAt(rowIndex);
//return ((Vector<Object>) tableData.get(rowIndex)).get(columnIndex);
}
public void removeRow(int row) {
tableData.removeElementAt(row);
fireTableDataChanged();
}
public void insertRow(Object aValue) {
tableData.addElement(aValue);
fireTableDataChanged();
//fireTableRowsInserted(index, index);
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex){
Category edit = (Category) tableData.elementAt(rowIndex);
edit.setName((String) aValue);
fireTableCellUpdated(rowIndex, columnIndex);
//fireTableRowsUpdated(rowIndex, columnIndex);
}
// This method is used by the JTable to define the default
// renderer or editor for each cell. For example if you have
// a boolean data it will be rendered as a check box. A
// number value is right aligned.
/*#Override
public Class<?> getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
//return data[0][columnIndex].getClass();
}*/
public Class<?> getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
}

Related

show specific column in JTable with a predefined model

I have a JTable with 10 columns headers: "A","d","e","f","B","g","h","C","i","j".I want in first view JTable Show only "A","B","C" and I have two JButton when clicked on ViewAll Button all columns show and when clicked on hide Jtable show only three columns with "A","B","C" headers.how can do it?my GridTableModel is:
public abstract class GridTableModel<T> extends AbstractTableModel {
private static final long serialVersionUID = 4283080272635443348L;
private List<T> rows = new ArrayList<T>();
/**
* The property used to find real index of rows that currently are shown to user.
*/
private int offset;
public abstract String[] getColumnNames();
#Override
public String getColumnName(int column) {
return getColumnNames()[column];
}
#Override
public int getColumnCount() {
return getColumnNames().length;
}
#Override
public int getRowCount() {
return rows == null ? 0 : rows.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (rows.size() > 0) {
return getValueAt(rows.get(rowIndex), rowIndex, columnIndex);
}
return null;
}
public void setData(List<T> results, int offset) {
this.rows = results;
this.offset = offset;
fireTableDataChanged();
}
public T get(int row) {
return rows.size() > 0 ? rows.get(row) : null;
}
public abstract Object getValueAt(T t, int rowIndex, int columnIndex);
public List<T> get(int[] rows) {
List<T> list = new ArrayList<T>();
for (int row : rows) {
list.add(this.rows.get(row));
}
return list;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public List<T> getRows() {
return rows;
}
#Override
public Class<?> getColumnClass(int columnIndex) {
if (1==1) {
return Object.class;
}
return getValueAt(0, columnIndex).getClass();
}
}

JTable Rows Not Rendered in Expected Colors

I have a jtable , a customtablemodel and a customcellrenderer with the NetBeans IDE.
I want to have different colors for different rows. But anytime I run the application ,
the rows are not painted as expected.
The code snippets are provided below.
This code is from the jtable :
duesTable = new javax.swing.JTable();
duesTable.setModel(duestableModel);
TableColumn tcol = duesTable.getColumnModel().getColumn(2);
tcol.setCellRenderer(new CustomTableCellRenderer2());
duesTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
this code is from the TableCellRenderer
public class CustomTableCellRenderer2 extends DefaultTableCellRenderer{
#Override
public Component getTableCellRendererComponent (JTable table,
Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(
table, obj, isSelected, hasFocus, row, column);
if (isSelected) {
cell.setBackground(Color.green);
}
else {
if (row % 2 == 0) {
cell.setBackground(Color.green);
}
else {
cell.setBackground(Color.lightGray);
}
}
return cell;
}
}
This is from the Table Model.
public class DuesTableModel extends AbstractTableModel implements TableModelListener {
private List<List<Object>> dataList = new ArrayList<>();
private String[] header = { "ID"," PAYMENT YEAR" , "AMOUNT"}; // Payment year is a date
datatype
private int minRowCount = 5;
public DuesTableModel()
{ super(); }
public List<List<Object>> getDataList() {
return dataList;
}
public void setDataList(List<List<Object>> dataList) {
this.dataList = dataList;
fireTableDataChanged();
fireTableStructureChanged();
}
#Override
public int getRowCount() {
return Math.max(minRowCount, dataList.size());
}
#Override
public int getColumnCount() {
return header.length;
}
public void setHeader(String[] header) {
this.header = header;
}
public String[] getHeader() {
return header;
}
#Override
public void setValueAt(Object value, int row, int col)
{
int x = 0;
for(List<Object> l : dataList)
{
if(x == row)
{ l.set(col, value);}
x++;
}
fireTableCellUpdated(row,col);
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
if(rowIndex < dataList.size())
{value = dataList.get(rowIndex).get(columnIndex);}
return value;
}
#Override
public String getColumnName(int col) {
return header[col];
}
#Override
public Class<?> getColumnClass(int column)
{
switch (column) {
case 0:
return Integer.class;
case 1:
return Date.class;
case 2:
return Double.class;
default:
return String.class;
}
}
#Override
public boolean isCellEditable(int row, int col) {
return true; //col != 1;
}
#Override
public void tableChanged(TableModelEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Any suggestion to get the desired result.

How to bind booleans in JTable with JGoodies

I have seven boolean values in a column of a JTable that I want to bind to my bean.
How do I bind them?
All the JTable binding examples out there focus on binding the table selection, but I only care about what the values of those booleans are.
You need to implement your own data model. I give you simplified example that shows idea of usage. Take a look at getColumnClass method.
Usage: table.setModel(new DataModel(myData));
class DataModel extends AbstractTableModel
{
public DataModel(Object yourData){
//some code here
}
#Override
public int getRowCount() {
return yourData.rows;
}
#Override
public int getColumnCount() {
return yourData.colums;
}
#Override
public Class<?> getColumnClass(int col) {
if (col == myBooleanColumn) {
return Boolean.class;
} else {
return null;
}
}
#Override
public boolean isCellEditable(int row, int col)
{
return col >= 0;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
return yourData.get(rowIndex,columnIndex);
}
#Override
public void setValueAt(Object aValue, int row, int col) {
yourData.set(aValue,row,col)
this.fireTableCellUpdated(row, col);
}
}
Hope this helps.

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?

get cells odf a JTable

how to display a row of a jtable in a from of JTextField when click on the row,
( I need this to edit the data base from the JTable )
My table model
static class TableDataModel extends AbstractTableModel
{
private List nomColonnes;
private List tableau;
public TableDataModel(List nomColonnes, List tableau){
this.nomColonnes = nomColonnes;
majDonnees(tableau);
}
public void majDonnees(List nouvellesDonnees){
this.tableau = nouvellesDonnees;
fireTableDataChanged();
}
public int getRowCount(){
return tableau.size();
}
public int getColumnCount(){
return nomColonnes.size();
}
public Object getValueAt(int row, int col){
return ((ArrayList)( tableau.get(row))).get(col);
}
public String getColumnName(int col){
return nomColonnes.get(col).toString();
}
public Class getColumnClass(int c)
{
return getValueAt(0,c).getClass();
}
public boolean isCellEditable(int row, int col){
return true;
}
public void setValueAt(Object value, int row, int col)
{
((List)tableau.get(row)).set(col,value);
fireTableCellUpdated(row, col);
//i suppose i should update the database here
}
}
Use a ListSelectionListener. Whenever a row is selected you get the data from the model for the given row using table.getValueAt(...) and then you display the data in the text field of your form.

Categories