• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

MultiComponent JTable

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
});
}
}
 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No body is there who can solve my problem ?? :-(
Its urgent please solve this.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thanks for the reply.
But my requirement is something else.
Actually the application can have multiple rows.
The first column will have same combobox having the values : ONE, TWO, THREE...
First Row - first column have value ONE, 2nd Column will show the Combo box with value A, B, C, D

Second Row - first column have value TWO, 2nd Column will show the Combo box with value AAA, BBB, CCC, DDD

Third Row - first column have value FIVE, 2nd Column will show the Combo box with value AAAAA, BBBB, CCCCC, DDDDD
and so on.

I need to have a addrow button and a remove row button which will add/delete a row dynamically. In this case the existing rows should retain their selected values if earlier updated.
Also i have a Ok button on pressing it, collets the data from the table and sends to Backend.
This makes the application complecated. I am struggling with all these.

So can you update the code as per this requirement. As i am very new to JTable implementation i am finding it difficult to solve this. Please help me soon.
 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me to solve this problem. Its urgent.
 
If you're gonna buy things, buy this thing and I get a fat kickback:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic