Hi. I have a constructor like below whose class extends JFrame. The problem is that my for loop is printing only the last string.( I am using this information ultimately to build a JButton table).

Can somebody tell me whats wrong with this ?
Below is the code:-

public MyclassConstructor() {
    super("JButton Table builder");

    DefaultTableModel dm = new DefaultTableModel();
    String[] Fields1={"String1","String2","String3","String4","String5"};
    for(int i=0; i<Fields1.length;i++){
    dm.setDataVector(new Object[][] { {new String(Fields1[i])} },
        new Object[] { "Fields", "Answers" });
    }
    JTable table = new JTable(dm);
    String[] answer = { "A", "B", "C", "D", "E", "F" };

    table.getColumn("My Answers").setCellRenderer(
        new RadioButtonRenderer(answer));
    table.getColumn("My Answers").setCellEditor(
        new RadioButtonEditor(new JCheckBox(), new RadioButtonPanel(
            answer)));
    JScrollPane scroll = new JScrollPane(table);
    getContentPane().add(scroll);
  }

Normally I do it like this and get all the strings correctly:

public JRadioButtonTableExample2() {
    super("JButton Table builder");

    DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(new Object[][] { {new String("String1") },
        {{new String("String2") }, { {new String("String3") },
        { {new String("String4") }, { {new String("String5") } },
        new Object[] { "Fields", "Answers" });

    JTable table = new JTable(dm);
    String[] answer = { "A", "B", "C", "D", "E", "F" };

    table.getColumn("Map to MGB Format").setCellRenderer(
        new RadioButtonRenderer(answer));
    table.getColumn("Map to MGB Format").setCellEditor(
        new RadioButtonEditor(new JCheckBox(), new RadioButtonPanel(
            answer)));
    JScrollPane scroll = new JScrollPane(table);
    getContentPane().add(scroll);
  }

Thanks

How can I make it work if I try to do it the way I am trying to fill using for loop ? Is there any way to do it like filling strings dynamically ?

Thanks

This article has been dead for over six months. Start a new discussion instead.