Pratik
Pratik

Reputation: 367

JTable new column Add not successful

Below is a part of code I am trying to resolve. What I am trying t achieve here is "To add one more blank column at the end and then filling up cell values row by row for that column".

Eventhough I can debug, in line table.setValueAt(finalCell, row, table.getColumnCount()-1);

Its trying to update cell (1,5) but after that line my row becomes [Hello, , , , 1] from [Lee, Hsien Yang, , , , 1].

What I am expecting is [Lee, Hsien Yang, , , , 1,Hello]

But here its updating first column rather than new 5th one.

How can I achieve my goal?

            CSVReader reader = new CSVReader(new FileReader(file)); 

            List<String[]> myEntries = reader.readAll();
            String[][] rowData = myEntries.toArray(new String[0][]);

            String[] columnNames = { "People", "Place", "Organisation", "Event", "Mentions" };

            JTable table=new JTable(rowData, columnNames);

            TableColumn newColumn=new TableColumn();
            table.getColumnModel().addColumn(newColumn);

            Boolean isFirstLine=true;

            for(int row=0;row< table.getRowCount();row++)
            {
                if(isFirstLine)
                {
                    isFirstLine=false;
                    continue;
                }
                String finalCell="";

                for(int col=0;col<table.getColumnCount()-1;col++)
                {
                    finalCell="Hello";
                }
                table.setValueAt(finalCell, row, table.getColumnCount()-1);
            }

Upvotes: 0

Views: 494

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

Use an appropriate TableModel

            CSVReader reader = new CSVReader(new FileReader(file));

            List<String[]> myEntries = reader.readAll();
            String[][] rowData = myEntries.toArray(new String[0][]);

            String[] columnNames = {"People", "Place", "Organisation", "Event", "Mentions"};

            DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames);

            tableModel.addColumn(""); // What ever name you want to give it...

            Boolean isFirstLine = true;

            for (int row = 0; row < tableModel.getRowCount(); row++) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue;
                }
                String finalCell = "";

                for (int col = 0; col < tableModel.getColumnCount() - 1; col++) {
                    finalCell = "Hello";
                }
                tableModel.setValueAt(finalCell, row, tableModel.getColumnCount() - 1);
            }

            JTable table = new JTable(tableModel);

The TableModel is generally responsible for managing the data and is generally the go to place to set these things up.

See How to Use Tables for more details

Thanks but New column name is coming null

Works fine for me...

enter image description here

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                String[] columnNames = {"People", "Place", "Organisation", "Event", "Mentions"};

                String[][] rowData = new String[][]{
                    {"Lee", "Hsien Yang", "", "", ""}
                };

                DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames);

                tableModel.addColumn(""); // What ever name you want to give it...

                Boolean isFirstLine = true;

                for (int row = 0; row < tableModel.getRowCount(); row++) {
                    tableModel.setValueAt("Hello", row, tableModel.getColumnCount() - 1);
                }

                JTable table = new JTable(tableModel);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

What seems strange is, you're example code;

[Lee, Hsien Yang, , , , 1]

has 6 columns, but you're only allowing for 5...

String[] columnNames = {"People", "Place", "Organisation", "Event", "Mentions"};

This will overwrite the data for that column when you call addColumn

Upvotes: 1

Related Questions