Hi,
Can somebody spend some time to correct this code.
Requirement :
I have implemented a Table with comboboxes with different values.
When the first column combo box is selets value "ONE" in the second column ComboBox the combobox with "A', "B", "C" should be displayed.
And when the first column combo box is selets value "TWO" in the second column ComboBox the combobox with "AAA", "BBB", "CCC" should be displayed.
Both the comboboxes are displayed when i have not attached the listener to the combobox.
But when i attach a listener to the first column combobox then some problem is happening. I am not able to figure it out.
To view the problem :
Unmask the codes that are masked.
And make selectedIndex = 1; And see the problem. Please correct this.
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.EventObject;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import java.awt.event.*;
class ComboString {
String str;
ComboString(String str) {
this.str = str;
}
public String toString() {
return str;
}
}
class ComboStringTwo {
String str;
ComboStringTwo(String str) {
this.str = str;
}
public String toString() {
return str;
}
}
class ColONEComboString {
String str;
ColONEComboString(String str) {
this.str = str;
}
public String toString() {
return str;
}
}
class MultiRenderer extends DefaultTableCellRenderer {
JCheckBox checkBox = new JCheckBox();
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Boolean) { // Boolean
checkBox.setSelected(((Boolean) value).booleanValue());
checkBox.setHorizontalAlignment(JLabel.CENTER);
return checkBox;
}
String str = (value == null) ? "" : value.toString();
return super.getTableCellRendererComponent(table, str, isSelected,
hasFocus, row, column);
}
}
class MultiEditor implements TableCellEditor {
private final static int COMBO = 0;
private final static int COMBOTWO = 1;
private final static int COMBOTHREE = 2;
DefaultCellEditor[] cellEditors;
JComboBox comboBox;
JComboBox comboBoxTwo;
JComboBox comboBoxThree;
int flg = -1;
public MultiEditor() {
cellEditors = new DefaultCellEditor[3];
comboBox = new JComboBox();
comboBox.addItem("A");
comboBox.addItem("B");
comboBox.addItem("C");
comboBox.addItem("D");
cellEditors[COMBO] = new DefaultCellEditor(comboBox);
comboBoxTwo = new JComboBox();
comboBoxTwo.addItem("AAA");
comboBoxTwo.addItem("BBB");
comboBoxTwo.addItem("CCC");
comboBoxTwo.addItem("DDD");
cellEditors[COMBOTWO] = new DefaultCellEditor(comboBoxTwo);
comboBoxThree = new JComboBox();
comboBoxThree.addItem("ONE");
comboBoxThree.addItem("TWO");
comboBoxThree.addItem("THREE");
comboBoxThree.addItem("FOUR");
comboBoxThree.addItem("FIVE");
cellEditors[COMBOTHREE] = new DefaultCellEditor(comboBoxThree);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (value instanceof ComboString)
{ // ComboString
flg = COMBO;
String str = (value == null) ? "" : value.toString();
System.out.println("str in Combo = "+str);
return cellEditors[COMBO].getTableCellEditorComponent(table, str,
isSelected, row, column);
}
else if(value instanceof ComboStringTwo)
{ // ComboStringTwo
flg = COMBOTWO;
String str = (value == null) ? "" : value.toString();
System.out.println("str in ComboTwo = "+str);
return cellEditors[COMBOTWO].getTableCellEditorComponent(table, str,
isSelected, row, column);
}
else if(value instanceof ColONEComboString)
{ // ColONEComboString
flg = COMBOTHREE;
String str = (value == null) ? "" : value.toString();
System.out.println("str in COMBOTHREE = " + str);
return cellEditors[COMBOTHREE].getTableCellEditorComponent(table, str,
isSelected, row, column);
}
return null;
}
public Object getCellEditorValue() {
System.out.println("flag = "+flg);
switch (flg) {
case COMBO:
String str = (String) comboBox.getSelectedItem();
return new ComboString(str);
case COMBOTWO:
String strTwo = (String) comboBoxTwo.getSelectedItem();
return new ComboStringTwo(strTwo);
case COMBOTHREE:
String strThree = (String) comboBoxThree.getSelectedItem();
return new ColONEComboString(strThree);
default:
return null;
}
}
public Component getComponent() {
return cellEditors[flg].getComponent();
}
public boolean stopCellEditing() {
return cellEditors[flg].stopCellEditing();
}
public void cancelCellEditing() {
cellEditors[flg].cancelCellEditing();
}
public boolean isCellEditable(EventObject anEvent) {
return true;//cellEditors[flg].isCellEditable(anEvent);
}
public boolean shouldSelectCell(EventObject anEvent) {
return cellEditors[flg].shouldSelectCell(anEvent);
}
public void addCellEditorListener(CellEditorListener l) {
cellEditors[flg].addCellEditorListener(l);
}
public void removeCellEditorListener(CellEditorListener l) {
cellEditors[flg].removeCellEditorListener(l);
}
public void setClickCountToStart(int n) {
cellEditors[flg].setClickCountToStart(n);
}
public int getClickCountToStart() {
return cellEditors[flg].getClickCountToStart();
}
public void setFlag(int flagVal)
{
this.flg = flagVal;
}
public JComboBox getFirstColComboBox()
{
return this.comboBoxThree;
}
public JComboBox getSecComboBox()
{
return this.comboBox;
}
}
public class MultiComponentTable extends JFrame {//implements ActionListener{
JComboBox commonComboBox;
DefaultTableModel dm ;
JTable table;
MultiRenderer mulRend = new MultiRenderer();
MultiEditor mulEdit = new MultiEditor();
public MultiComponentTable() {
super("MultiComponent Table");
dm = new DefaultTableModel() {
public boolean isCellEditable(int row, int column) {
if ((column == 0) || (column == 1)) {
return true;
}
return false;
}
};
dm.setDataVector(
new Object[][] {
{ new ColONEComboString("ONE"), new ComboString("A"), "JLabel"} },
new Object[] { "Data", "Component", "Renderer"});
table = new JTable(dm);
table.getColumn("Component").setCellRenderer(mulRend);
table.getColumn("Component").setCellEditor(mulEdit);
table.getColumn("Data").setCellRenderer(mulRend);
table.getColumn("Data").setCellEditor(mulEdit);
//mulEdit.getFirstColComboBox().addActionListener(this);
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll);
setSize(400, 160);
setVisible(true);
}
/* public void actionPerformed(ActionEvent e)
{
int selectedIndex = mulEdit.getFirstColComboBox().getSelectedIndex();
System.out.println("getselected index = " + selectedIndex);
if (selectedIndex == 0) {
dm.setDataVector(new Object[][] { {new ColONEComboString("ONE"), new ComboString("A"),
"JLabel"}
}
,
new Object[] {
"Data", "Component", "Renderer"});
table.getColumn("Data").setCellRenderer(mulRend);
table.getColumn("Data").setCellEditor(mulEdit);
table.getColumn("Component").setCellRenderer(mulRend);
table.getColumn("Component").setCellEditor(mulEdit);
}
else if (selectedIndex == 1) {
dm.setDataVector(new Object[][] { {new ColONEComboString("ONE"),
new ComboStringTwo("AAA"), "JLabel" }
}
,
new Object[] {
"Data", "Component", "Renderer"});
table.getColumn("Data").setCellRenderer(mulRend);
table.getColumn("Data").setCellEditor(mulEdit);
table.getColumn("Component").setCellRenderer(mulRend);
table.getColumn("Component").setCellEditor(mulEdit);
}
}*/
public static void main(String[] args) {
MultiComponentTable frame = new MultiComponentTable();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}