I am trying to implement a JTable which will format cells in columns in ways depending on their type. I am implementing TableCellRenderer's `getTableCellRendererComponent()' method to achieve this.
The problem is that my getTableCellRendererComponent() method never seems to be called, as the test output in the code never appears in the console.
Here is the code for the renderer:
public class MenuSheetTableCellRenderer extends JLabel implements TableCellRenderer {
#Override
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column){
JLabel label = new JLabel();
System.out.println("test");
if( value instanceof GregorianCalendar ){
System.out.println("test2");
GregorianCalendar timeGregorianCalendar = (GregorianCalendar) value;
Date time = timeGregorianCalendar.getTime();
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm a");
String mealTime = timeFormat.format(time);
label.setText(mealTime);
}
else if( value instanceof MealChoice){
label.setText(value.toString());
}
else if( value instanceof Recipe){
label.setText(" " + value.toString());
}
if (value instanceof String || value instanceof MealChoice){
label.setFont(new Font("Tahoma", Font.BOLD, 11));
}
return label;
}
}
Here is the code (edited for relevant code) which generates the table. Note that getRows() is my custom method that returns the data to be rendered and also that I am using netbeans (hence the abridged initComponents() method). Should be irrelevant.
private void initComponents() {
menuSheetTable = new javax.swing.JTable();
menuSheetTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
menuSheetTable.setEnabled(false);
menuSheetTable.setFocusable(false);
menuSheetTable.setShowHorizontalLines(false);
menuSheetTable.setShowVerticalLines(false);
jScrollPane1.setViewportView(menuSheetTable);
}
public void renderTable(){
String[] columns = { "Sunday", "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
Object[][] rows = getRows();
DefaultTableModel menuSheetModel = new DefaultTableModel( rows, columns );
menuSheetTable.setModel( menuSheetModel );
MenuSheetTableCellRenderer renderer = new MenuSheetTableCellRenderer();
try{
menuSheetTable.setDefaultRenderer(Class.forName("java.lang.String"), renderer);
menuSheetTable.setDefaultRenderer(Class.forName("java.util.GregorianCalendar"), renderer);
menuSheetTable.setDefaultRenderer(Class.forName("MenuSystemManager.MealChoice"), renderer);
menuSheetTable.setDefaultRenderer(Class.forName("MenuSystemManager.Recipe"), renderer);
}
catch( ClassNotFoundException e){
System.exit(1);
}
}
The code which set the default renderers doesn't have the Object object registered. Because the rows[][] was declared to hold type "Object," the renderer was never called because it was passed Object objects, not GregorianCalendars, Strings, or Recipes.
I ran into the same issue. Well, you have already found the reason why it behaves like this. So, here is the solution.
JTable works with TableModel interface that supplies table with all information about the data it holds. This information also contains the class associated with a column. So basically, what you need to do is to derive TableModel or AbstractTableModel with your class which will hold the data for the table and override the method public Class<?> getColumnClass(int columnIndex).
After that enhancement you can use your custom TableCellRenderer.
In your code you supplied JTable with a DefaultTableModel which returns Object for any column and that's why it did not work out.
Related
I am using glazedlists for JComboBox in java to make a JComboBox searchable and sortable. But there is situation I can't get to solve it.
I have a JComboBox attached with glazedlists. glazedlists takes String array to fill this combobox and make it searchable like this
String[] Values = {"ABC", "DEF", "GHI", "JKL", "MNO"};
JComboBox cmb = new JComboBox();
AutoCompleteSupport.install(cmb , GlazedLists.eventListOf(Values));
This works good but the problem is that I want to add ID along with value coming from Database and for that I am implementing my own custom ListCellRenderer like this
class MyListRenderer extends JLabel implements ListCellRenderer
{
#Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Object[] itemData = (Object[])value;
setText((String)itemData[1]);
return this;
}
}
cmb.setRenderer(new MyListRenderer());
And to add value in JComboBox, I have to
while(rs.next()){
int id=rs.getInt("company_id");
String category=rs.getString("company_name");
Object[] itemData = new Object[] {id, category};
cmb.addItem(itemData);
}
Now how can I implement my JComboBox with glazedlists while setting my own custom renderer?
Didn't have any success with your way, but I found a solution in an earlier project. You can set the model of the JComboBox instead by doing something like this:
//load the list of objects to use
ContainerClass[] allOptions = ContainerClass.getAll();
//create an EventList with this list and set is as the combobox model
final EventList<ContainerClass> eventList = GlazedLists.eventList(Arrays.asList(allOptions));
comboBox.setModel(new DefaultComboBoxModel<ContainerClass>(allOptions));
//finally initialize the combobox by SwingUtilities if done on a non-UI thread
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
AutoCompleteSupport<ContainerClass> install =
AutoCompleteSupport.install(comboBox, eventList);
install.setFilterMode(TextMatcherEditor.CONTAINS);
install.setHidesPopupOnFocusLost(true);
install.setSelectsTextOnFocusGain(false);
install.setCorrectsCase(false);
}
});
And with a ContainerClass like:
class ContainerClass{
int id;
String company;
//helper method to get all the objects needed
static ContainerClass[] getAll(){
ContainerClass test = new ContainerClass();
test.id = 2;
test.company = "abc";
return new ContainerClass[]{test,test,test};
}
#Override
//this String is what actually will be displayed in the combobox
public String toString(){return "(" + id + ") " + company;}
}
And I'm assuming that your JComboBox has the following type:
JComboBox<ContainerClass> comboBox;
(I had to obfuscate the names of all variables, so there might be errors in the code. Let me know and I will correct them)
So to recap. GlazedLists uses the model to get the names, which again asks the ContainerClass for it's toString() method which will return the name to display in the JComboBox.
As a note, when you call comboBox.getSelectedItem() it will return an object of type ContainerClass, or null if it isn't a valid selection.
UPDATE
If you want to be able to control the order as well as the name, you would need to implement your own model for the ComboBox. Found this that seems to explain it well:
class MyComboBoxModel extends AbstractListModel implements ComboBoxModel {
String[] ComputerComps = { "Monitor", "Key Board", "Mouse", "Joy Stick",
"Modem", "CD ROM", "RAM Chip", "Diskette" };
String selection = null;
public Object getElementAt(int index) {
return ComputerComps[index];
}
public int getSize() {
return ComputerComps.length;
}
public void setSelectedItem(Object anItem) {
selection = (String) anItem; // to select and register an
} // item from the pull-down list
// Methods implemented from the interface ComboBoxModel
public Object getSelectedItem() {
return selection; // to add the selection to the combo box
}
}
I ahve a JTable that is supposed to be 2 columns (String, JComboBox). When i initialize the table everything looks good. As soon as a I select a value in the table the JComboBox cell aquires the data type of the selected item.
I want to keep the JCOmboBox there and have it fire the events of data change and the Table ignore data changes in that column and keep the ComboBox populated.
My table has this as an override
#Override
public TableCellEditor getCellEditor(int row, int column) {
Object value = super.getValueAt(row, column);
if (value != null) {
if (value instanceof JComboBox) {
return new DefaultCellEditor((JComboBox) value);
}
return getDefaultEditor(value.getClass());
}
return super.getCellEditor(row, column);
}
Implementation
JComboBox uploadBox = new JComboBox();
uploadBox.addItem(MyPanel.UPLOAD_OPTIONS.PROMPT);
uploadBox.addItem(MyPanel.UPLOAD_OPTIONS.UPLOAD);
uploadBox.addItem(MyPanel.UPLOAD_OPTIONS.DONT_UPLOAD);
Object[][] tableData = new Object[][]{
{"Upload data on save", uploadBox}
};
table.setModel(
new DefaultTableModel(tableData, new String[]{"Description", "Options"}) {
Class[] types = new Class[]{String.class, JComboBox.class};
boolean[] canEdit = new boolean[]{false, true};
#Override
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit[columnIndex];
}
});
table.getColumnModel().getColumn(1).setCellRenderer(new TableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int i, int i1) {
return (Component)o;
}
});
answer is quite simple don't put JComboBox to the XxxTableModel or to set getColumClass for JComboBox.class, this is wrong (sure is possible but with bunch of side effects), XxxTableModel (is designated for) can hold directly only standard Java data types (String, Date, Icon/ImageIcon, Integer, Double etc... )
XxxTableModel should be store (if you don't want to parsing between Java data types) the same data type like as is stored in DefaultComboBoxModel (noting clear what constans are MyPanel.XXX), e.g in XxxTableModel is stored String value when DefaultComboBoxModel has the same data types, similair logics for Date, Icon/ImageIcon, Integer or Double
for more info to read Oracle tutorial How to use Tables - Using a Combo Box as an Editor
In this code, I'm adding the data from Database. I want to set cell renderer with a label. But if I run this code I got only check box.
try {
List<Group> listgrChild = grMgmtModel.performList();
for (final Group group : listgrChild) {
table.getColumnModel().getColumn(0)
.setCellRenderer(new TableCellRenderer() {
// the method gives the component like whome the
// cell must
// be rendered
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean isFocused,
int row, int col) {
boolean marked = new Boolean(String
.valueOf(value));
JCheckBox rendererComponent = new JCheckBox();
if (marked) {
rendererComponent.setSelected(true);
}
return rendererComponent;
}
});
tbModel.addRow(new Object[] { group.getGroupName() });
}
You comment, "renderComponent.setText("Hello") is giving only last value."
Verify that your TableModel has individual storage for each row's check box state and label value. In this example, the class Value holds the relevant data:
private static class Value implements Comparable<Value> {
private Boolean selected;
private Double value;
…
}
The corresponding TableModel manages a List<Value> and the required renderer and editor use the data from each Value instance accordingly. As an aside, Value implements the Comparable interface for convenience in sorting.
I created a Java GUI that displays the table using the following syntax:
table = new JTable(new MyTableModel(columnNames,
updateTable(cmbAdversary.getSelectedItem().toString(),
cmbdataType.getSelectedItem().toString())));
where columnNames is a Vector of Strings
cmbadversary and smbdataType are the selection od combo boxes.
and updateTable is a method that returns a Vector of Vectors depending on the combo box selection as follows:
static Vector updateTable(String FilterVal1 , String FilterVal2)
{
try {
myVector = tssc.testSeverityFunctionService(FilterVal1,FilterVal2);
} catch (Exception e) {
e.printStackTrace();}
return myVector;
}
This is how my custom class MyTableModel that extends AbstractTableModel looks like:
class MyTableModel extends AbstractTableModel
{
Vector columnNames = new Vector();
Vector Fdb = new Vector();
public MyTableModel(Vector cName,Vector rName){
this.columnNames = cName;
this.Fdb = rName;}
public int getColumnCount() { // number of columns in the model.
return columnNames.size();
}
public int getRowCount() { // number of rows in the model.
return Fdb.size();
}
#Override
public String getColumnName(int col) {
return columnNames.get(col).toString();
}
public Object getValueAt(int row, int col) {
Vector v = (Vector) this.Fdb.get(row);
return v.get(col);
}
#Override
public Class getColumnClass(int c) {
Vector v = (Vector) Fdb.get(0);
return v.get(c).getClass();}
public boolean isCellEditable(int row, int col)
{ return true; }
public void setValueAt(Vector value, int row, int col)
{
for(int i=0;i<value.size();i++)
{ for(int j=0;j<columnNames.size();j++) {
Fdb.setElementAt(value.get(j),j); }
}
fireTableCellUpdated(row, col);
}
}
The problem is that when I run the code, the table GUI show me initial values but fails to update when I change the selection in the 2 comboboxes and click the selection button.
The Selection button, btw, calls a method which implements the action listener.
Please help me out. Am no pro in Java, but willing to learn. If you have any followup qs., I'll be happy to provide details.
Your solution seems overly complicated. If I understand the basics, the user chooses a value from a combo box, then based on the selection some data is loaded into the table.
There is no need to create a custom table model to do this.
A TableModel contains data. If you want to change the data, then one way to do this is to simply create a new TableModel. So you add an ActionListener to your combo box. When an item is selected you retrive your data and load the data into an Vector or an Array. Using this data you can create a new TableModel and update the JTable in two lines of code:
DefaultTableModel model = new DefaultTableModel(...);
table.setModel( model );
If you need to customize the model to override the getColumnClass() or isCellEditable() methods, then you should extend the DefaultTableModel. I don't see any need to implement the whole model.
I have a tree and a table on my panel, when I click the tree node, the table needs to change at the same time, but it doesn't. I search online and read the Java tutorial and didn't find any solutions. From some posts I think I need to use fireTableStruetureChanged(), but it just doesn't work in my code. Could anyone help me out of this? The following is the code. Thanks a ton!
public class tableStructureChange extends JFrame implements ... {
.....
/ //columnNames is a public variable, because I need to change the columns later
columnNames = new String[] {"col1","col2"}; */
data = new String[][]{
{"Mary", "Campione"},
{"Alison", "Huml"}, };
table = new JTable(new MyTableModel());
table.setAutoCreateColumnsFromModel( false );
feedback = new JScrollPane(table); //feedback is the bottom panel
...
}
//the following class is the problem, i need the table to be reloaded
//when the class is called, but the table doesn't change at all
public void displayFeedback(String tempString) {
//create table for bottom panel
columnNames = new String[] {"col3","col4", "col5"};
String[][] data = new String[][]{
{"Mary", "Campione", "us"},
{"Alison", "Huml", "canada"}, };
//table = new JTable(data, columnNames);
//fireTableStructureChanged(); //this is the problem part
}
// my table model
class MyTableModel extends AbstractTableModel {
String[] columnNames = new String[] {"col1","col2"};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
...
}
In your method displayFeedback you seem to be hoping to replace the JTable object and have the display change to reflect what is selected in the JTree above. Instead of replacing what is in the View object, you should focus your effort on updating the Model, in this case, the AbstractTableModel subclass that you have created. There are a couple ways you can do that, but for a brute force proof of concept, you could do something like the following:
add a constructor to MyTableModel that takes a 2 dimensional array of data
in displayFeedback, create a new instance of MyTableModel that has new data relevant to the tree node that was selected.
call setModel on your global table variable.