ImageIcon in JTableCell - java

I am having an issue adding an ImageIcon to my JLabel in my JTable. So far I am entirely able to manipulate the cell based on the value of the data in the cell however whenever I try to add in an image I am only seeing the text.
Table Renderer
class DeviceTableModel extends AbstractTableModel {
private Object[][] data = Globals.getArray();
private String[] columnNames = {"Name","Status","Description"};
#Override
public int getRowCount() {
return data.length;
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
#Override
public String getColumnName(int col) {
return columnNames[col];
}
#Override
public Class getColumnClass(int c) {
return getValueAt(0,c).getClass();
}
#Override
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row,col);
}
}
This is the Renderer I am using in my JTable.
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
JLabel comp = (JLabel)super.prepareRenderer(renderer, row, col);
Object value = getModel().getValueAt(row, col);
if (value.equals("online")) {
comp.setIcon(new ImageIcon("/Res/online.png"));
comp.setBackground(Color.green);
}else {
comp.setBackground(Color.white);
}
return comp;
}
The color and text set just fine but the icon will not display. Any ideas would be appreciated!
EDIT- Suggestions by VGR and Camickr
Your advice was spot on and resolved the issue! Take a look at the redone portion. I am very grateful. Thanks guys!
//preloaded just added here to show.
ImageIcon icon = new ImageIcon(getClass().getResource("/Res/onlineIcon.png"));
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
JLabel comp = (JLabel)super.prepareRenderer(renderer, row, col);
Object value = getModel().getValueAt(row, col);
if (value.equals("online")) {
comp.setIcon(icon);
comp.setBackground(new Color(173,255,92));
}else {
comp.setIcon(null);
comp.setBackground(Color.white);
}
return comp;
}
}

The ImageIcon constructor documentation makes it clear that the string argument is a filename. Unless your system has a Res directory in the root of the file system, you probably meant to do new ImageIcon(getClass().getResource("/Res/online.jpg")) or new ImageIcon(getClass().getResource("/online.jpg")).
Note that your else clause should be setting the icon to null, since a single renderer may be used for multiple table cells.

Related

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
}

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