0

I making a JTable and i want a list of String is inserted in a single column in the JTable when a button is clicked.

will this be possible with this type of data:

String datelist = format.format(cal2.getTime()));
List<String> wordList = Arrays.asList(datelist);
System.out.println(wordList);

The output will be like this:

[2013-05-10]

[2013-05-11]

[2013-05-12]

[2013-05-13]

[2013-05-14]

[2013-05-15]

[2013-05-16]

[2013-05-17]

[2013-05-18]

[2013-05-19]

[2013-05-20]

I'm using DefatulTableModel:

table_4 = new JTable (new DefaultTableModel(
                new Object[][] {
                        {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                    {"", "", "", "", "", "", "", "", "", "", "", ""},
                },
                new String[] {
                    "", "", "", "", "", "", "", "", "", "", "", ""
5
  • The short answer is "yes" it can be done...but I don't think that's what you're asking. Take a look at How to use Tables for more details Commented May 30, 2013 at 1:30
  • Ofcourse it is possible. Question is you want to insert into an existing JTable or create a new JTable? Commented May 30, 2013 at 1:31
  • how can i insert that list in the JTable? Commented May 30, 2013 at 1:34
  • @meewoK: i want to insert it in a existing Jtable :D Commented May 30, 2013 at 1:36
  • Ok...example with code updated :) Commented May 30, 2013 at 1:53

1 Answer 1

2

First off I think you have an error...DateList is a String, but your converting it into a list?

List<String> wordList = Arrays.asList(datelist);

I think this results in a list of zero elements.

Second off, based on examples such as https://stackoverflow.com/a/5107112/1688441 I have made the following code:

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TableDemo extends JFrame {

    DefaultTableModel model;

    public TableDemo() {

        JButton btnNewButton = new JButton("Insert Data");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                model.setValueAt("Testing", 0, 0);
            }
        });
        getContentPane().add(btnNewButton, BorderLayout.NORTH);
    }
    JTable jTable1;

    public static void main(String... args) {

        TableDemo tableDemo = new TableDemo();
        tableDemo.pamnel();

        JFrame frame = new TableDemo();

    }

    public void initComponents() {
    }

    public void pamnel() {
        initComponents();
        String[] columnNames = { "", "", "", "", "", "", "", "", "", "", "", "" };

        String[][] data = { { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" },
                { "", "", "", "", "", "", "", "", "", "", "", "" } };
        model = new DefaultTableModel(data, columnNames);

        jTable1 = new JTable(model);

        getContentPane().add(new JScrollPane(jTable1));
        pack();
        setVisible(true);
    }
}

You can use the model.setValueAt("VALUE HERE", ROW, COLUMN); call to set any value within the existing data.

Additionally, you can use the model object to add new rows and new columns.

So, if you want to insert new rows with the first column being your dates:

model.addRow(new String[]{ datelist, "", "", "", "", "", "", "", "", "", "", "" });
Sign up to request clarification or add additional context in comments.

4 Comments

"You can use the model.setValueAt("VALUE HERE", ROW, COLUMN); call to set any value within the existing data." thanks a lot for this men!
how can i call the next string in the string list and set that String to model.setValueAt("NEXT STRING", ROW, COLUMN);
because i want to fill all the row with the list.
You could have an iterator or for loop, to go through your list and have the setValue command there...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.