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

导入Arraylist到JTable

章烨烨
2023-03-14

我有一个ArrayString,其中包含一些我想要打印在GUI上的html" target="_blank">数据,我开始使用JPanel之类的,最后使用Eclipse上的windowbuilder来帮助我修改可视化方面,我认为我的代码很糟糕(或者至少看起来很糟糕)我发现了一些与我的问题类似的线程,但我并不真正理解:

我可以不使用Jtable来代替看起来像一堆废话的东西吗?我只是无法理解JTable,我希望它看起来像我的代码输出,但我不明白表如何读取arraystring和像我的表一样的“输出它”,我也不知道如何实现组合框...任何帮助都很有用,谢谢!

编辑:我的第一个想法是每个“JTextField”将“导入”每一个数据行,但我认为这是非常低效的,而且可能容易出现编码错误,而我可以让我的生活更简单?

下面是呈现代码的图像:http://i57.tinypic.com/34j7mtd.jpg

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;


public class InfoVehiculos extends JFrame {
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    private JTextField textField_6;
    private JTextField textField_7;
    private JTextField textField_8;
    private JTextField textField_9;
    private JTextField textField_10;
    private JTextField textField_11;
    private JTextField textField_12;
    private JTextField textField_13;
    private JTextField textField_14;
    private JTextField textField_15;
    private JTextField textField_16;
    private JTextField textField_17;
    private JTextField textField_18;
    private JTextField textField_19;
    private JTextField textField_20;
    private JTextField textField_21;
    private JTextField textField_22;
    private JTextField textField_23;
    private JTextField textField_24;
    private JTextField textField_25;
    private JTextField textField_26;
    private JTextField textField_27;
    private JTextField textField_28;
    private JTextField textField_29;
    private JTextField textField_30;
    private JTextField textField_31;
    private JTextField textField_32;
    private JTextField textField_33;
    private JTextField textField_34;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    InfoVehiculos frame = new InfoVehiculos();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public InfoVehiculos() 
    {
        setTitle("Informacion");
        setBounds(100, 100, 495, 309);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        JLabel lblTipo = new JLabel("Tipo");
        lblTipo.setBounds(39, 24, 46, 14);
        getContentPane().add(lblTipo);

        JLabel lblMarca = new JLabel("Marca");
        lblMarca.setBounds(95, 24, 46, 14);
        getContentPane().add(lblMarca);

        JLabel lblModelo = new JLabel("Modelo");
        lblModelo.setBounds(151, 24, 46, 14);
        getContentPane().add(lblModelo);

        JLabel lblMatrcula = new JLabel("Matricula");
        lblMatrcula.setBounds(207, 24, 46, 14);
        getContentPane().add(lblMatrcula);

        JLabel lblItv = new JLabel("ITV");
        lblItv.setBounds(281, 24, 46, 14);
        getContentPane().add(lblItv);

        JLabel lblParking = new JLabel("Parking");
        lblParking.setBounds(337, 24, 46, 14);
        getContentPane().add(lblParking);

        JLabel lblEstado = new JLabel("Estado");
        lblEstado.setBounds(410, 24, 46, 14);
        getContentPane().add(lblEstado);

        //Cada combo box se puede desplegar para A/B/C
        JComboBox ComboTipo = new JComboBox();
        ComboTipo.setBounds(25, 49, 35, 20);
        ComboTipo.addItem("A");
        ComboTipo.addItem("B");
        ComboTipo.addItem("C");
        getContentPane().add(ComboTipo);

        JComboBox ComboTipo1 = new JComboBox();
        ComboTipo1.setBounds(25, 80, 35, 20);
        ComboTipo1.addItem("A");
        ComboTipo1.addItem("B");
        ComboTipo1.addItem("C");
        getContentPane().add(ComboTipo1);

        JComboBox ComboTipo2 = new JComboBox();
        ComboTipo2.setBounds(25, 111, 35, 20);
        ComboTipo2.addItem("A");
        ComboTipo2.addItem("B");
        ComboTipo2.addItem("C");
        getContentPane().add(ComboTipo2);

        JComboBox ComboTipo3 = new JComboBox();
        ComboTipo3.setBounds(25, 142, 35, 20);
        ComboTipo3.addItem("A");
        ComboTipo3.addItem("B");
        ComboTipo3.addItem("C");
        getContentPane().add(ComboTipo3);

        JComboBox ComboTipo4 = new JComboBox();
        ComboTipo4.setBounds(25, 173, 35, 20);
        ComboTipo4.addItem("A");
        ComboTipo4.addItem("B");
        ComboTipo4.addItem("C");
        getContentPane().add(ComboTipo4);

        JComboBox ComboTipo5 = new JComboBox();
        ComboTipo5.setBounds(25, 204, 35, 20);
        ComboTipo5.addItem("A");
        ComboTipo5.addItem("B");
        ComboTipo5.addItem("C");
        getContentPane().add(ComboTipo5);

        JComboBox ComboTipo6 = new JComboBox();
        ComboTipo6.setBounds(25, 235, 35, 20);
        ComboTipo6.addItem("A");
        ComboTipo6.addItem("B");
        ComboTipo6.addItem("C");
        getContentPane().add(ComboTipo6);

        //Cada combobox tiene activo o inactivo
        JComboBox ComboEstado = new JComboBox();
        ComboEstado.setBounds(408, 49, 50, 20);
        ComboEstado.addItem("Activo");
        ComboEstado.addItem("Inactivo");
        getContentPane().add(ComboEstado);

        JComboBox ComboEstado1 = new JComboBox();
        ComboEstado1.setBounds(408, 80, 50, 20);
        ComboEstado1.addItem("Activo");
        ComboEstado1.addItem("Inactivo");
        getContentPane().add(ComboEstado1);

        JComboBox ComboEstado2 = new JComboBox();
        ComboEstado2.setBounds(408, 111, 50, 20);
        ComboEstado2.addItem("Activo");
        ComboEstado2.addItem("Inactivo");
        getContentPane().add(ComboEstado2);

        JComboBox ComboEstado3 = new JComboBox();
        ComboEstado3.setBounds(408, 142, 50, 20);
        ComboEstado3.addItem("Activo");
        ComboEstado3.addItem("Inactivo");
        getContentPane().add(ComboEstado3);

        JComboBox ComboEstado4 = new JComboBox();
        ComboEstado4.setBounds(408, 173, 50, 20);
        ComboEstado4.addItem("Activo");
        ComboEstado4.addItem("Inactivo");
        getContentPane().add(ComboEstado4);

        JComboBox ComboEstado5 = new JComboBox();
        ComboEstado5.setBounds(408, 204, 50, 20);
        ComboEstado5.addItem("Activo");
        ComboEstado5.addItem("Inactivo");
        getContentPane().add(ComboEstado5);

        JComboBox ComboEstado6 = new JComboBox();
        ComboEstado6.setBounds(408, 235, 50, 20);
        ComboEstado6.addItem("Activo");
        ComboEstado6.addItem("Inactivo");
        getContentPane().add(ComboEstado6);

        textField = new JTextField();
        textField.setBounds(70, 49, 71, 20);
        getContentPane().add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(70, 80, 71, 20);
        getContentPane().add(textField_1);
        textField_1.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setBounds(70, 111, 71, 20);
        getContentPane().add(textField_2);
        textField_2.setColumns(10);

        textField_3 = new JTextField();
        textField_3.setBounds(70, 142, 71, 20);
        getContentPane().add(textField_3);
        textField_3.setColumns(10);

        textField_4 = new JTextField();
        textField_4.setBounds(70, 173, 71, 20);
        getContentPane().add(textField_4);
        textField_4.setColumns(10);

        textField_5 = new JTextField();
        textField_5.setBounds(70, 204, 72, 20);
        getContentPane().add(textField_5);
        textField_5.setColumns(10);

        textField_6 = new JTextField();
        textField_6.setBounds(70, 235, 72, 20);
        getContentPane().add(textField_6);
        textField_6.setColumns(10);

        textField_7 = new JTextField();
        textField_7.setBounds(151, 49, 48, 20);
        getContentPane().add(textField_7);
        textField_7.setColumns(10);

        textField_8 = new JTextField();
        textField_8.setBounds(151, 80, 48, 20);
        getContentPane().add(textField_8);
        textField_8.setColumns(10);

        textField_9 = new JTextField();
        textField_9.setBounds(151, 111, 48, 20);
        getContentPane().add(textField_9);
        textField_9.setColumns(10);

        textField_10 = new JTextField();
        textField_10.setBounds(151, 142, 48, 20);
        getContentPane().add(textField_10);
        textField_10.setColumns(10);

        textField_11 = new JTextField();
        textField_11.setBounds(151, 173, 48, 20);
        getContentPane().add(textField_11);
        textField_11.setColumns(10);

        textField_12 = new JTextField();
        textField_12.setBounds(151, 204, 48, 20);
        getContentPane().add(textField_12);
        textField_12.setColumns(10);

        textField_13 = new JTextField();
        textField_13.setBounds(151, 235, 48, 20);
        getContentPane().add(textField_13);
        textField_13.setColumns(10);

        textField_14 = new JTextField();
        textField_14.setBounds(207, 49, 59, 20);
        getContentPane().add(textField_14);
        textField_14.setColumns(10);

        textField_15 = new JTextField();
        textField_15.setBounds(207, 80, 59, 20);
        getContentPane().add(textField_15);
        textField_15.setColumns(10);

        textField_16 = new JTextField();
        textField_16.setBounds(207, 111, 59, 20);
        getContentPane().add(textField_16);
        textField_16.setColumns(10);

        textField_17 = new JTextField();
        textField_17.setBounds(207, 142, 59, 20);
        getContentPane().add(textField_17);
        textField_17.setColumns(10);

        textField_18 = new JTextField();
        textField_18.setBounds(207, 173, 59, 20);
        getContentPane().add(textField_18);
        textField_18.setColumns(10);

        textField_19 = new JTextField();
        textField_19.setBounds(207, 204, 59, 20);
        getContentPane().add(textField_19);
        textField_19.setColumns(10);

        textField_20 = new JTextField();
        textField_20.setBounds(207, 235, 59, 20);
        getContentPane().add(textField_20);
        textField_20.setColumns(10);

        textField_21 = new JTextField();
        textField_21.setBounds(269, 49, 32, 20);
        getContentPane().add(textField_21);
        textField_21.setColumns(10);

        textField_22 = new JTextField();
        textField_22.setBounds(269, 80, 32, 20);
        getContentPane().add(textField_22);
        textField_22.setColumns(10);

        textField_23 = new JTextField();
        textField_23.setBounds(269, 111, 32, 20);
        getContentPane().add(textField_23);
        textField_23.setColumns(10);

        textField_24 = new JTextField();
        textField_24.setBounds(269, 142, 32, 20);
        getContentPane().add(textField_24);
        textField_24.setColumns(10);

        textField_25 = new JTextField();
        textField_25.setBounds(269, 173, 32, 20);
        getContentPane().add(textField_25);
        textField_25.setColumns(10);

        textField_26 = new JTextField();
        textField_26.setBounds(269, 204, 32, 20);
        getContentPane().add(textField_26);
        textField_26.setColumns(10);

        textField_27 = new JTextField();
        textField_27.setBounds(269, 235, 32, 20);
        getContentPane().add(textField_27);
        textField_27.setColumns(10);

        textField_28 = new JTextField();
        textField_28.setBounds(312, 49, 86, 20);
        getContentPane().add(textField_28);
        textField_28.setColumns(10);

        textField_29 = new JTextField();
        textField_29.setBounds(312, 80, 86, 20);
        getContentPane().add(textField_29);
        textField_29.setColumns(10);

        textField_30 = new JTextField();
        textField_30.setBounds(312, 111, 86, 20);
        getContentPane().add(textField_30);
        textField_30.setColumns(10);

        textField_31 = new JTextField();
        textField_31.setBounds(312, 142, 86, 20);
        getContentPane().add(textField_31);
        textField_31.setColumns(10);

        textField_32 = new JTextField();
        textField_32.setBounds(312, 173, 86, 20);
        getContentPane().add(textField_32);
        textField_32.setColumns(10);

        textField_33 = new JTextField();
        textField_33.setBounds(312, 204, 86, 20);
        getContentPane().add(textField_33);
        textField_33.setColumns(10);

        textField_34 = new JTextField();
        textField_34.setBounds(312, 235, 86, 20);
        getContentPane().add(textField_34);
        textField_34.setColumns(10);

    }
}

共有1个答案

汪才英
2023-03-14

创建表和模型,然后创建列标题:

DefaultTableModel dftModel = new DefaultTableModel();
JTable mytable = new JTable(dftModel);
myTable.setColumnIdentifiers(new String [] {"Tipo","Column 1","Column 2","Column 3"});

根据需要从arraylist中添加行:

myTable.addRow(new String[] {"",yourArrayList.get(1),"blah","blah"});

添加您的Tipo组合框,如下所示:

String [] tipArray = {"A","B","C"};
DefaultComboBoxModel comboModel = new DefaultComboBoxModel(tipArray);
JComboBox ComboTipo = new JComboBox(comboModel);
mytable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(ComboTipo));
 类似资料:
  • 问题内容: 我正在尝试使用导入文件: 我不明白是怎么工作的,因为如果我继续初始化三个变量(,和)的,它失败的: 如果我仅保留docNumber,则它可以工作。你可以帮帮我吗? 问题答案: 输入文件中的某些行似乎包含少于3个逗号分隔的字段。您应始终检查令牌生成器是否具有更多令牌(StringTokenizer.hasMoreTokens),除非您100%确信输入正确。 正确解析CSV文件并不是一件容

  • 我有一个csv文件,我想将它导入JTable。 是否有一个简单的示例显示如何将csv文件导入JTable?

  • 问题内容: 我创建了一个数据库和一个表。我还创建了所有需要的字段。我创建了46个字段,其中一个是该行的ID。CSV不包含ID字段,也不包含列的标题。我对所有这些都是陌生的,但一直在努力解决。我不是在这里懒惰地寻求答案,而是在寻找方向。 我试图弄清楚如何导入CSV,但是让它开始从第二个字段开始导入数据,因为我希望auto_increment将填充ID字段,这是我创建的第一个字段。 我没有运气就尝试了

  • 我正在开发一个基于Django的web应用程序。 我将把一个csv导入postgresql数据库,该数据库有100000多行,并将其用作Django应用程序的数据库。在这里,我面临两个问题。字段名包括以下特殊字符: 首先,如何定义Postgresql数据库的字段名以导入csv? 导入后,我将通过django模型获取数据。那么如何定义包含特殊字符的Django模型变量名呢? 当然,如果我更改包含特殊

  • 问题内容: 我有一个 对象,我需要在JTable中显示此数组的内容。 Map-键是列名,对象是数据,我该如何放置? Baiscally Map包含一个表中的多行,并且所有行都添加到了数组列表中,现在我需要以表的形式在Swing应用程序中显示它们,并对它们执行排序和过滤。 问题答案: 对于数据,将您的列表嵌入到TableModel中。一的DefaultTableModel是最好的。 对于列,将您的列

  • 我必须使用谷歌云语音API在Xamarin.iOS应用程序中实现语音转换文本功能。 首先,我尝试使用google.cloud.speech.v1 Nuget包,但是gRPC库似乎不能与Xamarin一起工作(https://github.com/gRPC/gRPC/issues/1620,https://forums.Xamarin.com/discussion/94534/how-to-use-