-2

How can insert this set of code in jtable. The problem is when I do md.addElement(id); it shows me a red underline on addElement()

Here is my code

public class hospitalisation extends javax.swing.JFrame {

DefaultTableModel md = new DefaultTableModel();

public hospitalisation() {
    initComponents();

    hospitalisationtable.setModel(md);
    buttonGroup1.add(male);
    buttonGroup1.add(female);

}
private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {                                       
    String id = "id: " + idtxt.getText();
    String name = "name: " + nametxt.getText();
    String sex = "sex:";
    String address = "address: " + addresstxt.getText();
    String sdate = "sdate: " + sdatetxt.getText();
    String room = "room: " + cs_room.getSelectedItem().toString();
    String father = "father: " + fathertxt.getText();
    String phone = "phone: " + phonetxt.getText();
    String age = "age: " + agetxt.getText();
    String edate = "edate " + edatetxt.getText();

    if (female.getModel().isSelected() == true)
        sex += female.getText();
    else if (male.getModel().isSelected() == true)
        sex += male.getText();

    md.addElement(id);
    md.addElement(name);
    md.addElement(sex);
    md.addElement(address);
    md.addElement(sdate);
    md.addElement(room);
    md.addElement(father);
    md.addElement(phone);
    md.addElement(age);
    md.addElement(edate);        
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • from md.addElement(id) upto md.addElement(edate) all are red underline Please a need your help thank you for advance – I Need help Jun 08 '16 at 19:13
  • 1
    Your IDE should show **more** than just a red underline. It will also show text that explains **why** the line of code doesn't compile. Please post that with your question. – Hovercraft Full Of Eels Jun 08 '16 at 19:15
  • 1
    Shoot, DefaultTableModel doesn't have an `addElement(...)` method -- so don't use it. Only use methods that are legal, methods that the [API](http://docs.oracle.com/javase/8/docs/api/javax/swing/table/DefaultTableModel.html) tells you you can use. – Hovercraft Full Of Eels Jun 08 '16 at 19:16
  • 1
    Instead create a `Vector`, fill it with your row of data, and then call the DefaultTableModel method, `addRow(...)` passing in your vector. – Hovercraft Full Of Eels Jun 08 '16 at 19:17

1 Answers1

2

it shows me a red underline on addElement() this is because as you can see here there is no DefaultTableModel.addElement(String) in that class...

Consider instead taking a look at the doc and maybe consider one of the given methods like

addColumn(Object columnName)
addColumn(Object columnName, Object[] columnData)
addColumn(Object columnName, Vector columnData)
addRow(Object[] rowData)
addRow(Vector rowData)
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97