Can anyone solve this?
Thanks Iris
Here is the source-code (3 Classes :
1. MyJTable = A Frame containing a JTable
2. MyTableModel = the TableModel
3. DataObject = Object contained in the model)
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.table.*;
import java.awt.*;
public class MyJTable extends JFrame implements TableModelListener {
private JPanel frameContentPane = null;
private JTable scrollPaneTable = null;
private JScrollPane sP = null;
private MyTableModel myTM = null;
public MyJTable (){
super();
setName("MyTable");
setSize(650, 241);
setContentPane(getJFrameContentPane());
getTM().addTableModelListener(this);
//The Table model is set to the table in MyJTable() as follows:
getScrollPaneTable().setModel(getTM());
getScrollPaneTable().createDefaultColumnsFromModel();
}
private JPanel getJFrameContentPane() {
if (frameContentPane == null) {
frameContentPane = new JPanel();
frameContentPane.setName("JFrameContentPane");
frameContentPane.setLayout(new BorderLayout());
frameContentPane.add(getSP(), "Center");
};
return frameContentPane;
}
private JTable getScrollPaneTable() {
if (scrollPaneTable == null) {
scrollPaneTable = new JTable();
scrollPaneTable.setName("ScrollPaneTable");
getSP().setColumnHeaderView(scrollPaneTable.getTableHeader());
getSP().getViewport().setBackingStoreEnabled(true);
scrollPaneTable.setBounds(0, 0, 200, 200);
scrollPaneTable.setAutoCreateColumnsFromModel(false);
scrollPaneTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
};
return scrollPaneTable;
}
private JScrollPane getSP() {
if (sP == null) {
sP = new JScrollPane();
sP.setName("SP");
sP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
sP.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
getSP().setViewportView(getScrollPaneTable());
};
return sP;
}
private MyTableModel getTM() {
if (myTM == null) {
myTM = new MyTableModel();
java.util.Vector aRow = new DataObject().toRowVector();
myTM.addRow(aRow);
};
return myTM;
}
public static void main(java.lang.String[] args) {
try {
MyJTable aMyJTable;
aMyJTable = new MyJTable();
try {
Class aCloserClass =
Class.forName("com.ibm.uvm.abt.edit.WindowCloser");
Class parmTypes[] = { java.awt.Window.class };
Object parms[] = { aMyJTable };
java.lang.reflect.Constructor aCtor =
aCloserClass.getConstructor(parmTypes);
aCtor.newInstance(parms);
} catch (java.lang.Throwable exc) {};
aMyJTable.setVisible(true);
} catch (Throwable exception) {
System.err.println("Exception occurred in main() of
JFrame");
exception.printStackTrace(System.out);
}
}
/**
* tableChanged method comment.
*/
public void tableChanged(TableModelEvent e) {
}
}
import com.sun.java.swing.table.*;
import com.sun.java.swing.event.*;
import java.util.*;
public class MyTableModel extends AbstractTableModel {
protected Vector rows = new Vector();
protected final String[] columnIdentifiers = {"StringColumn1",
"StringColumn2", "BooleanColumn"};
public MyTableModel() {
this(0);
}
public MyTableModel(int numRows) {
rows = new Vector();
setNumRows(numRows);
}
public void addRow(Vector rowData) {
if (rowData == null) {
rowData = new Vector(getColumnCount());
}
else {
rowData.setSize(getColumnCount());
}
rows.addElement(rowData);
newRowsAdded(new TableModelEvent(this, getRowCount()-1,
getRowCount()-1,
TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}
public Class getColumnClass(int c) {
return getValueAt(0,c).getClass();
}
public int getColumnCount() {
return columnIdentifiers.length;
}
public String getColumnName(int column) {
if (columnIdentifiers == null || columnIdentifiers.length <=
column) {
return super.getColumnName(column);
}
Object id = columnIdentifiers[column];
if (id == null) {
return super.getColumnName(column);
}
else {
return id.toString();
}
}
public int getRowCount() {
return rows.size();
}
public Object getValueAt(int aRow, int aColumn) {
Vector row = (Vector)rows.elementAt(aRow);
return row.elementAt(aColumn);
}
public void newRowsAdded(com.sun.java.swing.event.TableModelEvent event)
{
int start = event.getFirstRow();
int end = event.getLastRow();
if (start < 0)
start = 0;
if (end < 0)
end = getRowCount()-1;
for (int i = start; i < end; i++)
((Vector)rows.elementAt(i)).setSize(getColumnCount());
fireTableChanged(event);
}
public void setNumRows(int newSize) {
if ((newSize < 0) || (newSize == getRowCount()))
return;
int oldNumRows = getRowCount();
if (newSize <= getRowCount()) {
rows.setSize(newSize);
fireTableRowsDeleted(getRowCount(), oldNumRows-1);
}
else {
int columnCount = getColumnCount();
while(getRowCount() < newSize) {
Vector newRow = new Vector(columnCount);
newRow.setSize(columnCount);
rows.addElement(newRow);
}
fireTableRowsInserted(oldNumRows, getRowCount()-1);
}
}
public void setRows(Vector v) {
Vector y = new Vector();
for ( int i=0; i<=v.size()-1; i++)
//toRowVector returns a Vector containing the attribute
values of the instance
//of DataObject to display in the tables columns (Make a
row from an object)
y.addElement(((DataObject)v.elementAt(i)).toRowVector());
rows = y;
fireTableChanged(new TableModelEvent(this,-1,-1));
}
public void setValueAt(Object aValue, int row, int column) {
Vector rowVector = (Vector)rows.elementAt(row);
if (column == 3)
rowVector.setElementAt((Boolean)aValue, column);
else rowVector.setElementAt(aValue, column);
rowVector.setElementAt(aValue, column);
fireTableChanged(new TableModelEvent(this, row, row, column));
}
}
import java.util.*;
public class DataObject {
Boolean flag = null;
String lastName = null;
String firstName = null;
public DataObject() {
super();
firstName = "Vorname";
lastName= "Nachname";
flag = new Boolean(true);
}
public Vector toRowVector () {
Vector v = new Vector(3);
v.addElement(firstName);
v.addElement(lastName);
v.addElement(flag);
return v;
}
}
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
: Can anyone solve this?
Yes.
The problem is the opaque flag, which is set to false per default.
JTable uses an internal renderer for Boolean values that does not
change this flag.
Just create your own renderer for the boolean values, setting the
background and the opaque flag when selected.
Look at the swing source to see what happens, sometimes it's the only
way to find out ...:-(
--
-------------------------------------------------------------------------
"Is it a 8086 or a Pentium with M$ Software?"
urs
public VVCheckBoxRenderer(JTable aTable) {
super();
setTable(aTable);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected)
setBackground(table.getSelectionBackground());
else setBackground(table.getBackground());
setOpaque(isSelected);
setSelected(value != null && ((Boolean)value).booleanValue());
return this;
}
public void setTable(JTable aTable) {
table = aTable;
}
}
ischw...@my-dejanews.com wrote:
> I want to use a JTable that contains columns with Boolean values. The column
> values are displayed as checkBoxes. My problem is that selecting a row
> excludes the Boolean columns from selection (they are selected but the
> selectionBachgroundColor is not shown). What my mistake?
>
> Can anyone solve this?
>
> Thanks Iris
>
> Here is the source-code (3 Classes :
> 1. MyJTable = A Frame containing a JTable
> 2. MyTableModel = the TableModel
> 3. DataObject = Object contained in the model)
>
... source code deleted look for it in the original message
In article <7aj4cg$6u$1...@nnrp1.dejanews.com>,
ischw...@my-dejanews.com wrote:
> In article <794a1a$r8h$1...@nnrp1.dejanews.com>, The solution is: public class
> VVCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
> JTable table = null;
>
> public VVCheckBoxRenderer(JTable aTable) {
> super();
> setTable(aTable);
> }
>
> public Component getTableCellRendererComponent(JTable table, Object value,
> boolean isSelected, boolean hasFocus, int row, int column) {
> if (isSelected)
> setBackground(table.getSelectionBackground());
> else setBackground(table.getBackground());
> setOpaque(isSelected);
>
> setSelected(value != null && ((Boolean)value).booleanValue());
> return this;
> }
>
> public void setTable(JTable aTable) {
> table = aTable;
> }
> }
>
> ischw...@my-dejanews.com wrote:
> > I want to use a JTable that contains columns with Boolean values. The column
> > values are displayed as checkBoxes. My problem is that selecting a row
> > excludes the Boolean columns from selection (they are selected but the
> > selectionBachgroundColor is not shown). What my mistake?
> >
> > Can anyone solve this?
> >
> > Thanks Iris
> >
> > Here is the source-code (3 Classes :
> > 1. MyJTable = A Frame containing a JTable
> > 2. MyTableModel = the TableModel
> > 3. DataObject = Object contained in the model)
> >
> ... source code deleted look for it in the original message
>