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

Recommended Answers

All 3 Replies

Because setDataVector sets the data for the entire table.

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

Take a look at the methods in DefaultTableModel and use the getModel method of JTable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.