Java JTable 问题

发布于 2024-10-17 20:53:22 字数 429 浏览 4 评论 0 原文

我在互联网上搜索了一些解决我问题的方法,但我似乎还没有想出任何办法,而且我真的一直在努力解决这个问题。

简而言之,我需要创建一个嵌入 JTable 的 JDialog。该 JTable 的列数量必须是动态的。另外,我希望 JTable 的前两行的每个单元格中都有组合框。第一行都具有相同的组合框,第二行都具有相同的组合框,但与第一行不同。从那里开始,我只需为一组其他行填充静态文本数据。所以表格的形式应该是:

组合,组合,组合,组合,...
组合,组合,组合,组合,...
文本,文本,文本,文本,...

我遇到了很多麻烦a)使列号动态(这将取决于传递的数组)和b)仅制作前两行组合框 - 唯一的方法我可以在任何地方找到制作整列组合框的地方。

如果有帮助的话,我正在使用 Netbeans IDE。提前非常感谢您抽出宝贵的时间,如果需要任何进一步的知识,请告诉我。

I've scoured the internet looking for bits and pieces of a solution to my problem, but I haven't seemed to come up with anything yet, and I've really been struggling to solve this.

In short, I need to create a JDialog that embeds a JTable. This JTable has to be dynamic in regards to its amount of columns. Also, I'd like the first two rows of the JTable to have combo boxes in each of their cells. Row one all has the same combo box, and row two all has the same combo box, different from row one. From there, I'm just filling in static text data for a set number of other rows. So the table should be of the form:

combo, combo, combo, combo, ...
combo, combo, combo, combo, ...
text, text, text, text, ...

I'm having a lot of trouble a) making the column number dynamic (it will depend on an array passed) and b) making just the first two rows combo boxes - the only way I can find anywhere makes an entire column combo boxes.

If it helps at all, I'm using Netbeans IDE. Thank you very much in advance for your time, and if any further knowledge is needed, just let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏雪无痕 2024-10-24 20:53:22

如何返回返回 JComboBox 的 TableCellEditor?

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class TableComboBoxByRow extends JFrame
{
    List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);

    public TableComboBoxByRow()
    {
        // Create the editors to be used for each row

        String[] items1 = { "Red", "Blue", "Green" };
        JComboBox comboBox1 = new JComboBox( items1 );
        DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
        editors.add( dce1 );

        String[] items2 = { "Circle", "Square", "Triangle" };
        JComboBox comboBox2 = new JComboBox( items2 );
        DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 );
        editors.add( dce2 );

        String[] items3 = { "Apple", "Orange", "Banana" };
        JComboBox comboBox3 = new JComboBox( items3 );
        DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 );
        editors.add( dce3 );

        //  Create the table with default data

        Object[][] data =
        {
            {"Color", "Red"},
            {"Shape", "Square"},
            {"Fruit", "Banana"},
            {"Plain", "Text"}
        };
        String[] columnNames = {"Type","Value"};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model)
        {
            //  Determine editor to be used by row
            public TableCellEditor getCellEditor(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 1 && row < 3)
                    return editors.get(row);
                else
                    return super.getCellEditor(row, column);
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        TableComboBoxByRow frame = new TableComboBoxByRow();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
}

how can I return a TableCellEditor returning a JComboBox?

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class TableComboBoxByRow extends JFrame
{
    List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);

    public TableComboBoxByRow()
    {
        // Create the editors to be used for each row

        String[] items1 = { "Red", "Blue", "Green" };
        JComboBox comboBox1 = new JComboBox( items1 );
        DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
        editors.add( dce1 );

        String[] items2 = { "Circle", "Square", "Triangle" };
        JComboBox comboBox2 = new JComboBox( items2 );
        DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 );
        editors.add( dce2 );

        String[] items3 = { "Apple", "Orange", "Banana" };
        JComboBox comboBox3 = new JComboBox( items3 );
        DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 );
        editors.add( dce3 );

        //  Create the table with default data

        Object[][] data =
        {
            {"Color", "Red"},
            {"Shape", "Square"},
            {"Fruit", "Banana"},
            {"Plain", "Text"}
        };
        String[] columnNames = {"Type","Value"};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model)
        {
            //  Determine editor to be used by row
            public TableCellEditor getCellEditor(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 1 && row < 3)
                    return editors.get(row);
                else
                    return super.getCellEditor(row, column);
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        TableComboBoxByRow frame = new TableComboBoxByRow();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
}
妄想挽回 2024-10-24 20:53:22

对于动态列数部分,这是由 DefaultTableModel列,或者更好的是,通过扩展 AbstractTableModel,并根据包含数据的数组实现 getColumnCount()

对于组合部分,我猜您想使用组合来编辑表中的数据。您需要扩展 JTable 并重新定义 getCellEditor() 方法,以便在行为 0 或 1 时返回一个返回 JComboBox 的 TableCellEditor。并且您可以返回 super.getCellEditor(row, column) 如果该行大于 1。

您还应该阅读 有关 JTable 的 Java 教程< /a>,其中包含有关您想做的所有事情的部分。

For the dynamic number of columns part, this is defined by the TableModel of your JTable. Use a DefaultTableModel with the appropriate number of columns, or, even better, implement your own table model by extending AbstractTableModel, and implement getColumnCount() based on the array containing your data.

For the combo part, I guess you want to use a combo to edit the data in the table. You the need to extend JTable and redefine the getCellEditor() method in order to return a TableCellEditor returning a JComboBox if the row is 0 or 1. And you may return super.getCellEditor(row, column) if the row is bigger than 1.

You should also read the Java Tutorial about JTable, which has sections about everything you want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文