2

I have little confusion. I am trying to update JTable, but the updates are not coming up. This is my present code:

private void addTable(){

        table = new JTable();// the table for 'Benchmark' tab
        table.setShowVerticalLines(false);
        table.setBorder(null);  

        data = new Object[][] {
                {"Key", ""},//result[0]
                {"Precision", ""},//result[2]+" %"
                {"Cipher", ""},//result[4]
                {"Language", ""},
                {"Performance", ""},//result[3]
        };


        String [] header = new String[] {"Subject of Interest", "Output"};
        model = new DefaultTableModel(data, header);        

        table.setModel(model);


        table.getColumnModel().getColumn(0).setPreferredWidth(136);
        table.getColumnModel().getColumn(1).setPreferredWidth(550);
        GroupLayout gl_panelBenchmark = new GroupLayout(panelBenchmark);
        gl_panelBenchmark.setHorizontalGroup(
                gl_panelBenchmark.createParallelGroup(Alignment.TRAILING)
                .addComponent(textInfoCrypto, GroupLayout.DEFAULT_SIZE, 759, Short.MAX_VALUE)
                .addGroup(gl_panelBenchmark.createSequentialGroup()
                        .addGap(10)
                        .addComponent(textPane, GroupLayout.DEFAULT_SIZE, 739, Short.MAX_VALUE)
                        .addGap(10))
                        .addGroup(Alignment.LEADING, gl_panelBenchmark.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(table, GroupLayout.DEFAULT_SIZE, 739, Short.MAX_VALUE)
                                .addContainerGap())
                );
        gl_panelBenchmark.setVerticalGroup(
                gl_panelBenchmark.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panelBenchmark.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(textInfoCrypto)
                        .addPreferredGap(ComponentPlacement.UNRELATED)
                        .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(ComponentPlacement.UNRELATED)
                        .addComponent(table, GroupLayout.PREFERRED_SIZE, 79, Short.MAX_VALUE)
                        .addContainerGap())
                );
        panelBenchmark.setLayout(gl_panelBenchmark);

    }

And this is how I am trying to update, at this stage, a single row. The below code has been invoked from another method:

data[0][0]= result[0];
model.fireTableCellUpdated(0, 0);

At this stage I can see only the table frames including row names, but no data. Any help, please?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
uml
  • 1,171
  • 4
  • 16
  • 28

2 Answers2

3

data is only local data not contained in the table model. You need to use:

tableModel.setValue(result[0], 0, 0);

Reimeus
  • 158,255
  • 15
  • 216
  • 276
2
  1. all fireXxxXxx event has DefaultTableModel implemented and correctly

  2. JTable in this from and based on DefaultTableModel haven't any issue with update ModelToView or ViewToModel, there no restriction,

  3. have to edit your questin with SSCCE

  4. before that read JTable tutorial

  5. especially JTable and TableModel

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I would love to read it, honestly, but that was pretty urgent. I got no time to learn from the scratch all the issues I encounter. – uml Aug 14 '12 at 12:41