I would like a simple way to put a JList in a column of a JTable. I already have the JLists and the table, but when put in the table, the Jlists are displayed as Strings, which is normal, because I use DefaultTableModel. I have overriden the getColumnClass() as:
public Class<? extends Object> getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
but this just formats the integer and float values.
I suppose that setValueAt() and getValueAt() should also be overriden, in order to return am array of Strings when I call JList.getSelectedValues(), but I can't figure out how.
I also want the cells to be editable, so the users can choose one or more option from the JList. After editing a row, I use a Save button to save the changes in a database, so I don't think I need a ListSelectionListener, JList.getSelectedValues() works just fine.
I know this is a common question, but I couldn't find an answer here. If this is a duplicate, please let me know and I will delete it.
I've done it. For everyone who needs the same thing, here is what I've done:
1)I have created a JScrollTableRenderer, and set the column I needed to show the JList to use this renderer
table.getColumnModel().getColumn(5).setCellRenderer(new JScrollTableRenderer());
The JScrollTableRenderer class content:
public class JScrollTableRenderer extends DefaultTableCellRenderer {
JScrollPane pane = new JScrollPane();
public JScrollTableRenderer()
{
super();
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
pane = (JScrollPane) value;
return pane;
}
}
2)I have created a JScrollTableEditor, and set the column I needed to show the JList to use this editor
table.getColumnModel().getColumn(5).setCellEditor(new JScrollTableEditor());
The JScrollTableEditor class content:
public class JScrollTableEditor extends AbstractCellEditor implements TableCellEditor {
JScrollPane component = new JScrollPane();
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int rowIndex, int vColIndex)
{
component = ((JScrollPane) value);
return ((JScrollPane) value);
}
public Object getCellEditorValue()
{
return component;
}
}
3)I added this method in the JTable model:
public Class<? extends Object> getColumnClass(int c)
{
if(c == 5) return JScrollPane.class;
else return getValueAt(0, c).getClass();
}
Related
I am trying to change the color of the cells of the third row of my JTable if they have a value. I read that a good way to do this is using a table cell renderer. However, it just seems not to be doing anything! Here there is my RENDERER code:
public class RenderTablaPrestamos extends DefaultTableCellRenderer{
#Override
public Component getTableCellRendererComponent (JTable tabla, Object valor,
boolean isSelected, boolean hasFocus,
int row, int col){
JLabel celda = (JLabel) super.getTableCellRendererComponent(tabla, valor, isSelected, hasFocus, row, col);
if(valor instanceof Integer){
Integer v=(Integer)valor;
if(col==3){
if(valor!=null){
celda.setBackground(Color.red);
}
else{
celda.setBackground(Color.WHITE);
}
}
else{
celda.setBackground(Color.WHITE);
}
}
return celda;
}
}
Here there is how I use my renderer:
tablaUsuariosPrestamos.setDefaultRenderer(Object.class,new RenderTablaPrestamos());
Here there is a picture of my JTable (I do not think the model code would be any useful as it is kinda long):
I do not think it does anything to do with the if clausules, as I commented them and it did not work either.
Where am I going wrong?
Use
for (int i = 0; i < tabla.getColumnCount(); i++) {
tabla.getColumnModel().getColumn(i).setCellRenderer(new RenderTablaPrestamos());
}
instead of
tablaUsuariosPrestamos.setDefaultRenderer(Object.class,new RenderTablaPrestamos());
The code in setDefaultRenderer functions as intended:
final YourCellRenderer cellRenderer = new YourCellRenderer();
YourTableModel stModel = new YourTableModel();
table = new JTable(stModel);
table.setDefaultRenderer(YourComponent.class, cellRenderer);
The reason that it doesn't appear to render is because while you are mapping the YourComponent.class to the renderer, it's not firing because the YourTableModel thinks the class is an Object.
To correct this, you need to override the getColumnClass method within the YourTableModel class:
public class YourTableModel extends AbstractTableModel {
...
#Override
public Class<?> getColumnClass(int columnIndex) {
return YourComponent.class;
}
...
}
I want to create a JTable having the last column with advanced options icon. On clicking this last column in the JTable, I want a new JPanel to pop up allowing user to enter input for required 4 string input fields. This JPanel when dismissed, should return to the original JTable.
I am not sure where to save the data for 4 fields from the new JPanel. As their would be 4 string input fields per JTable row, just displayed in the JPanel.
Can my JTabel cell hold an object saving the data?
UseCase: I have a JTable with 10 columns. It is getting very cluttered so I want to move 5 columns to a new panel which will be launched on clicking an advanced options icon in the original JTable last column.
Sample code on how to associate the data from the JPanel with the row in JTable will be highly appreciated.
In order to show a pop-up when cell is clicked, you need a cell editor class. The main purpose of this class is to provide custom editors for cells, but you can use it to trigger some action when your cell is clicked:
public class InfoCellEditor extends AbstractCellEditor implements TableCellEditor {
#Override
public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
InfoObject info = (InfoObject) value;
editButton = new JButton(new InfoAction(info));
editButton.setText("INFO");
editButton.setEnabled(true);
}
private class InfoAction extends AbstractAction {
InfoObject info;
public InfoAction(InfoObject info) {
super();
this.info = info;
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, info.toString());
stopCellEditing();
}
}
}
Then, extend JTable class and implement getColumnClass and isCellEditable methods:
public class MyTable extends JTable {
public MyTable() {
super();
setDefaultEditor(InfoObject.class, new InfoCellEditor());
}
#Override
public Class getColumnClass(int columnIndex) {
if(columnIndex == 4)
return InfoObject.class;
else
return String.class;
}
#Override
public boolean isCellEditable(int row, int column) {
if(column == 4)
return true;
else
return false;
}
}
Lastly, you should make sure that InfoObject instances are inserted to 5th column. And you can also implement a TableCellRenderer for some custom visual representation of that column.
Object headers = new Object[COLUMN_COUNT];
Object cells[][] = new Object[ROW_COUNT][];
...
cells[0][4] = new InfoObject(data[0]);
cells[1][4] = new InfoObject(data[1]);
table.setModel(new DefaultTableModel(cells, headers));
table.getModel().fireTableDataChanged();
table.setVisible();
I am going to be using a lot of custom TableCellRenderers in a JTable, but I don't want to have to recreate the default properties for every single one, because it seems I have to start with a plain JLabel. This creates a ton of annoying overhead, because I even have to populate the value myself as well as the matching background, the foreground, the font, etc... to match the rest of the JTable.
Is there a way I can retrieve the parent TableCellRenderer or the it's resulting JLabel and manipulate that? That way I have all the defaults set already, and I can just manipulate the properties I am actually changing? I have tried everything with super.getCellRenderer and it is not giving me anything to accomplish that.
Also, my TableCellRenderer is not column-specific. Each cell can vary.
public class ActionTable extends JTable {
...
public TableCellRenderer getCellRenderer(int row, int column) {
String colName = ((ActionTableModel)this.getModel()).getColumnName(column);
if ( colName.equals("CUSTOM COL") ) {
return new ActionCellRenderer(this);
}
return super.getCellRenderer(this.convertRowIndexToModel(row), this.convertColumnIndexToModel(column));
}
public class ActionCellRenderer extends JLabel implements TableCellRenderer {
private ActionTable actionTable;
public ActionCellRenderer(ActionTable actionTable) {
this.actionTable = actionTable;
setOpaque(true); //MUST do this for background to show up.
}
#Override
public Component getTableCellRendererComponent(JTable table, Object color,boolean isSelected, boolean hasFocus,int row, int column) {
int modelRow = actionTable.convertRowIndexToModel(row);
int modelCol = actionTable.convertColumnIndexToModel(column);
String colName = table.getModel().getColumnName(modelCol);
/*
annoying overhead to retrieve default cell renderer properties
to match the rest of the JTable
*/
if (isSelected)
{
setBackground(table.getSelectionBackground());
setForeground(table.getSelectionForeground());
}
else
{
setBackground(table.getBackground());
setForeground(table.getForeground());
}
//AND I HAVE TO RETRIEVE THE VALUE MYSELF TOO
String textVal = ((ActionTableModel)table.getModel()).getValueAt(modelRow, modelCol).toString();
this.setHorizontalAlignment(SwingConstants.CENTER);
this.setFont(new Font("Serif", Font.PLAIN, 15));
this.setText(textVal);
//NOW I CAN DO CUSTOMIZATIONS
//PUT CUSTOMIZATIONS HERE
return this;
}
Just have your ActionCellRenderer extend DefaultTableCellRenderer and call the getTableCellRendererComponent() method to get the JLabel:
public class ActionCellRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus,int row, int column){
//get the label
JLabel label = (JLabel)super.getTableCellRendererComponent(table, value,isSelected, hasFocus,row, column);
//do whatever you want with the label
return label;
}
}
Does someone know a good way to display the sorting icons in the header of a JTable, without using the build in sort functionality?
The sorting is done by the table model (actually a database) and not by the JTable itself. Thats why the automatic display of the icons doesn't work. Maybe one can insert a dummy RowSorter that does nothing, but makes the sort icons appear?
I found a better Solution
I just wrote my own RowSorter, so that the sorting does not have any effect, but redirects the sorting request to the model instead. That way the sort order is displayed by the look and feel itself. Some Pseudocode:
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.swing.RowSorter;
import xyz.SortableTableModel;
public class MyRowSorter<M extends SortableTableModel> extends RowSorter<M> {
private M tableModel;
private List<? extends SortKey> sortKeys = new LinkedList<>();
public MyRowSorter(M tableModel) {
this.tableModel = tableModel;
}
#Override
public M getModel() {
return tableModel;
}
#Override
public void toggleSortOrder(int column) {
// redirecting sort request to model and modification of sortKeys
List<? extends SortKey> newSortKeys = ...;
setSortKeys(newSortKeys);
}
#Override
public int convertRowIndexToModel(int index) {
return index; // will always be the same
}
#Override
public int convertRowIndexToView(int index) {
return index; // will always be the same
}
#Override
public void setSortKeys(List<? extends SortKey> keys) {
if (keys == null) {
sortKeys = Collections.EMPTY_LIST;
} else {
sortKeys = Collections.unmodifiableList(keys);
}
fireSortOrderChanged();
}
#Override
public List<? extends SortKey> getSortKeys() {
return sortKeys;
}
#Override
public int getViewRowCount() {
return tableModel.getRowCount();
}
#Override
public int getModelRowCount() {
return tableModel.getRowCount();
}
// no need for any implementation
#Override public void modelStructureChanged() { }
#Override public void allRowsChanged() { }
#Override public void rowsInserted(int firstRow, int endRow) { }
#Override public void rowsDeleted(int firstRow, int endRow) { }
#Override public void rowsUpdated(int firstRow, int endRow) { }
#Override public void rowsUpdated(int firstRow, int endRow, int column) { }
}
In that case you can try to write a custom TableCellRenderer for JTableHeader.
Here is simple example of renderer:
private static class MyRenderer implements TableCellRenderer {
private ImageIcon icon1;
private ImageIcon icon2;
private TableCellRenderer defaultRenderer;
MyRenderer(JTable t){
defaultRenderer = t.getTableHeader().getDefaultRenderer();
icon1 = new ImageIcon(getClass().getResource("1.png"));
icon2 = new ImageIcon(getClass().getResource("2.png"));
}
#Override
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
Component c = defaultRenderer.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, col);
if(col%2 == 0){
((JLabel)c).setIcon(icon1);
} else {
((JLabel)c).setIcon(icon2);
}
return c;
}
}
Here icon1 and icon2 is your sorting icons.
And you can set that renderer for your JTableHeader like next:
table.getTableHeader().setDefaultRenderer(new MyRenderer(table));
table - is your JTable.
The sorting is done by the table model (actually a database) and not by the JTable itself.
Check out the DefaultRowSorter class. Maybe you use the setSortsOnUpdates(...) and setSortKeys(...) so the sorting icons match the sort from the database. You could try:
Creating an empty model
Set the sort keys
use setSortsOnUpdates(false);
Update the model using the setDataVector() (or some equivalent method if using a custom model)
Note this approach assumes you have created the TableModel with column names and no data and added the model to the JTable. I think you will then also need to use:
table.setAutoCreateColumnsFromModel(false);
to prevent the TableColumnModel from being recreated when you load the data into the model.
Solution is tricky when you want your code to work with other existing Swing layouts (I am talking about com.formdev .... flatlaf ). These L&Fs create a special Header renderer.
Here is a simple solution that will work with all main L&Fs on the market (tatoo, formdev, jgoodies). The trick is to subclass from DefaultTableCellHeaderRenderer but also to pass the table look and feel current header renderer as parameter.
// this custom renderer will display the sorting icon for all afftected columns.
class CustomTableHeaderRenderer extends DefaultTableCellHeaderRenderer implements TableCellRenderer{
final private Icon ascIcon = UIManager.getIcon("Table.ascendingSortIcon");
final private Icon descIcon = UIManager.getIcon("Table.descendingSortIcon");
TableCellRenderer iTableCellRenderer = null;
public CustomTableHeaderRenderer(TableCellRenderer tableCellRenderer)
{
iTableCellRenderer = tableCellRenderer;
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = (JLabel) iTableCellRenderer.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column) ;
List<? extends SortKey> sortKeys = table.getRowSorter().getSortKeys();
label.setIcon(null);
for (SortKey sortKey : sortKeys) {
if (sortKey.getColumn() == table.convertColumnIndexToModel(column)){
SortOrder o = sortKey.getSortOrder();
label.setIcon(o == SortOrder.ASCENDING ? ascIcon : descIcon);
break;
}
}
return label;
}
}
yourTable.getTableHeader().setDefaultRenderer( new CustomTableHeaderRenderer( yourTable.getTableHeader().getDefaultRenderer() ));
I have a column in a JTable with cells rendered as checkboxes below:
How do I prevent the checkboxes from moving every time I click on it? Here is my cell renderer and cell editor:
protected class CheckBoxCellRenderer extends JCheckBox implements TableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
this.setSelected((Boolean) tableModel.getValueAt(row, 0));
return this;
}
}
protected class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
private final CheckBoxCellRenderer cell = new CheckBoxCellRenderer();
private int row;
public CheckBoxCellEditor() {
cell.addActionListener(this);
}
#Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) {
cell.setSelected(((Boolean) value).booleanValue());
row = rowIndex;
return cell;
}
#Override
public Object getCellEditorValue() {
return cell.isSelected();
}
}
Thanks!
Don't create a custom renderer/editor.
JTable already supports a default renderer. Just override the getColumnClass(...) method of the TableModel or JTable to return Boolean.class for the first column and the JTable will use the default renderer/editor.