473,396 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

combobox in JTable column

can anyone help me flesh out the problem?
I wanted to insert a combo box into one of the columns of
a JTable. although the combo box is drawn, the list
of the data model fails to render.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableAvecRenderer extends JPanel {

public TableAvecRenderer(){


JTable table = new JTable(new TheModel());
//place table in a scroll pane
JScrollPane pane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
table.setDefaultRenderer(JComboBox.class, new CellRenderer());
this.add(pane);
}

class CellRenderer extends JComboBox
implements TableCellRenderer {

public CellRenderer(){
setOpaque(true);
}

public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {

//let all the cells in this column
//be rendered as a drop-down list

JComboBox hobbies = (JComboBox)value;
hobbies.setEnabled(true);
return this;
}
}

class TheModel extends AbstractTableModel {

String[] sports = {"Footbals", "BasketBall", "HandBall",
"Hockey"};
JComboBox box = new JComboBox(sports);

Object[] cols = {
"FirstName", "LastName", "Sex",
"Age", "State", "educated", "hobbies"
};

Object[][] rows = {
{"David", "Odimegwu","M", "32",
"Imo", new Boolean(false), box},
{"Micheal", "Eke", "M", "34",
"Abia", new Boolean(true), box},
{"Justina", "Akunnaya", "F", "25",
"Anambra", new Boolean(true), box}
};

//implementing the abstract methods.
public Object getValueAt(int row, int col){
return rows[row][col];
}

public int getColumnCount(){
return cols.length;
}

public int getRowCount(){
return rows.length;
}

//implment this class for renderer
@Override
public Class<?> getColumnClass(int col){
return getValueAt(0,col).getClass();
}

@Override
public String getColumnName(int col){
return (String)cols[col];
}
}
private static void showGUI(){
JFrame frame = new JFrame("Simple Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

TableAvecRenderer contentPane = new TableAvecRenderer();
frame.setContentPane(contentPane);

frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
showGUI();
}
});
}

}

thanx
Aug 4 '08 #1
1 5792
JosAH
11,448 Expert 8TB
Look at your CellRenderer: it is a JComboBox (you extend from it) but you never
populate it with any data. Also your TableModel returns the wrong class, it doesn't
give the JTable any indication that it should use a JComboBox for its renderer
and editor.

kind regards,

Jos
Aug 4 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Alan Hoffman | last post by:
Hi, Is it possible to create a JTable where the column is not all one type of Component. For example using a JTable to enter in values for an address where the first column is just JLabels and...
1
by: Ben Munday | last post by:
Hi all, I have a problem with a Checkbox in a JTable. When i add the Checkbox into the JTable it displays the String representation of the Checkbox, which is not what i want. So, i created a...
1
by: asd | last post by:
I need to make the cells in the 1st column look like the column header. I tried the following code but it didn't change anything: private void rendererTest() { TableColumn column =...
1
by: raysaun | last post by:
I am trying to set a TableCellEditor for a JTable, however, the editor never seems to get called. I can not even get a simplified version (below) to work. When I put a breakpoint on the return...
1
by: Andrea Sansottera | last post by:
Hi fellows, I was wondering if in the Java API there's a way to easily map a disconnected RowSet to a JTable... I have looked at the doc but i did not found anything. What I want is simply do...
7
blazedaces
by: blazedaces | last post by:
Hi guys, what I'm I have is a jtable with boolean variables in the first column and text in the next two (checkboxes). I want my tableChanged method to make it so when I check one of the checkboxes...
0
by: san1014 | last post by:
Hi to all' I am using Java swings. I written a code to display a table. In the table i used Boolean.class type column. when i click the checkbox it value to be change from false to true....
5
by: moizpalitanawala | last post by:
Hello friends, How to add data to JTable from a .txt file. I had seen this code from one of the tutorial from java.com. This code doesnt take any input. But shows what is written in the array....
1
by: Man4ish | last post by:
Hi, How Eventlistner can be used with rendred combo box. I got one example of combobox in table as follows . /* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved. * ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.