I am currently reading an excel sheet (.xls) and placing it in a JTable. The excel sheet consists of 3 columns. I am successfully able to read it. However, when reading the excel sheet, I want to add an extra fourth column in the JTable that includes JButtons (One button for each row). When JButton is clicked in a row, I want to take the content of the third column and perform some action.
I am currently using the code from here.
What is the best way to add JButtons in a JTable column?
You can create a class extending JButton. Then add property to that class.(field with getter and setter) when you adding data to the table, add JButton instance for each row using your custom JButton class and set value in the third column using setter method. So you can use that value in processing in the click event. Hope it helps :)
You can add your button this way.
class MyRenderer implements TableCellRenderer {
JButton button = new JButton();
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
button.setText(value.toString());
return button;
}
and to add action listener do this
class mybutttoneditor extends AbstractCellEditor implements TableCellEditor,
ActionListener {
JTable table;
JButton button = new JButton();
public mybutttoneditor(JTable table) {
this.table = table;
button.setFont(new Font("Serif", Font.BOLD, 14));
button.setForeground(Color.blue);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
final int row = table.getEditingRow();
String column3data=table.getValueAt(row, 2);
//do what you want with the data here
//hopefully this helps and if so accept the answer
}
//other abstract methods are here
}
}
DefaultTableModel md=(DefaultTableModel)mytable.getModel();
//do this while reading your excel sheet
Object row[]={"dataone","datatwo","data3","Open Button"};
md.addRow(row);
TableColumnModel colModel = mytable.getColumnModel();
colModel.getColumn(3).setCellRenderer(new MyRenderer());
colModel.getColumn(3).setCellEditor(new mybutttoneditor(mytable));
Related
I have a JBtable and in one of the columns I would like to insert a button.
My attempt was as follows:
private class DeleteColumn extends ColumnInfo<Field, JButton> {
public DeleteColumn() {
super("Delete");
}
#Nullable
#Override
public JButton valueOf(final Field field) {
final JButton removalButton = new JButton();
removalButton.setText("-");
removalButton.addActionListener((e) -> {
// do something
});
return removalButton;
}
#Override
public Class<?> getColumnClass() {
return JButton.class;
}
}
However when this is rendered, it still only displays the .toString() of the JButton. How can I display a button in the table?
You should write a custom renderer. Please look at:
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender
There are also examples you could look at:
http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableRenderDemoProject/src/components/TableRenderDemo.java
I would like to insert a button
You should NOT be adding a JButton to the table. The data of the JTable should not be a Swing component. You should just be storing text in the column and then use a JButton to render the text.
For example check out Table Button Column for a class that allow you to use a button as the renderer/editor.
I am adding a combobox to the 3rd column of a table so..every time a new row is added it will create a new combobox and adds items from those in vector1
TableColumn ProfileCol = Table.getColumnModel().getColumn(3);
ProfileCol.setCellEditor(new tableList(vector1));
Here tableList is a a below mentioned class that extends DefaultcellEditor and its constructor method is
public tableList(java.util.Vector v) {
super(new JComboBox(v));
My problem is If I write an action even like
Table.addMouseListener(new java.awt.event.MouseAdapter() {
#Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
int row = Table.rowAtPoint(evt.getPoint());
int col = Table.columnAtPoint(evt.getPoint());
if (row >= 0 && col ==3) {
}
}
});
it is not getting triggered..
I need a code that will fire for every selection of item in the combo box
I need a code that will allow me to dynamically update the contents of the combobox that is inserted in the table
Please help with this.
every time I add a new row to the table a new combobox is generated..so how can I write an action listern to them
You should NOT be using an ActionListener.
You have three options:
Override the setValue(...) method of the TableModel to do your processing
Add a TableModelListener to the TableModel. Then you do you processing when the TableModelEvent is generated.
Use a Table Cell Listener on your table to handle the processing.
public class AdminControlPanel extends javax.swing.JFrame
I'm using the JFrame class...so it will automatically inherit the JFrame class...
But problem now is I want to disable the cell editing in the JTable...How to do it even I'm not inherit the AbstractTableModel???
If you want to use a custom table model:
//instance table model
DefaultTableModel tableModel = new DefaultTableModel() {
#Override
public boolean isCellEditable(int row, int column) {
return false; // or a condition at your choice with row and column
}
};
table.setModel(tableModel);
Or in a quick and dirty way:
table.setEnabled(false);
This second approach is inconsistent with some L&F (it looks grayed out).
I have a JTable that just contains one item which is a JButtton that has an icon which is set to the JFreeChart generated chart.
If you click the button it launches a new window with the JFreeChart chart.
I have a cell renderer for the button which simply returns the JButton object that contains the chart item.
If I single click on the button it will launch the new window.
But if I double click on the button the new window launches, but the icon on the button, which used to be the JFreeChart, changes to just a text string with the class name path of the JButton with the class field values.
They other way I can get this behavior to happen is if I remove the cell renderer then the JButton just diplays the class name. So I'm not sure what is going on and have played around with it a ton and haven't made any progress. I was going to post some code but there is just too much of it.
If anybody has any ideas, I can post certain sections of code to help explain.
Here is code:
Class init {
...
ItModel itModel = new ItModel ();
JTable table = new JTable(itModel );
TableRendrend rend = new TableRend();
table.getColumn("it").setCellRenderer(rend);
}
class TableRend implements TableCellRenderer {
JButton itButton;
...
getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int columns){
if(itButton ==null){
itButton = new JButton (JButton)value.getIcon();
}
return itButton;
}
}
Class itModel extends AbstractTableModel{
JButton button;
...
public Object getValueAt(int rowIndex, intColumnIndex){
if(button==null){
button = new JButton( new ImageIcon(JFreeChartImage.getImage()));
}
return button
}
}
This all works except when double clicking on the JButton and it displays TEXT ex.
javax.Swing.JButton(0,0,...)
all field values instead of the the actual chart image that should be displayed
There are several issues with the code you posted, and too long to include them all in a comment.
You should not store Swing components in the model. The creation of the components is the task for the renderer
I do not believe you can click the button which is returned by the renderer. That button is not contained in the JTable which is displayed. Only an image/screenshot/stamp of the button is drawn on-screen, but it does not behave like a button. You would need an editor for that. Consult the JTable tutorial about the editors and renderers for more information
I have a custom editor composed of several components. Something like:
class MyCellEditor extends AbstractCellEditor implements TableCellEditor {
JTextArea textArea;
JButton button;
JPanel panel;
MyCellEditor() {
textArea = new JTextArea();
button = new JButton();
panel = new JPanel(new BorderLayout());
panel.add(textArea, BorderLayout.CENTER);
panel.add(button, BorderLayout.EAST);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
textArea.setText((String) value);
return panel;
}
public Object getCellEditorValue() {
return textArea.getText();
}
}
I want the inner textArea to grab focus when editing starts. It works just fine when I click the cell, but not when I navigate the table with keyboard and start typing in this cell.
How can I fix this?
I had the same problem some time ago and took me ages to find a solution. Tried a lot with focuslistener and stuff, but nothing really seemed to work the way I wanted it to until I found this useful article by Santhosh Kumar.
Its well written and should fix your problem.