Adding button to a jtable - java

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.

Related

JButton action is not performed inside of JTable

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.

How to make the JComboBox dropdown always visible in a JTable

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;
}
});

How do I add a JLabel[] to a JTable?

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;
}
}

Multiline JTable Cells are not multilined during editing

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();
}
}

JCombobox, Editor and Renderer related

As a JCombobox ListCellRenderer, I have a class like this one:
class ZComboBoxRenderer extends JPanel implements ListCellRenderer{
private ZGrid grid;
public ZComboBoxRenderer(ZGrid grid) {
setLayout(new BorderLayout());
this.grid = grid;
add(new JScrollPane(grid), BorderLayout.CENTER);
}
public ZGrid getGrid(){
return grid;
}
#Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
grid.fetchSQL();
return this;
}
}
ZGrid here, extends JTable.
As a ListCellRendererComponent, I provide a JPanel which has a ZGrid inside, to the JCombobox. The problem is, in its list, this ZGrid is painting properly. But it is also being painted inside the Editor of JCombobox. I have uploaded an image to show this better.
Is there a way to separate Editor from List?
alt text http://img444.imageshack.us/img444/564/soex.jpg
From what I understand, you are implementing a custom Renderer for your JComboBox, and though it correctly renders the contents of your dropdown, it completely messes up the current value of the combo box.
I see two options at your disposal:
you can extend the UI component for your JComboBox and override the paint method to get a custom representation of your grid for your current value view. This would be a pretty quick proof of concept, but it poses issues as you would need to extend every UI (metal, windows, mac, etc) that you expect your app to be running with.
you can roll your own dropdown, and make it look like a JComboBox. This would not be that difficult to do as a POC as well, but the complexity here is to handle the different keyboard inputs that influence the selection and navigation around the combo box.

Categories

Resources