JComboBox fails to expand in JTable TableHeader - java

I have read through the majority of the JTable/JComboBox responses to other questions of this ilk, but haven't yet found a solution to my problem.
I have created a table that has JComboBox TableHeader elements. None of the JComboBox elements will open to display a list of items. How do I get the item lists for the individual JComboBox elements to display?
Please note that a distinguishing element of this question is that the JComboBox is in the TableHeader, not embedded within a JTable cell.
Any help is appreciated.
SSCE
import java.awt.Component;
import java.awt.Dimension;
import java.util.Enumeration;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class ComboHeaderTest extends JScrollPane {
private static final Dimension DEFAULT_SIZE = new Dimension(200, 200);
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ComboHeaderTest().initComponents();
}
});
}
private ComboHeaderTest() {
final String[][] data = { {"Header 1", "Header 2", "Header 3"},
{"A", "B", "C"},
{"D", "E", "F"},
{"G", "H", "I"}};
setViewportView(getTable(data));
setPreferredSize(DEFAULT_SIZE);
}
private void initComponents() {
JFrame frame = new JFrame("ComboHeaderTest");
frame.add(this);
frame.pack();
frame.setVisible(true);
}
private JTable getTable(final String[][] data) {
final String[] items = data[0];
final ComboHeaderRenderer[] columnHeaders = new ComboHeaderRenderer[items.length];
for(int i = 0; items.length > i; ++i) {
columnHeaders[i] = new ComboHeaderRenderer(items);
}
final JTable table = new JTable(data, columnHeaders);
final Enumeration<TableColumn> tableEnum = table.getColumnModel().getColumns();
for (int columnIndex = 0; tableEnum.hasMoreElements(); ++columnIndex) {
final TableColumn column = tableEnum.nextElement();
final ComboHeaderRenderer combo = columnHeaders[columnIndex];
column.setHeaderValue(combo.getItemAt(columnIndex));
column.setHeaderRenderer(combo);
}
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(false);
table.setCellSelectionEnabled(false);
table.setFillsViewportHeight(true);
table.setSize(DEFAULT_SIZE);
table.validate();
return table;
}
private static class ComboHeaderRenderer extends JComboBox implements TableCellRenderer{
public ComboHeaderRenderer(final String[] entries) {
for (int i = 0; entries.length > i; ++i) {
addItem(entries[i]);
}
}
#Override
public Component getTableCellRendererComponent(final JTable table, final Object value,
final boolean isSelected, final boolean hasFocus, final int row, final int column) {
setSelectedItem(value);
return this;
}
}
}

This actually works exactly as expected. I think the clue is TableCellRenderer.
Renderer's are typically non-interactive components. They are usually just a "snap-shot" of the component painted on to a surface and there is typically no way for a user to interact with them.
This is the expected behavior.
In order to supply editable functionality to the table header, you're going to need to supply your implementation of a JTableHeader
Have a look at this example for some ideas

Here is an example that uses a JComboBox in a JTable TableHeader.
For other types of components is easier, have a look here.

Related

Disable JTableHeader update after row sorting

I customized a Jtable headers with a TableCellRenderer so filter icon would appear, and as the user clicks on the filter icon, a pop-up filter like excel's appears (picture 1). However, if the user clicks on the text of the header, a row sorter is appliead. When the row sorter is applied (picture 2), it override the customization and filter icon disappears. Is there a way to avoid this behavior while keeping the row sorter on the table header?
You can do this by decorating the original renderer of the table header with a custom one which will add the desired icon of filtering.
For example, follows some code to preserve the sort icons while adding your own label next to the original column header:
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.util.Objects;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
public class Main {
private static class MyTableCellRenderer extends DefaultTableCellRenderer {
private final TableCellRenderer originalRenderer;
public MyTableCellRenderer(final TableCellRenderer originalRenderer) {
this.originalRenderer = Objects.requireNonNull(originalRenderer);
}
#Override
public Component getTableCellRendererComponent(final JTable table,
final Object value,
final boolean isSelected,
final boolean hasFocus,
final int row,
final int column) {
final Component original = originalRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (row >= 0) //The header will have a row equal to -1.
return original;
final JPanel container = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
final JLabel filtering = new JLabel("Filter", JLabel.CENTER); //Your icon should go in this label...
//Put some fancy background to make the positioning of the label clear:
filtering.setOpaque(true);
filtering.setBackground(Color.CYAN);
//The default renderer comes with a border... Let's apply the border to the whole new container:
if (original instanceof JComponent) {
container.setBorder(((JComponent) original).getBorder());
((JComponent) original).setBorder(null);
}
container.add(filtering);
container.add(original);
return container;
}
}
private static void createAndShowGUI() {
final JTable table = new JTable(new Object[][] {
new Object[]{"Data001", "Data002", "Data003"},
new Object[]{"Data011", "Data012", "Data013"},
new Object[]{"Data021", "Data022", "Data023"},
new Object[]{"Data031", "Data032", "Data033"},
new Object[]{"Data041", "Data042", "Data043"}
}, new Object[] {"Column1", "Column2", "Column3"});
table.setAutoCreateRowSorter(true);
final JTableHeader header = table.getTableHeader();
header.setDefaultRenderer(new MyTableCellRenderer(header.getDefaultRenderer()));
final JFrame frame = new JFrame("Table header");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(Main::createAndShowGUI);
}
}
Instead of the word Filter (which appears on each column header) you can use your icons on the created label instead.
References:
Source (see the section: 3. Keeping sort icons).
How can I put a control in the JTableHeader of a JTable?

How to display differnt values in JCombo in each row

For my project i just want to show a dialogue box with JTable. On that i want to show a JCombobox with available staff based on row index. I tried the following coding,
for(int i=0;i<n;i++)
{
Object obj[] = new Object[4];
obj[0]=2,
obj[1]=3;
obj[2]=""; //Here combo appear.
obj[3]=3;
JComboBox aa = new JComboBox();
for(int j=0;j<m;j++)
{
aa.addItem(rs.getString(1));
aa.addItem(rs.getString(2));
}
table.getcolumnModel.getcolumn(2).setcellEditor(new DefaultCellEditor(aa));
model.addRow(obj);
}
if i use this output generated. But last rows combo value is present in all previous rows combo. Those different values are not in that. its totally same. But all other text fields are correctly displayed. What should i do here. thanking you...
Note: Here
aa.addItem(rs.getString(1));
aa.addItem(rs.getString(2));
is just for example. Actually it will return many number of values based on id.
You try to set editor to each row, but it's wrong, editor can be set to whole column. Read Concepts: Editors and Renderers. Instead of that implement your logic in getTableCellEditorComponent() method of TableCellEditor.
Simple example with different values for each row:
import java.awt.Component;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
public class TestFrame extends JFrame {
private DefaultComboBoxModel<String> model;
private Map<String, List<String>> keyVal;
public TestFrame() {
init();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void init() {
keyVal = new HashMap<>();
keyVal.put("1", Arrays.asList(new String[]{"a","b"}));
keyVal.put("0", Arrays.asList(new String[]{"c","d"}));
keyVal.put("2", Arrays.asList(new String[]{"e","f","g"}));
JTable t = new JTable(3,3);
t.getColumnModel().getColumn(0).setCellEditor(getEditor());
add(new JScrollPane(t));
}
private TableCellEditor getEditor() {
return new DefaultCellEditor(new JComboBox<String>(model = new DefaultComboBoxModel<String>())){
#Override
public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row, int column) {
model.removeAllElements();
if(keyVal.containsKey(row+"")){
List<String> list = keyVal.get(row+"");
for(String s : list)
model.addElement(s);
}
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
};
}
public static void main(String args[]) {
new TestFrame();
}
}

For one column of a JTable, how do I put a unique combo box editor in each row?

Here's the idea: Let's say I have a class extending TableModel, with something like a List<List<String>> collection field. On an event, I want to clear out a JTable and re-add rows where one specific column is a combo box; the items in combo box n are the items in the List<String> from collection.get(n) in my list. So I'm iterating over collection adding rows, and I'm iterating over each collection.get(n) adding combo box items.
There are no event listeners. When a separate button is clicked I can work out what needs to happen as long as I can getSelectedIndex on each combo box.
I have spent two and a half hours turning my GUI into code spaghetti where I've extended virtually everything and I have maximum coupling on practically everything. I think I might even have two disjoint collections of JComboBox instances. I say this to stress that it would do absolutely no good for me to post any code I currently have.
I have read the oracle tutorial on JTable, all of these that strangely don't have an explanation I can understand.
So basically I just need an idea of what I need, because I'm not getting anywhere with the resources I've found.
So, basically, you need a TableCelLEditor that is capable of seeding a JComboBox with the rows from the available list, for example...
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class TableCellEditor {
public static void main(String[] args) {
new TableCellEditor();
}
public TableCellEditor() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
List<List<String>> values = new ArrayList<>(25);
for (int row = 0; row < 10; row++) {
List<String> rowValues = new ArrayList<>(25);
for (int index = 0; index < 10; index++) {
rowValues.add("Value " + index + " for row " + row);
}
values.add(rowValues);
}
DefaultTableModel model = new DefaultTableModel(new Object[]{"Data"}, 10);
JTable table = new JTable(model);
table.setShowGrid(true);
table.setGridColor(Color.GRAY);
table.getColumnModel().getColumn(0).setCellEditor(new MyEditor(values));
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MyEditor extends DefaultCellEditor {
private List<List<String>> rowValues;
public MyEditor(List<List<String>> rowValues) {
super(new JComboBox());
this.rowValues = rowValues;
}
#Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
JComboBox cb = (JComboBox) getComponent();
List<String> values = rowValues.get(row);
DefaultComboBoxModel model = new DefaultComboBoxModel(values.toArray(new String[values.size()]));
cb.setModel(model);
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
}

Start editing in a cell in JTable on gaining focus

I have defined cell editors for the two columns in my table in the following manner:
Java Code:
JComboBox combo = new JComboBox();
//code to add items to the combo box goes here.
JTextField textField = new JTextField();
textField.setHorizontalAlignment(JTextField.RIGHT);
TableColumn column = myJTable.getColumnModel().getColumn(0);
column.setCellEditor(new DefaultCellEditor(combo));
column = myJTable.getColumnModel().getColumn(1);
column.setCellEditor(new DefaultCellEditor(textField));
The problem I am facing is that when a focus is moved to a table cell, the cell doesn't become automatically editable. So, when the focus is moved to column 2 (that has a text field as an editor), the caret sign doesn't not appear unless the cell is double-clicked or the user starts typing. Similar is the case for column 1 (that has a combo box as an editor) as here the combo box doesn't appear unless the cell is clicked. These behaviors are counter-intuitive and undesirable for a user operating with the keyboard.:(
Please suggest pointers on how this could be resolved.
Thanks in advance.
This example overrides editCellAt() in a JTable having a DefaultCellEditor using JTextField.
You can bind the Space key to the startEditing action defined for JTable:
table.getInputMap().put(
KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "startEditing");
As commented above, you can use JComboBox both as renderer and editor. Below is a very basic example. It also shows DefaultCellEditor.setClickCountToStart() usage.
import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class ComboBoxEditorDemo {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("ComboBoxEditorDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(
new Object[][] { { "1", "2" }, { "1", "2" } },
new Object[] {"Col1", "Col2" });
table.setRowHeight(24);
TableColumn column = table.getColumnModel().getColumn(1);
column.setCellRenderer(new MyComboBoxRenderer(new String[] { "1", "2", "3" }));
column.setCellEditor(new MyComboBoxEditor(new String[] { "1", "2", "3" }));
DefaultCellEditor editor = new DefaultCellEditor(new JTextField());
editor.setClickCountToStart(1);
column = table.getColumnModel().getColumn(0);
column.setCellEditor(editor);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}
static class MyComboBoxRenderer extends JComboBox implements
TableCellRenderer {
public MyComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelectedItem(value);
return this;
}
}
static class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}
}

JTable with horizontal scrollbar

Is there any way to enable horizontal scroll-bar whenever necessary?
The situation was as such: I've a JTable, one of the cells, stored a long length of data. Hence, I need to have horizontal scroll-bar.
Anyone has idea on this?
First, add your JTable inside a JScrollPane and set the policy for the existence of scrollbars:
new JScrollPane(myTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Then, indicate that your JTable must not auto-resize the columns by setting the AUTO_RESIZE_OFF mode:
myJTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Set the AutoResizeMode to OFF in the properties of the jTable
For reference, here's a minimal example of the accepted approach. Moreover,
You can adjust the size of individual columns as shown in Setting and Changing Column Widths, as well as here and here.
You can adjust the overall size of the enclosing scroll pane as shown in Implementing a Scrolling-Savvy Client, as well as here and here.
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
/**
* #see https://stackoverflow.com/a/37318673/230513
*/
public class Test {
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableModel model = new AbstractTableModel() {
private static final int N = 32;
#Override
public int getRowCount() {
return N;
}
#Override
public int getColumnCount() {
return N;
}
#Override
public Object getValueAt(int rowIndex, int colIndex) {
return "R" + rowIndex + ":C" + colIndex;
}
};
JTable table = new JTable(model) {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(320, 240);
}
};
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
f.add(new JScrollPane(table));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}
For me it works:
table.setAutoscrolls(true);
I had to do several things to get mine working
Set autoResize to AUTO_RESIZE_OFF.
Set preferredSize on JTable to null.
Set horizontalScrollBarPolicy on JScrollPane to AS_NEEDED.

Categories