0

I'm doing a java app which has to pass an initialized array of string to display as a row to a JTable.

This is what I tried:

      Object[] _row1;

      public PrintMeNow() {
            table = new JTable();
    table.setBounds(16, 203, 362, 16);
    table.setShowHorizontalLines(true);
    table.setShowVerticalLines(true);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setModel(new DefaultTableModel(
        new Object[][] {
                _row1, {2,2,2}, {3,3,3}, {4,4,4}
        },
        new String[] {
            "QTY", "Item Code", "Amount"
        }
    ));

     }

  btnStart.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
           _row1 = new Object[]{"DN Korina", "DN Madrid", "DN Romania"};
      }
  });

But when I pressed the start button, it's not filling the row. What am I doing wrong in here? Help is much appreciated. Thanks.

neknek mouh
  • 1,802
  • 8
  • 27
  • 58
  • May the same question here http://stackoverflow.com/questions/20514181/actionevent-of-a-component-on-a-table-cell-doesnt-take-place – PHPFan Jan 07 '14 at 09:26

2 Answers2

3

What you need to do is add a row to the Model. Something like this

public void actionPerformed(ActionEvent arg0) {
    DefaultTableModel model = (DefaultTableModel)table.getModel();
    model.addRow(new Object[]{"DN Korina", "DN Madrid", "DN Romania"});
}

The easiest way is to set up you model before hand to have no rows

String[] colNames = {
       "QTY", "Item Code", "Amount"
};

DefaultTableModel model = new DefaultTableModel(colNames, 0);  <== 0 rows
JTable table = new JTable(model);

Then whatever rows you want to add, just add to the model

public void actionPerformed(ActionEvent e){
    model.addRow(new Object[]{"DN Korina", "DN Madrid", "DN Romania"});
}

UPDATE Example

import java.awt.*;
import java.util.Stack;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class TestTable {

    private String[] colNames = { "Col 1", "Col2", "Col3", "Col4", "Col5" };

    private DefaultTableModel model = new DefaultTableModel(colNames, 0);
    private JTable table = new JTable(model);
    private MyStack myStack = new MyStack();
    private Stack<Integer[]> stack;

    private JButton button = new JButton("Add Row");

    public TestTable() {
        stack = myStack.getStack();

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!stack.isEmpty()) {
                    Integer[] array = stack.pop();
                    model.addRow(array);
                }
            }
        });

        JFrame frame = new JFrame();
        frame.add(new JScrollPane(table), BorderLayout.CENTER);
        frame.add(button, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestTable();
            }
        });
    }
}

class MyStack {
    public Stack<Integer[]> stack = new Stack<Integer[]>();

    public MyStack() {
        int k = 1;
        for (int i = 0; i < 20; i++) {
            Integer[] array = new Integer[5];
            for (int j = 0; j < 5; j++) {
                array[j] = i * k * (j + 1);
            }
            k++;
            stack.push(array);
        }
    }

    public Stack<Integer[]> getStack() {
        return stack;
    }
}

Explanation

  • I have a MyStack class that has a Stack. It could be any data structure like an array even, but I chose to use a Stack because I can just .pop() it. And the get the Integer[].
  • I instantiate that class in the GUI class
  • I then extract that Stack. Keep in mind the Stack holds Integer[]'s
  • As i explained, you want to add a single dimension array to the model.
  • So every time I click the button, an Integer[] is pulled out of the Stack, then I just add that array to the model as a row.
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

But when I pressed the start button, it's not filling the row. What am I doing wrong in here?

  • your code inside ActionListener missing any code to addRow to JTable or to DefaultTableModel (table.getModel....)

  • in the case that you are using DefaultTableModel doesn't matter if is row added to JTable or DefaultTableModel

  • don't to use NullLayout in Swing, Swing is designated to be laid by using LayoutManager

mKorbel
  • 109,525
  • 20
  • 134
  • 319