I'm using JComboBox with JTables, but the dropdown menu is only "visible" when it's clicked. How can I change this default behavior and make it always visible and user-friendly?
public void start(){
TableColumn column = table.getColumnModel().getColumn(0);
JComboBox comboBox = new JComboBox();
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement("a");
model.addElement("b");
comboBox.setModel(model);
}
As I understand it, you would like the cells to always look like JComboBoxes, and not jLabels.
This can easily be accomplished by adding a TableCellRenderer to your TableColumn.
Pasting in the following code should have the desired effect.
column.setCellRenderer(new TableCellRenderer() {
JComboBox box = new JComboBox();
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
box.removeAllItems();
box.addItem(value.toString());
return box;
}
});
Related
I got a simple JTable which one of it's cells is an Edit button which needs to open a new JDialog.
After a several examples from the web I created a new class which implements TableCellRenderer and returned a JButton from it but the button is not doing what I need for some reason.
This is my code:
final MessageResourcesTableModel model = new MessageResourcesTableModel(columnNames);
JTable resultsTable = new JTable(model);
resultsTable.setShowGrid(true);
resultsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
resultsTable.setPreferredScrollableViewportSize(resultsTable.getPreferredSize());
resultsTable.setFillsViewportHeight(true);
resultsTable.setFont(font13);
final TableCellRenderer buttonRenderer = new JTableButtonRenderer();
resultsTable.getColumn(COLUMN_EDIT).setCellRenderer(buttonRenderer);
And this is the renderer:
class JTableButtonRenderer implements TableCellRenderer {
#Override public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
final JButton editButton = new JButton("Edit");
editButton.setOpaque(true);
editButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(final ActionEvent e) {
System.out.println("Hi from Action !!!");
}
});
return editButton;
}
}
Although there is a simple ActionListener implementation, there is nothing over the console
A renderer only displays the data in the cell. You can't add an ActionListener to the button because it is not a real component. A renderer only paints an image of the button in the table.
You need to create a custom editor if you want to be able to click on the button.
See Table Button Column for an editor implementation that does this for you.
I have an array of JLabels which I want to add to a JTable. I tried using
myJTable.add(myJLabelArray);
Hoping it would work, but it doesn't (Obviously, otherwise I wouldn't be here).
Can somebody please help?
Using add method is not the way to add components to a JTable. Components should never be added directly to a JTable or its TableModel.
JLabels are just Swing components that render text.
You can use a TableCellRenderer. Have a look at Editors & Renderers
You cannot just add myJTable.add(myJLabelArray). As Reimeus pointed out use Renderers
jTable1.getColumnModel().getColumn(0).setCellRenderer(new Renderer()); //set column1 with jlabel
Your render should extend DefaulttableCellRenderer
class Renderer extends DefaultTableCellRenderer {
JLabel lbl = new JLabel();
//ImageIcon icon = new ImageIcon(getClass().getResource("sample.png"));
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
lbl.setText("hello");
//lbl.setIcon(icon);
return lbl;
}
}
I'm developing an app which has an JTable that needs to have multiline cells. Therefore I extended JTextArea and everything is shown noce, but when I try to edit a cell. the text is shown in a single line, and becomes multilined after edit. I want the text to stay multilined during editting.
Is there a way to do that?
Create your TableCellEditor using a JTextArea (instead of the default behaviour which uses JTextField) and set it to your JTable.
You can use a JEditorPane as well to support text styling, if you wish.
---- Edit2 ----
New TableCellEditor:
class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
JComponent component = new JTextArea();
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int rowIndex, int vColIndex) {
((JTextArea) component).setText((String) value);
return component;
}
public Object getCellEditorValue() {
return ((JTextArea) component).getText();
}
}
I want to save Name and value of jtable into two variable
public class NewClass extends JPanel implements TableModelListener {
private final String[] columnNames = { "Name", "Value","check"};
private JTable table;
private DefaultTableModel tableModel;
private final JButton buttonSave;
public NewClass(){
tableModel = new DefaultTableModel(columnNames, 0);
tableModel.addTableModelListener(this);
table = new JTable(tableModel);
javax.swing.table.TableColumn var_col;
var_col = table.getColumnModel().getColumn(2);
final JCheckBox check = new JCheckBox();
var_col.setCellEditor(new DefaultCellEditor(check));
var_col.setCellRenderer(new DefaultTableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int
column) {
check.setSelected(((Boolean)value).booleanValue()) ;
return check;
}
});
JScrollPane scrollPane = new JScrollPane(table);
setLayout(new BorderLayout());
setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
add(BorderLayout.NORTH, new JLabel("Mon panier", JLabel.CENTER));
add(BorderLayout.CENTER, scrollPane);
//--------I want to save these Name and value in two variables -----------
Object[] data1 = {
new String("work"), new String("done"),new Boolean(false)};
tableModel.addRow(data1);
buttonSave = new JButton("Save");
buttonSave.setEnabled(false);
buttonSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
buttonSave.setEnabled(false);
}
});
As discussed in How to Use Tables, your table's data should be managed by a TableModel such as AbstractTableModel or the concrete DefaultTableModel used in your example. In this example, DataModel extends AbstractTableModel and synthesizes a List<Value> of test data; yours would listen to whatever object monitors the serial port. The example also uses the class Value to encapsulate a selectable numeric value. The custom TableCellEditor updates each Value as it is changed, so the DataModel always contains the selection state of each element in the list. Your save button could then save the list elements in whatever format you prefer.
I am having a table in which i have to adda JButton.
I am doing
TableColumnModel colModel = table.getColumnModel();
colModel.getColumn(0).setCellEditor(new MYCellEditor(new JCheckbox()));
MyCellEditor extends DefaultCellEditor{
public MyCellEditor(JCheckbox checkbox){
super(checkbox);
Jbutton button = new JButton("Start");
//actionlistener for button.
}
}
MyRenderer extends DefaultTablecellRenderer{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
//return a button for column ==0
}
My understanding is that the Celleditor has same instance of button for all cells in a column. So if i click on one button the text changes from "Start" to "stop" but if i click on button in other row it doesnt work.. After debugging it shows that the text is alreadt Stop .
How can i have different instance of button in each row ?
The article Table Button Column cited in #camickr's previous answer provides a more flexible solution, but you may find the tutorial How to Use Tables: Using Other Editors helpful, too. The ColorEditor discussed there is part of the TableDialogEditDemo, available via Java Web Start. You'll need to change the corresponding ColorRenderer accordingly.