kohinoor2007
Programmer
Hi guys,
Have made a small test application to tell my problem.
Have rendered a table with a comboBox.Everything works fine,the way I wanted it to be, apart from the following things.
[1]
When the user Doubleclicks on one of the combocells,the comboitem in the combobox is already displayed on the tablecell.I dont want it to be like that.
I want the comboitem to be displayed on the tablecell only when the user explicitly goes and selects the comboitem.If the user doublciks on the cell as said before,the old cell value should remain.
[2]
How to catch a combo close up event when the user explicitly selects an item from the combo.Because I want to do something when the user does so....
see Pic:
Iam attaching the code(running) also along with this....
Any help is greatly appreciated.
Thanks.
Have made a small test application to tell my problem.
Have rendered a table with a comboBox.Everything works fine,the way I wanted it to be, apart from the following things.
[1]
When the user Doubleclicks on one of the combocells,the comboitem in the combobox is already displayed on the tablecell.I dont want it to be like that.
I want the comboitem to be displayed on the tablecell only when the user explicitly goes and selects the comboitem.If the user doublciks on the cell as said before,the old cell value should remain.
[2]
How to catch a combo close up event when the user explicitly selects an item from the combo.Because I want to do something when the user does so....
see Pic:
Iam attaching the code(running) also along with this....
Any help is greatly appreciated.
Thanks.
Code:
package set.test;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class TableCellColour extends JPanel {
private int tableHilightedRow, tableHilightedCol;
JTable table = null;
private Vector rowData;
private Vector columnNames;
private JButton jButton = null; // @jve:decl-index=0:visual-constraint="348,164"
public TableCellColour(){
super(new GridLayout(1,0));
rowData = new Vector();
columnNames = new Vector();
this.columnNames.addElement("Entry Type");
this.columnNames.addElement("First");
this.columnNames.addElement("Last");
this.tableHilightedRow = -1;
this.tableHilightedCol = -1;
table = new JTable(new MyTableModel(rowData,columnNames));
addRows();
table.setPreferredScrollableViewportSize(new Dimension(500,70));
JScrollPane scrollpane = new JScrollPane(table);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//table.setSelectionBackground(Color.YELLOW);
table.addMouseListener(new MouseHandler());
setUpStartEndRangeColumn(table);
add(scrollPane);
add(getJButton());
}
private void addRows(){
Vector row = new Vector();
row.addElement("");
row.addElement("555");
row.addElement("566");
MyTableModel myModel = (MyTableModel) this.table.getModel();
myModel.addRow(row);
Vector row1 = new Vector();
row1.clear();
row1.addElement("");
row1.addElement("655");
row1.addElement("666");
myModel.addRow(row1);
Vector row2 = new Vector();
row2.clear();
row2.addElement("");
row2.addElement("755");
row2.addElement("766");
myModel.addRow(row2);
Vector row3 = new Vector();
row3.clear();
row3.addElement("");
row3.addElement("855");
row3.addElement("866");
myModel.addRow(row3);
}
public void setUpStartEndRangeColumn(JTable table){
// These are the combobox values
JComboBox comboBox1 = new JComboBox();
comboBox1.addItem("First");
//sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
//JComboBox comboBox = new JComboBox(values1);
MyComboBoxEditor cellEditor = new MyComboBoxEditor(comboBox1);
TableColumn startRange = table.getColumnModel().getColumn(1);
//startRange.setCellEditor(new DefaultCellEditor(comboBox));
startRange.setCellEditor(cellEditor);
MyComboBoxRenderer comboRenderer= new MyComboBoxRenderer();
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
startRange.setCellRenderer(comboRenderer);
JComboBox comboBox2 = new JComboBox();
comboBox2.addItem("Last");
TableColumn endRange = table.getColumnModel().getColumn(2);
MyComboBoxEditor cellEditor2 = new MyComboBoxEditor(comboBox2);
endRange.setCellEditor(cellEditor2);
DefaultTableCellRenderer renderer2 = new DefaultTableCellRenderer();
endRange.setCellRenderer(comboRenderer);
}
public void setTableRowColSelection() {
if (tableHilightedRow != -1)
table.setRowSelectionInterval(0, tableHilightedRow);
if (tableHilightedCol != -1)
table.setColumnSelectionInterval(0, tableHilightedCol);
}
public class MyTableModel extends DefaultTableModel{
public MyTableModel(Vector rowDataSet, Vector columnNamesSet) {
super(rowDataSet, columnNamesSet);
}
public Class getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
public boolean isCellEditable(int row,int col){
if(col<1)
return false;
else
return true;
}
public void setValueAt(Object value,int row,int col){
super.setValueAt(value, row, col);
}
}
public class MyComboBoxEditor extends DefaultCellEditor {
/**
*
*/
private static final long serialVersionUID = 1L;
public MyComboBoxEditor(JComboBox combo) {
super(combo);
setClickCountToStart(2);
}
public void setClickCountToStart(int count) {
super.setClickCountToStart(count);
}
}
public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
int highlightRow = -1;
int highlightCol = -1;
public MyComboBoxRenderer() {
super();
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (row == highlightRow && column == highlightCol) {
setBackground(Color.yellow);
}
else if (isSelected) {
// setBackground(Color.yellow);
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else
{
//setBackground(Color.yellow);
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setBorder(null);
removeAllItems();
addItem( value );
return this;
}
public void setHighlightCell(int row, int col) {
highlightRow = row;
highlightCol = col;
tableHilightedRow = highlightRow;
tableHilightedCol = highlightCol;
setTableRowColSelection();
}
}
class MouseHandler extends MouseAdapter {
public void mousePressed(MouseEvent e) {
JComboBox comp = null;
Point p = e.getPoint();
int row = table.rowAtPoint(p);
int col = table.columnAtPoint(p);
int click = e.getClickCount();
MyComboBoxRenderer render = (MyComboBoxRenderer) table.getCellRenderer(row, col);
String eVal = (String)table.getValueAt(row,col);
comp = (JComboBox) render.getTableCellRendererComponent(table,eVal,true,true,row,col);
if(click==1&& (row != -1) && (col != 0))
{
render.setHighlightCell(row, col);
}
table.repaint();
}
}
private static void createAndShowGUI(){
JFrame frame = new JFrame("TableComBoBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableCellColour newContentPane = new TableCellColour();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (this.jButton == null) {
jButton = new JButton();
jButton.setSize(new Dimension(176, 29));
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
table.getModel().setValueAt("Test",0, 1);
table.repaint();
}
});
}
return jButton;
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}