当前位置: 首页 > 知识库问答 >
问题:

在编辑开始时将JCombobox中的选定索引设置为jtable中的单元格编辑器

徐晔
2023-03-14
public class PersianLetterIDPair {              
    private int id;
    private String letter;

    public PersianLetterIDPair(int id, String description)
    {
        this.id = id;
        this.letter = description;
    }

    public int getID()
    {
        return id;
    }

    public String getLetter()
    {
        return letter;
    }


    public void setID(int inID)
    {
        id = inID;
    }

    public void setLetter(String inLetter)
    {
        letter = inLetter;
    }

    public String toString()
    {
        return letter;
    }    
Vector<PersianLetterIDPair> model2 = new Vector<PersianLetterIDPair>();


                        model1 = myDBLayer
                                .getVectorPersianLettersIDContent();
                        int tPLID;
                        String tPL;

                        Iterator iterator = model1.iterator();
                        while (iterator.hasNext()) {

                            Vector newRow = new Vector();
                            newRow = (Vector) iterator.next();
                            tPLID = (Integer) newRow.get(0);
                            tPL = (String) newRow.get(1);

                            model2.addElement(new PersianLetterIDPair(tPLID,
                                    tPL));
                        }

                        PersianLetters0001 = new JComboBox(model2);
                        jTable2.getColumnModel().getColumn(0).setCellEditor(new  DefaultCellEditor(PersianLetters0001));
public class FormSimilaritiesTableModel  extends AbstractTableModel {

private DBLayer myDBLayer = new DBLayer();
private ResultSet rs;
private String colNames[];
private Vector<FormSimilarityRow> allRows;
private FormSimilarityRow row;
private Vector<FormSimilarityRow> newRow;

private int li_cols;

private String tableName;
private ResultSetMetaData myM;

private Vector<FormSimilarityRow> deletedKeys;
private Vector newRows;
private Vector updatedRows;

private boolean ibRowInserted = false;
private guiBaseTables myGuiBaseTables ;
boolean ibRowNew = false;

FormSimilaritiesTableModel() {

    deletedKeys = new Vector();
    newRows = new Vector();
    updatedRows = new Vector();
    try {
        ResultSet rs = myDBLayer.getrsFormSimilarities();
        li_cols = 3 ;
        colNames = new String[li_cols];
        colNames[0] = "Persian Letter 1";
        colNames[1] = "Persian Letter 2";
        colNames[2] = "Form Similarity";            
        allRows = new Vector<FormSimilarityRow>();
        int rPLID1;
        String rPL1;
        int rPLID2;
        String rPL2;
        while (rs.next()) {
            ///newRow = new Vector();
            rPLID1 = (Integer) rs.getObject(1);
            rPL1 = (String) rs.getObject(2);

            rPLID2 = (Integer) rs.getObject(3);
            rPL2 = (String) rs.getObject(4);


            allRows.addElement(new FormSimilarityRow(new PersianLetterIDPair(rPLID1, rPL1),new PersianLetterIDPair(rPLID2, rPL2), (Integer)rs.getObject(5)));

        } // while

    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }

}

public Class getColumnClass(int col) {


        if (col == 0 || col == 1) {
            return PersianLetterIDPair.class;
        } else if (col == 2) {
            return Integer.class;
        } else {
            return null;
        }
}

public String getColumnName(int col) {      
    return colNames[col];
}

public int getColumnCount() {
    return li_cols;
}

public int getRowCount() {
    return allRows.size();
}

public Object getValueAt(int arow, int col) {
    row =  allRows.elementAt(arow);
    if(col == 0){           
        PersianLetterIDPair pL1 = row.getPL1();
        return pL1.getLetter();
    }
    else
    if(col == 1){           
        PersianLetterIDPair pL2 = row.getPL2();
        return pL2.getLetter();
    }
    else{
        return row.getFS();
    }

}

public boolean isCellEditable(int row, int col) {
    int totalNewRows;                   
    if (col == 0 || col == 1) {
        totalNewRows = newRows.size();
        for(int j = 0; j< totalNewRows; j++)
        {
            int rVal = ((Integer) newRows.get(j)).intValue();               
            if(  rVal == row + 1){
                return true;
            }                               
        }
        return false;


    } else {
        return true;
    }
}

public void setValueAt(Object aValue, int aRow, int aCol) {     

    FormSimilarityRow dataRow =  allRows.elementAt(aRow);
    if (aCol == 0){     
        PersianLetterIDPair sV = (PersianLetterIDPair) aValue;
        dataRow.setPL1(sV);
    }
    else 
    if (aCol == 1){     
        PersianLetterIDPair sV = (PersianLetterIDPair) aValue;
        dataRow.setPL2(sV);

    }
    else        
        dataRow.setFS((Integer) aValue);
    fireTableCellUpdated(aRow, aCol);
}
...

}

Jtable的定义如下:

myFormSimilaritiesTableModel = new FormSimilaritiesTableModel();
                        jTable2 = new JTable();
                        jScrollPane2.setViewportView(jTable2);
                        jTable2.setModel(myFormSimilaritiesTableModel);
                        jTable2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
                        jTable2.getTableHeader()
                                .setReorderingAllowed(false);

按照@polet所说的提示,我更改了getValueAt方法,以返回PersianLetterIdPair:

public Object getValueAt(int arow, int col) {
    row =  allRows.elementAt(arow);
    if(col == 0){           
        PersianLetterIDPair pL1 = row.getPL1();
        //return pL1.getLetter();
        return pL1;
    }
    else
    if(col == 1){           
        PersianLetterIDPair pL2 = row.getPL2();
        //return pL2.getLetter();
        return pL2;
    }
    else{
        return row.getFS();
    }

}
class PersianLetterIDPairRenderer extends DefaultTableCellRenderer
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);       
        setText(((PersianLetterIDPair)value).getLetter());              
        return this;
    }   
TableColumn tCol = jTable2.getColumnModel().getColumn(0);
                        tCol.setCellRenderer(new PersianLetterIDPairRenderer());
                        tCol = jTable2.getColumnModel().getColumn(1);
                        tCol.setCellRenderer(new PersianLetterIDPairRenderer());

但当我断开与TableCellRenderer的连接时,它会正确为:

TableColumn tCol = jTable2.getColumnModel().getColumn(0);
                        //tCol.setCellRenderer(new PersianLetterIDPairRenderer());
                        tCol = jTable2.getColumnModel().getColumn(1);
                        //tCol.setCellRenderer(new PersianLetterIDPairRenderer());

共有1个答案

刘兴修
2023-03-14

签出此链接单击此处

看看这个小例子:

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

public class TableTest extends JFrame {

    JTable table ;
    public TableTest(){
        table = new JTable(4,2);
        String []subject = {"Java Programming","Hoshang","Mathematics","Database"};
        String []lecturer={"Nuhara","Jon","Saran","Mikey"};
        table.setSize(200, 200);
        JComboBox subj = new JComboBox(subject);
        JComboBox lec = new JComboBox(lecturer);

        table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(lec));
        table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(subj));
        /*
         * OR you can use TableColumn 
         *  
         */
//        TableColumn tc = table.getColumnModel().getColumn(0);
//        tc.setCellEditor(new DefaultCellEditor(subj));


        this.add(new JScrollPane(table),BorderLayout.CENTER);
        this.setVisible(true);
        this.setSize(300,200);
        this.setDefaultCloseOperation(3);
    }
    public static void main(String...ag){
        new TableTest();
    }
}
 类似资料:
  • 我想要一个有4列的jtable。一列必须是组合框。其他列是字符串。 只要找到问题:在注释语句jcb.seteditable(true)时;,如果我在comboxcell上单击一次,它就会打开这个单元格。但我不知道为什么效果更好。此外,我希望combox可编辑。 我怎么能对其他细胞有同样的行为。 再次您好,我已经更新了代码,以便使-如果我通过重写方法在单元格上单击一次,单元格可以编辑-如果我通过重写

  • 问题内容: 我需要在jTable中显示数字,精确到小数点后两位。为此,我创建了一个自定义单元格编辑器,如下所示: 该单元格编辑器非常适合将点用作小数点的英语语言环境。但是在德语语言环境中,它不接受逗号作为小数点的值。请让我知道我的代码中有问题的地方。提前致谢。 编辑:这是我如何使其工作: 问题答案: 使用语言环境来发挥您的优势:

  • 问题内容: 我想让可编辑JTables中的编辑器在开始编辑时选择单元格中的所有文本。我已经尝试了几件事,都是围绕从TableCellEditor.getTableCellEditorComponent方法返回的组件上调用JTextComponent.selectAll()进行的。我尝试过的所有方法均无济于事。 在最近的尝试中,我从Swing教程更改了SimpleTableDemo类,以使用自定义T

  • 设计定制JTable我已经知道DefaultCellEditor在其构造函数中允许使用JComboBox。单击此JComboBox以显示列表项时,它显示在JTable的其他单元格上方。问题是,我需要一个更复杂的行为,就像JComboBox提供的那样,这样我就用一个JList和一个JButton实现了一个JTextField,当点击JButton(或者用户在JTextField中输入文本)时,JLi