Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Have Programming Question? Ask it here!

k murali
jtable combo
2 Answer(s)      4 years and 4 months ago
Posted in : Java Beginners

i am using jtable (using defaulttablemodel) ,when i am click on a particular cell of jtable i want to display the combo box in that cell,plz provide program

View Answers

April 24, 2010 at 1:19 PM

Hi Friend,

Try the following code:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.plaf.basic.*;

public class ComboBoxInTableCell {
public static void main(String[] args) {
JFrame f=new JFrame();
f.setLayout(new BorderLayout());
String data[][] = {{"",""},{"",""},{"",""},{"",""}};
String col[] = {"",""};
DefaultTableModel model = new DefaultTableModel(data,col);
JTable table = new JTable(model);
TableColumnModel tcm = table.getColumnModel();
for(int i=0;i<2;i++){
TableColumn tc= tcm.getColumn(i);
tc.setCellEditor(new ComboEditor());
JScrollPane jsp = new JScrollPane(table);
f.add(jsp, BorderLayout.CENTER);
f.setSize(400, 300);
class ComboUI extends BasicComboBoxUI {
public JList getList() {
return listBox;
class ComboEditor extends JComboBox implements TableCellEditor, CaretListener {
protected EventListenerList listenerList = new EventListenerList();
protected ChangeEvent changeEvent = new ChangeEvent(this);
private ComboUI comboUi = new ComboUI();
private JTextField tf;
Object newValue;
public ComboEditor(){
tf = (JTextField) getEditor().getEditorComponent();
tf.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == e.VK_ENTER) {
Object value = comboUi.getList().getSelectedValue();
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
public void addCellEditorListener(CellEditorListener listener) {
listenerList.add(CellEditorListener.class, listener);
public void removeCellEditorListener(CellEditorListener listener) {
listenerList.remove(CellEditorListener.class, listener);
public void cancelCellEditing() {
public boolean stopCellEditing() {
return true;
public boolean isCellEditable(EventObject event) {
return true;
public boolean shouldSelectCell(EventObject event) {
return true;
public Object getCellEditorValue() {
return newValue;
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int row, int column) {
return this;
public void caretUpdate(CaretEvent e) {
if (!isPopupVisible() && tf.isShowing() && tf.hasFocus())
JTextField tf = (JTextField) e.getSource();
String text = tf.getText().toLowerCase();
int index = -1;
for (int i = 0; i < super.getItemCount(); i++) {
String item = ((String) getItemAt(i)).toLowerCase();
if (item.startsWith(text)) {
index = i;
if (index != -1)
newValue = comboUi.getList().getSelectedValue();


November 12, 2011 at 6:27 PM

when i select an option in combo box for e.g. "city" & write city name in a text field for e.g. "Delhi" & click on button "show details", then i want a JTable to be displayed showing all the data from database having "city" as "Delhi".

later when i change option in combo box for e.g. "name" & write name in text field for e.g. "xyz" & click on button "show details", then i want the same previous(showing "city") JTable to now show all the data from database with "name" as "xyz". so the data should be updated in JTable.

can you plz help me?

Related Tutorials/Questions & Answers:
jtable combo - Java Beginners
jtable combo  i am using jtable (using defaulttablemodel) ,when i am click on a particular cell of jtable i want to display the combo box...); JTable table = new JTable(model); TableColumnModel tcm = table.getColumnModel
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
JTable   how to select a definite cell which containing a similar text containg to the one which the user entering from a jtable at runtime in java
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
JTable  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
jtable  how to get the values from database into jtable and also add a checkbox into it and then when selected the checkbox it should again insert into database the selected chewckbox.plzz help
jtable  hi Sir i am working netbeans IDE,I have a jtable when i insert values in jtable then i am unable to print all inserted values,For eg if i insert 1,2,3,4,5,6,7,8 values then , i am getting output
jtable  hey i have build a form and i m also able to add data from database to jtable along with checkbox.the only problem is that if i select multiple checkboxes the data doesnt get inserted into new database and if only one
JTable  Hi I have problems in setting values to a cell in Jtable which is in a jFrame which implements TableModelListener which has a abstract method tableChanged(TableModelEvent e) . I'll be loading values from data base when
"}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane
combo box
combo box  Hi, [_|] dropdown box [ ] [INCLUDE] [ ] | | [INCLUDE All
combo boxes
combo boxes  how do you store numbers in the choices. ex. soda is $.75 and tax is $.07 then it equals a new total. I've been trying to use the Itemlistener but i can't figure out how to store the numbers import javax.swing.
populate Combo Box dynamically
populate Combo Box dynamically  Hi, How to populate the Combo Box dynamically using Flex with Jsp's
combo box - JSP-Servlet
combo box  how to get a combo box in jsp page which allows editing as well as list box
jtable problem
jtable problem  how to make a cell text hypertext
JTABLE OF JAVA  i have a jtable in java,i have used checkbox in jtable. now i want to add(submit) only those records that i have checked by checkbox how? i want small example with coding
Jtable-Java  Hi all,I have a Jtable And i need to clear the data in the table .I only Need to remove the data in the table.not the rows.Please help me
Swings JTable
Swings JTable  add values to JTable with four coloums,two of them are comboboxes
sum in JTable
sum in JTable  how to calculate sum from JTable's one field like total
JTABLE Issue  Hi Eveyone, I am developing a small application on Swing-AWT. I have used JTABLE to show data. There is "input field" and "search... on basis of input data provided in input field. For JTABLE is on some other
assigning arraylist to combo box
assigning arraylist to combo box  Hi I am busy coding the Airline Reservation program, i have an arraylist of locations which i want to assing to the combo box. please help
date in combo box
date in combo box  strong texthello, how can i desplay the current year and next year in combo box???!!! it must be uppdate atumaticaly every year>>>
java combo box
java combo box  how to display messagedialogbox when the combobox is null, Thanks in advance
java jtable
java jtable  Hello Sir, I am developing a desktop application in which i have to display database records in jtable .now I want to read only... that in jtable. plz help me with the code
jtable query
jtable query  I need a syntax...where i could fetch the whole data from the database once i click the cell in jtable...and that must be displayed in the nearby text field which i have set in the same frame
regarding jtable...
regarding jtable...  sir, im working with jtables. i wanted to populate a jtable from the database and when i click any row it should add a container... a container on the jtable. kindly help me sir. thanks in advance regards, rajahari
ABOUT Jtable
ABOUT Jtable  My Project is Exsice Management in java swing Desktop Application. I M Use Netbeans & Mysql . How can retrive Data in Jtable from Mysql Database in Net Beans
Disable of Combo - JSP-Servlet
am giving grading facility to the user. Once user selected his grade in combo , the combo should be disabled auotmatically. It should not get enabled at any... combo once grade selected.... pleas help me regarding this aspect.... Thanks
Combo Box - Struts
Combo Box  I hava acombo box cnnected to my database via html collections. When selecting an option, i want to display specific parts (sectons) on my web trying to use logic:present and logic:empty but its not working
JTable duplicate values in row
JTable duplicate values in row  JTable duplicate values in row
How to add JTable in JPanel
How to add JTable in JPanel  How to add JTable in JPanel
jtable insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
(Exception e){} JTable table = new JTable(data, columnNames); JScrollPane scrollPane
combo box connection
combo box connection  how to provide connection between three combo boxes,if my 1st combo box is course and 2nd combo box is semester and 3rd combo... combo boxes.   Here is Java swing code: import java.sql.*; import
JTable - JDBC
JTable   Hello..... I have Jtable with four rows and columns and i have also entered some records in MsSql database. i want to increase Jtable's... { JFrame f; JPanel p; JLabel l; JTextField tf; JButton btn; JTable tb
combo box - JSP-Servlet
combo box  I am using a JSP form in which a combobox is given and I want to populate data in it from database and on the basis of yhe selected item I want to display related data in a textbox  Hi Friend, Create
Helps with combo box and frame
Helps with combo box and frame  Hi i have a combobox and when i click... on the second window and click confirm i would have the information on the combo box. i... combo; JTextField txtBox; private String PersonViewing; private String
populating the combo box - Struts
populating the combo box  Hi I am having problem populating the combo box with a table in MS SQL Server 2005 Management Studio Express. Belo is the extract from the Struct?jsp application. Sector
combo program - Java Beginners
combo program  I want to give DOB in combo box for that i have to give 1 to 30 for days and 1900 to 2010. Without giving all, using for loop i have to get the values. Please help me..   Hi Friend, Please visit
combo box value
combo box value   i want to populate one combo box value based on another combo box value using ajax   1)country.jsp: <%@page import="java.sql.*"%> <html> <head>
combo box value
combo box value   i want to populate one combo box value based on another combo box value using ajax   1)country.jsp: <%@page import="java.sql.*"%> <html> <head> <script language
Problem With Combo Box Editable Property
Problem With Combo Box Editable Property  Hi I am new To Flex i had set Combo Box Editable Property as true. But I didn't get the property filtering in the combo box like when you enter text "S" in the combo box then the names
Loading combo box from oracle
Loading combo box from oracle  how can i load values into a combobox from oracle database when a value is selected in another combo box
JTable hold different components
JTable hold different components  create Jtable with two coloums,one coloumn is combobox and other normal
JTable - Swing AWT
JTable row selection event   Hi, it will be great if someone can share an example of row selection event in JTable
JTable in java - Java Beginners
JTable in java  Sir , I have created an application with a JTable showing the records of an MS Access database table. On the same frame I have... given one , JTable table; ....... .... table.print(); Here the error
combo box code problem
combo box code problem  in this my problem related to : when i select state MP then i wil open the its corresponding city but in database it only stores the option value no like MP at option value 10 then it will stores the 10
restrict jtable editing
restrict jtable editing  How to restrict jtable from editing or JTable disable editing?   public class MyTableModel extends...){ return false; } }   Disabling User Edits in a JTable Component