urgent help on editing cell of a JTable

I have a JTable and i want to add JCombobox to only one cell in the JTable not for all cell in the column, and I want to be able to write in the rest of cells and to be enable to make some cells not enable for writing.

and this is my code.. please i want your replay as soon as possible :

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Component;

import java.awt.Label;

import javax.swing.JFrame;

import javax.swing.*;

import javax.swing.table.TableCellEditor;

import javax.swing.table.TableCellRenderer;

import javax.swing.table.TableColumn;

/*

* Created on Sep 17, 2005

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

/**

* @author a

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

public class Proper extends JFrame implements TableCellRenderer {

private JPanel topPanel;

private JTable table;

private JScrollPane scrollPane;

Proper() {

setSize(300, 150);

topPanel = new JPanel();

topPanel.setLayout(new BorderLayout());

getContentPane().add(topPanel);

String columnNames[] = { "","" };

String dataValues[][] = { { "dd", "gg" }, {

"ff", "dgd" }, {

"", "" }};

table = new JTable(dataValues, columnNames);

scrollPane = new JScrollPane(table);

topPanel.add(scrollPane, BorderLayout.CENTER);

//TableColumn prmatiere = table.getColumnModel().getColumn(1);

TableColumn prmatiere = table.getColumnModel().getColumn(1);

JComboBox comboBox = new JComboBox();

comboBox.addItem("Snowboarding");

comboBox.addItem("Rowing");

prmatiere.setCellEditor(new DefaultCellEditor(comboBox));

TableColumn prmatiere1 = table.getColumnModel().getColumn(1);

getTableCellRendererComponent( table, comboBox, true,true,1,1);

JComboBox comboBox1 = new JComboBox();

comboBox1.addItem("Chasing toddlers");

comboBox1.addItem("Speed reading");

comboBox1.addItem("Teaching high school");

comboBox1.addItem("None");

//prmatiere1.setCellEditor(new DefaultCellEditor(comboBox1));

}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,

boolean hasFocus, int row, int column) {

JComboBox box = new JComboBox( new String[] {"Location", "No", "Gender"} );

box.setBackground(Color.white);

return box;

}

public static void main(String args[]) {

final Proper t = new Proper();

t.setVisible(true);

//table = new JTable(rowData, columnNames);

}

}

/*public void getCellRenderer(int row, int column) {

if(row == 0)

return t;

return super.getCellRenderer(row, column);

}*/

[3008 byte] By [birddreamsa] at [2007-10-3 5:12:41]
# 1
Hello,Would you mind if I ask what structural data you wish to show? I would like to understand why you want to have a single cell as a JComboBox and not the entire column?
Gyftusa at 2007-7-14 23:19:05 > top of Java-index,Desktop,Core GUI APIs...
# 2
because i want to make each row has it's own data typesay i have a table of two rows and tow columns in the first row the data of the second column will be booleanin the second row the the data of the second column will be text
birddreamsa at 2007-7-14 23:19:05 > top of Java-index,Desktop,Core GUI APIs...
# 3
Quit multi-posting questions. You where given the answer hours ago.
camickra at 2007-7-14 23:19:06 > top of Java-index,Desktop,Core GUI APIs...
# 4
Quit multi-posting questions. You where given the answer hours ago.??where is the answer please till me if you found it or keep your advice to your self thanx
birddreamsa at 2007-7-14 23:19:06 > top of Java-index,Desktop,Core GUI APIs...
# 5
> where is the answer please till me if you found it or keep your advice to your self Ok, I'll keep my advice to myself. Too bad, because I've already given you the answer. Learn how to use the forums and you will find the answer that you've been given.
camickra at 2007-7-14 23:19:06 > top of Java-index,Desktop,Core GUI APIs...
# 6

Try this (try to do yourself the extra work of enabling/....)

Atleast it will give you a good picture of how to implement-- what you want to do...

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Component;

import java.awt.Label;

import javax.swing.JFrame;

import javax.swing.*;

import javax.swing.table.TableCellEditor;

import javax.swing.table.TableCellRenderer;

import javax.swing.table.TableColumn;

public class Proper extends JFrame implements TableCellRenderer{

private JPanel topPanel;

private JTable table;

private JScrollPane scrollPane;

JComboBox comboBox;

Proper() {

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setSize(300, 150);

topPanel = new JPanel();

topPanel.setLayout(new BorderLayout());

getContentPane().add(topPanel);

String columnNames[] = { "","" };

String dataValues[][] = { { "dd", "gg" }, {

"ff", "dgd" }, {

"", "" }};

table = new JTable(dataValues, columnNames);

scrollPane = new JScrollPane(table);

topPanel.add(scrollPane, BorderLayout.CENTER);

TableColumn prmatiere = table.getColumnModel().getColumn(1);

comboBox = new JComboBox();

comboBox.addItem("Snowboarding");

comboBox.addItem("Rowing");

prmatiere.setCellEditor(new tbEditor());

prmatiere.setCellRenderer(this);

}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,

boolean hasFocus, int row, int column) {

if(column==1 && row==1){

JComboBox box =comboBox;

box.setBackground(Color.white);

return box;

}

return new JLabel((String)table.getValueAt(row,column));

}

public static void main(String args[]) {

final Proper t = new Proper();

t.setVisible(true);

}

class tbEditor extends AbstractCellEditor

implements TableCellEditor{

public Object getCellEditorValue() {

int r=table.getSelectedRow(),c=table.getSelectedColumn();

if(table.getSelectedColumn()==1 && table.getSelectedRow()==1) return comboBox.getSelectedItem();

JTextField s=(JTextField)table.getEditorComponent();

return s.getText();

}

public Component getTableCellEditorComponent(JTable table,

Object value,

boolean isSelected,

int row,

int column) {

if(column==1 && row==1){

JComboBox box =comboBox;

box.setBackground(Color.white);

return box;

}

return new JTextField((String)table.getValueAt(row,column));

}

}

}

Note: Actually camickr is best in this area-he has posted more solutions related to this table ......

pravintha at 2007-7-14 23:19:06 > top of Java-index,Desktop,Core GUI APIs...
# 7
thanx alot it's work very well
birddreamsa at 2007-7-14 23:19:06 > top of Java-index,Desktop,Core GUI APIs...