swing的JTable,某一列可以设定CellEditor为combox,但是我想每个combox的可选项都不同,怎么办?

李欣欣1981 2003-07-28 04:27:01

table.getColumnModel().getColumn(index).setCellEditor(new DefaultCellEditor(comboBox));

这句话是设定第index列的编辑形式为组合框,但是这样每个单元格的组合框的内容都是一样的,怎么样才能让她们不一样呢?
多谢!
一定结贴!
...全文
409 25 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyymmm 2004-02-28
  • 打赏
  • 举报
回复
mark
junglesong 2004-01-13
  • 打赏
  • 举报
回复
mk
李欣欣1981 2003-08-04
  • 打赏
  • 举报
回复
虽然问题没有解决,还是先结贴算了。
不过还是感谢各位的关注!
asdmonster 2003-07-30
  • 打赏
  • 举报
回复
楼主,不想打击你,你的需求没办法实现——除非你抛开api,自己干。
你可以想想啊:如果增一行,比如增一样,电脑怎么给你的cell增加一个render和editor?
只有一种情况:你的render和editor的内容根据行数有个映射关系,这种情况下倒是可以采取一点变通。但是也不能完全实现了你的想法。
duoshanx 2003-07-30
  • 打赏
  • 举报
回复
up & study ......
knight_qmh 2003-07-29
  • 打赏
  • 举报
回复
每个ComboBox都会有ComboBoxModel的,在JTable中每列只有一个CellEditor,也就只要一个ComboBox就可以了,要实现不同行显示不用列表就需要不同的ComboBoxModel来实现了
schwarzenegger 2003-07-29
  • 打赏
  • 举报
回复
我现在的某列,其中所有的单元格都是具有相同选项的combox,我希望他们能够拥有各自不同的选项。如您所说,我可以得到每个单元格的combox,但是这时候每个combox都是新的对象了啊,难道对其选项进行更改,还能反映到原来的表里吗?
多谢释疑!
----------------------------------------
你不需要得到每个单元格的combox, getTableCellEditorComponent是给jTable用的;
jTable在编辑每一列、行时都调用getTableCellEditorComponent。

你只需要在getTableCellEditorComponent里根据row ,生成不同的combox,就可以了。



knight_qmh 2003-07-29
  • 打赏
  • 举报
回复
看看API就好了,你可以参考上面的例子
李欣欣1981 2003-07-29
  • 打赏
  • 举报
回复
TO knight_qmh(辉) :
-----------------------------------------------------
要实现不同行显示不用列表就需要不同的ComboBoxModel来实现了
-----------------------------------------------------
感谢您的解答。可是我对comboxmodel的用法还不是很熟悉,您能说得再详细一点吗?
李欣欣1981 2003-07-29
  • 打赏
  • 举报
回复
TO schwarzenegger(找不到服务器):
-----------------------------------------------------
jTable是这么使用TableCellEditor的, 先new,然后调用getTableCellEditorComponent;
然后执行getTableCellEditorComponent。
-----------------------------------------------------
我现在的某列,其中所有的单元格都是具有相同选项的combox,我希望他们能够拥有各自不同的选项。如您所说,我可以得到每个单元格的combox,但是这时候每个combox都是新的对象了啊,难道对其选项进行更改,还能反映到原来的表里吗?
多谢释疑!
schwarzenegger 2003-07-28
  • 打赏
  • 举报
回复
这样取的CellEditor就是那一列的CellEditor,好像不能处理多种表现形式吧
-----------------------------------------------------------------
可以根据 cell选择状态、值、行、列作任意多种表现形式。

schwarzenegger 2003-07-28
  • 打赏
  • 举报
回复
我是要把不同的combox赋给某一列的不同单元格,而不是要得到某个单元格的celleditor。
---------------------------------------------------------------------------
getTableCellEditorComponent是jTable在需要时调用的;
jTable是这么使用TableCellEditor的, 先new,然后调用getTableCellEditorComponent;
然后执行getTableCellEditorComponent。

我上面的代码时TableCellEditor在不同情况下使用不同图标,并且与row有关;



李欣欣1981 2003-07-28
  • 打赏
  • 举报
回复
多谢各位的一直关注。
TO chwarzenegger(找不到服务器): 我是要把不同的combox赋给某一列的不同单元格,而不是要得到某个单元格的celleditor。莫非我用combox = getTableCellEditorComponent得到combox以后,直接对combox操作能够反映到表格里?

TO knight_qmh(辉): 您给的例子使用了DefaultComboBoxModel,这个又是怎么样和单元格绑定的呢?

再次感谢!
knight_qmh 2003-07-28
  • 打赏
  • 举报
回复
这样取的CellEditor就是那一列的CellEditor,好像不能处理多种表现形式吧
schwarzenegger 2003-07-28
  • 打赏
  • 举报
回复
这个方法我看到了,可是这是返回一个component用的呀,应该怎么样用才能把我生成的comboBox赋给某个单元格呢?
-----------------------------------

返回的component 就是真正要使用的cell editor or render啊。

schwarzenegger 2003-07-28
  • 打赏
  • 举报
回复

我的代码:

import javax.swing.table.TableCellRenderer;
import java.awt.Component;
import javax.swing.JTable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParsePosition;

import com.borland.dbswing.*;
import com.borland.dx.sql.dataset.*;
import oracle.jdbc.driver.OracleDriver;
import com.borland.dx.dataset.*;
import java.sql.Timestamp;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.ResourceBundle;

class IconRenderer extends JLabel
implements TableCellRenderer {
static ResourceBundle res = ResourceBundle.getBundle("event.Res");
private Icon[] icon = {new ImageIcon(event.IconRenderer.class.getResource(res.getString("openFile_gif"))),
new ImageIcon(event.IconRenderer.class.getResource(res.getString("closeFile_gif")))};
private SimpleDateFormat formatter
= new SimpleDateFormat (res.getString("yyyy_MM_dd_hh_mm_ss"));

private int columnStartDate=1;
private int columnStopDate=2;
private Timestamp priorStop=new Timestamp(0);
private Timestamp currentStart=new Timestamp(0);
private Timestamp currentStop=new Timestamp(0);
private Timestamp nextStart=new Timestamp(0);
private int i=0;
public IconRenderer() {

setHorizontalAlignment(JLabel.CENTER);
setHorizontalTextPosition(JLabel.CENTER);
setVisible(true);
}
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected,
boolean hasFocus,
int row, int col) {
i=0;
//priorStop.setTime(0);
//nextStart.setTime(Long.MAX_VALUE);
if(row>1){
priorStop = (Timestamp)table.getValueAt(row - 1,columnStopDate);
}
if(row< table.getRowCount() -1){
nextStart = (Timestamp)table.getValueAt(row + 1,columnStartDate);
}

currentStop = (Timestamp)table.getValueAt(row ,columnStopDate);
currentStart = (Timestamp)table.getValueAt(row ,columnStartDate);
if(currentStop!=null&&nextStart!=null){
if(currentStop.after(nextStart)){
i=1;
}
}
setIcon(icon[i]);
setEnabled(true);
return this;
}
private Date getDateFromString(String a,Timestamp a2){
ParsePosition pos = new ParsePosition(0);
if(a!=null){
return new Timestamp(formatter.parse(a, pos).getTime());
}
return a2;
}
}
knight_qmh 2003-07-28
  • 打赏
  • 举报
回复
一行有两个Combox,选择第一个改变第二个Combox显示列表
李欣欣1981 2003-07-28
  • 打赏
  • 举报
回复
这个方法我看到了,可是这是返回一个component用的呀,应该怎么样用才能把我生成的comboBox赋给某个单元格呢?
真的希望能够给一个例子(简短的程序片断即可),我觉得真是无从下手啊。多谢!
knight_qmh 2003-07-28
  • 打赏
  • 举报
回复
import javax.swing.plaf.metal.MetalComboBoxUI;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.JTable;

public class DeclareTaxItemComboUI extends MetalComboBoxUI
{
protected JTable jtable = null;
public DeclareTaxItemComboUI(JTable jtable)
{
this.jtable = jtable;
}
protected ComboPopup createPopup() {
DeclareItemComboPopup popup =new DeclareItemComboPopup(jtable,comboBox);
popup.getAccessibleContext().setAccessibleParent(comboBox);
return popup;
}

}
knight_qmh 2003-07-28
  • 打赏
  • 举报
回复
自己处理一下,供你参考

public class DeclareItemComboPopup extends ChangeModelComboPopup
{
protected JTable jtable = null;

public DeclareItemComboPopup(JTable jTable, JComboBox combo)
{
super(combo);
this.jtable = jTable;
}

public void show()
{
System.out.println("Show");
getListModel();
super.show();
}

public void getListModel()
{
Object taxKind = jtable.getModel().getValueAt(jtable.getSelectedRow(),0);
System.out.println("TaxKind:"+taxKind);
if(null == taxKind)
return;
this.list = changeList(taxKind);
configureList();
this.remove(scroller);
scroller = createScroller();
configureScroller();
configurePopup();

installKeyboardActions();
}

protected JList changeList(Object src)
{
DefaultComboBoxModel model = new DefaultComboBoxModel();
if(src.toString().equals("11 AA"))
{
model.addElement("22 aa1");
model.addElement("22 aa2");
model.addElement("22 aa3");
}
else if(src.toString().equals("12 BB"))
{
model.addElement("22 bb1");
model.addElement("22 bb2");
model.addElement("22 bb3");
}
else if(src.toString().equals("13 CC"))
{
model.addElement("22 cc1");
model.addElement("22 cc2");
model.addElement("22 cc3");
}
else if(src.toString().equals("14 DD"))
{
model.addElement("22 dd1");
model.addElement("22 dd2");
model.addElement("22 dd3");
}

return new JList(model)
{
public void processMouseEvent(MouseEvent e)
{
if (e.isControlDown())
{
// Fix for 4234053. Filter out the Control Key from the list.
// ie., don't allow CTRL key deselection.
e = new MouseEvent( (Component) e.getSource(), e.getID(),
e.getWhen(),
e.getModifiers() ^ InputEvent.CTRL_MASK,
e.getX(), e.getY(), e.getClickCount(),
e.isPopupTrigger());
}
super.processMouseEvent(e);
}
};

}
}
加载更多回复(5)

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧