Fill table with data typed in the JTextField, separated by commas

1

I have the code below a JTextField , where I wanted to enter comma-separated numbers, and that by clicking JButton , the data would be added in a table column.

public class Dados extends javax.swing.JPanel {

    public Dados() {
        initComponents();
    }

        @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        vNomes = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jLabel1.setText("Digite os valores separados por virgula");

        jButton1.setText("Formar tabela");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "classes", "Xi", "Fi", "Fac"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(vNomes, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(214, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(vNomes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(412, Short.MAX_VALUE))
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField vNomes;
    // End of variables declaration                   
}
    
asked by anonymous 11.05.2018 / 01:16

1 answer

1

You can get the text from JTextField and use the split() " to separate the comma-separated values to then add to the table. Since you are using DefaultTableMoldel , you can use the addRow() " to add the rows.

To avoid additions when the field has nothing, I used the trim() to remove empty spaces in the String captured field, and the conditional to check that this string is not empty.

I could not reproduce your code, it looks like it was generated by netbeans , but see it working in the example I wrote below:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextField;
import javax.swing.JButton;

public class Dados2 extends JFrame {

    private JPanel contentPane;
    private JTextField textField;

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new Dados2().setVisible(true));
    }

    public Dados2() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(450, 300));
        contentPane = new JPanel(new BorderLayout());
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JPanel panelField = new JPanel();
        contentPane.add(panelField, BorderLayout.NORTH);

        textField = new JTextField();
        panelField.add(textField);
        textField.setColumns(20);

        String[] columnNames = new String[] { "classes", "Xi", "Fi", "Fac" };

        DefaultTableModel model = new DefaultTableModel(columnNames, 0);
        JTable table = new JTable(model);
        JScrollPane scroll = new JScrollPane(table);

        contentPane.add(scroll, BorderLayout.CENTER);

        JPanel panelBtn = new JPanel();
        contentPane.add(panelBtn, BorderLayout.SOUTH);

        JButton jbutton = new JButton("Adicionar");
        panelBtn.add(jbutton);

        jbutton.addActionListener(e -> {

            String text = textField.getText();

            if (text.length() > 0 && text.contains(",")) {

                String[] values = text.split(",");

                for (int i = 0; i < values.length; i++) {
                    model.addRow(new Object[] { null, values[i], null, null });
                }
            }
        });
        pack();
    }
}

Running:

Notethatinadditiontotheemptystring,theconditionalalsochecksiftherearecommasinthestring,whichmeansthatplaintextisnotaddedtoasinglestring.Ifyouwanttochangethisbehavior,simplyremovetheconditionaltext.contains(",") .

    
11.05.2018 / 02:20