Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在编辑开始时,将JCombobox中的选定索引设置为jtable中的单元格编辑器_Java_Swing_Jtable_Jcombobox_Tablecelleditor - Fatal编程技术网

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

Java 在编辑开始时,将JCombobox中的选定索引设置为jtable中的单元格编辑器,java,swing,jtable,jcombobox,tablecelleditor,Java,Swing,Jtable,Jcombobox,Tablecelleditor,我是Java编程的初学者。我想在JTable中将JComboBox用作单元格编辑器。但是我想将JCombobox的所选索引设置为单击以进行编辑时单元格的当前值。我怎么能做到 我用于单元格和Jcombobox项的类: public class PersianLetterIDPair { private int id; private String letter; public PersianLetterIDPair(int id, String

我是Java编程的初学者。我想在JTable中将JComboBox用作单元格编辑器。但是我想将JCombobox的所选索引设置为单击以进行编辑时单元格的当前值。我怎么能做到

我用于单元格和Jcombobox项的类:

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));
}

我填写Jcombobox的方式:

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));
我根据@Polet所说的提示更改了getValueAt方法,以返回PersianlettridPair:

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();
    }

}
然后我创建一个TableCellRenderer:

class PersianLetterIDPairRenderer extends DefaultTableCellRenderer
{

}

然后我将以下内容添加到我的Jtable中:

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());
查看此链接

看看这个小例子:

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();
    }
}

根据建议,将
DefaultCellEditor
JComboBox
一起使用

小型工作示例演示了这一点:

import java.awt.BorderLayout;
import java.util.Random;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestTable2 {

    protected void initUI() {
        DefaultTableModel model = new DefaultTableModel();
        for (int i = 0; i < 3; i++) {
            model.addColumn("Col-" + (i + 1));
        }
        Random random = new Random();
        for (int i = 0; i < 2000; i++) {
            Vector<Object> row = new Vector<Object>();
            for (int j = 0; j < 3; j++) {
                row.add(VALUES[random.nextInt(VALUES.length)]);
            }
            model.addRow(row);
        }
        JTable table = new JTable(model);
        DefaultCellEditor editor = new DefaultCellEditor(new JComboBox(VALUES));
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellEditor(editor);
        }
        JFrame frame = new JFrame(TestTable2.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane scrollpane = new JScrollPane(table);
        frame.add(scrollpane, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTable2().initUI();
            }
        });
    }

    private static final String[] VALUES = { "Sun", "Hello", "Sky", "Cloud", "World", "Time", "System", "Computer", "Dummy", "Simple",
            "Today", "Lorem" };

}
导入java.awt.BorderLayout;
导入java.util.Random;
导入java.util.Vector;
导入javax.swing.DefaultCellEditor;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
导入javax.swing.table.DefaultTableModel;
公共类测试表2{
受保护的void initUI(){
DefaultTableModel=新的DefaultTableModel();
对于(int i=0;i<3;i++){
model.addColumn(“Col-”+(i+1));
}
随机=新随机();
对于(int i=0;i<2000;i++){
向量行=新向量();
对于(int j=0;j<3;j++){
行.add(值[random.nextInt(VALUES.length)]);
}
model.addRow(row);
}
JTable table=新的JTable(模型);
DefaultCellEditor=新的DefaultCellEditor(新的JComboBox(值));
对于(int i=0;i
您可以在getTableCellEditorComponent方法中初始化该值,使用a可以为您进行初始化。该值来自XxxTableModel,您还应该覆盖
等于
,以便在id和字母本身等于
时返回true。类似于
public boolean equals(Object o){if(o PersianLetterIDPair的实例){PersianLetterIDPair p=(PersianLetterIDPair)o;return p.id==id&&p.letter.equals(letter);}return false;}
请提供一个真实的答案,这将是可读的,而不仅仅是一个链接。@JBNizet谢谢,我编辑了答案并发布了一个例子。谢谢你的回复。我也这么做了,但在我的程序中不起作用。我应该提到的是,我的代码中存在差异。我的JComboBox模型不是一个字符串数组,因为我希望JComboBox中的每个项都有一个隐藏值,我创建了一个类,该类保留and id和des,然后用这个类的对象填充我的JComboBox。另一方面,我的单元格的类与我的tablemodel中提到的类相同。但每次jcombobox以单元格编辑器的形式打开时,其所选索引都与单击的单元格的当前值不同。感谢您的回复。我也这么做了,但在我的程序中不起作用。我应该提到的是,我的代码中存在差异。我的JComboBox模型不是一个字符串数组,因为我希望JComboBox中的每个项都有一个隐藏值,我创建了一个类,该类保留and id和des,然后用这个类的对象填充我的JComboBox。另一方面,我的单元格的类与我的tablemodel中提到的类相同。但每次jcombobox以单元格编辑器的形式打开时,它所选的索引与单击的单元格的当前值都不相同。@Leila你这样做很好。唯一要确保的是,当两项相同时,
equals
方法将返回true。如果同一对象有不同的实例,则可能必须重写专用类中的
equals
,以便在适当的时候返回
true
。调用equals方法时会发生什么情况?我添加了它,但JComboBox的行为没有区别,当它作为celleditor在单元格中打开时,它所选的索引位于前一个JComboBox中所选的索引上,该JComboBox在此之前打开和关闭,可能在其他类似的单元格中。我能告诉你到底发生了什么吗?描述更多:一个JCombobox在一个单元格中打开,用户选择其中一项,如“A”。然后用户单击另一个类型相似的单元格来编辑该单元格,所选单元格的值例如为“C”。但是,当JComboBox在此单元格中打开时,其所选值是在上一个刚刚关闭的JComboBox中选择的“A”,而不是应该选择的“C”。我的描述清楚吗?如果没有,请告诉我。谢谢正如你所说,我添加了equals方法,但没有什么不同。@Leila那么这意味着第一列的内容o
import java.awt.BorderLayout;
import java.util.Random;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestTable2 {

    protected void initUI() {
        DefaultTableModel model = new DefaultTableModel();
        for (int i = 0; i < 3; i++) {
            model.addColumn("Col-" + (i + 1));
        }
        Random random = new Random();
        for (int i = 0; i < 2000; i++) {
            Vector<Object> row = new Vector<Object>();
            for (int j = 0; j < 3; j++) {
                row.add(VALUES[random.nextInt(VALUES.length)]);
            }
            model.addRow(row);
        }
        JTable table = new JTable(model);
        DefaultCellEditor editor = new DefaultCellEditor(new JComboBox(VALUES));
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellEditor(editor);
        }
        JFrame frame = new JFrame(TestTable2.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane scrollpane = new JScrollPane(table);
        frame.add(scrollpane, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTable2().initUI();
            }
        });
    }

    private static final String[] VALUES = { "Sun", "Hello", "Sky", "Cloud", "World", "Time", "System", "Computer", "Dummy", "Simple",
            "Today", "Lorem" };

}