-1

I'm trying to create a address book and all I currently need is a table to store the data, however I cannot get it to display on the panel, any ideas why? I've got a method which should be creating the table on execution but its not.

private String name;
private String surname;
private String phone;
private String address;
static String summary;
private JPanel panel;
private JFrame frame;
private JLabel lblName;
private JLabel lblAddress;
private JLabel lblSurname;
private JLabel lblPhone;
private JTextField txtName;
private JTextField txtSurname;
private JTextField txtPhone;
private JTextField txtAddress;
private JButton btnSave;
private JButton btnUpload;
private JButton btnDelete;
String columns[] = {"Name","Surname","Phone","Address"};

String[][] data = 
    {{"human", "bean","07443244","somewhere"},
            {"Max", "Kenny","044353534","Somewhere"}};



public AddressBook() {
    createForm();
    createLabels();
    createTextField();
    createButton();
    createTable();
    frame.add(panel);
    frame.setVisible(true);

}
public void createLabels(){
    lblName = new JLabel ("Name");
    lblName.setBounds(10, 30, 100, 20);
    panel.add (lblName);

    lblSurname = new JLabel ("Surname");
    lblSurname.setBounds(10, 50, 100, 20);
    panel.add (lblSurname);

    lblAddress = new JLabel ("Address");
    lblAddress.setBounds(10, 70, 100, 20);
    panel.add (lblAddress);

    lblPhone = new JLabel ("Phone");
    lblPhone.setBounds(10, 90, 100, 20);
    panel.add (lblPhone);

}

public void createTextField(){

    txtName = new JTextField (null);
    txtName.setBounds(110, 30, 150, 20);
    panel.add (txtName);

    txtSurname = new JTextField (null);
    txtSurname.setBounds(110, 50, 150, 20);
    panel.add (txtSurname);

    txtAddress = new JTextField (null);
    txtAddress.setBounds(110, 70, 150, 20);
    panel.add (txtAddress);

    txtPhone = new JTextField (null);
    txtPhone.setBounds(110, 90, 150, 20);
    panel.add (txtPhone);

}

public void createForm(){
    panel = new JPanel();
    panel.setBorder (BorderFactory.createLineBorder (Color.RED, 3));
    panel.setLayout(null);



    frame = new JFrame();
    frame.setTitle("Address Book"); 
    frame.setSize(800,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

public void createButton(){
    btnSave = new JButton ("Save to a file");
    btnSave.setBounds(50, 200, 200, 20);
    btnSave.addActionListener(new SaveHandler());  
    panel.add (btnSave);


    btnDelete = new JButton ("Delete from the table");
    btnDelete.setBounds(50, 300, 200, 20);
    panel.add (btnDelete);

    btnUpload = new JButton ("Upload file to table");
    btnUpload.setBounds(50, 400, 200, 20);
    panel.add (btnUpload);


}

public void createTable(){
    JTable table = new JTable(data, columns);
    DefaultTableModel model = new DefaultTableModel();
    model.setColumnIdentifiers(columns);
    table.setModel(model);
    table.setBackground(Color.LIGHT_GRAY);
    table.setForeground(Color.black);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    panel.add(table);
}

class SaveHandler implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==btnSave)
        {
            name = ("");
            surname = ("");
            phone = ("");
            address = ("");

            name = txtName.getText();
            surname = txtSurname.getText();
            phone = txtPhone.getText();
            address = txtAddress.getText();

            summary = ("Name:" +name)+(surname)+("Phone:" + phone)+("Address:" + address);
            String saveFile = summary;

            try {
                BufferedWriter reader = new BufferedWriter(new FileWriter(new File("userinfo.txt"), true));
                reader.write(saveFile);
                reader.newLine();
                reader.close();
                JOptionPane.showMessageDialog(frame, "The details were sucessfuly saved");

            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    } }




public static void main(String[] args) {

    new AddressBook();

}

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
breadkun
  • 13
  • 3
  • 1) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Apr 22 '16 at 00:29

1 Answers1

3
   panel.setLayout(null);

You chose to use no layout manager and handle all of the layout yourself.

JTable table = new JTable(data, columns);
table.setModel(model);
table.setBackground(Color.LIGHT_GRAY);
table.setForeground(Color.black);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

However, you did not specify a size and position for the JTable.

I recommend learning to use the layout managers instead. It will work better, it will handle the user resizing the window, and will make the code more maintainable.

dsh
  • 12,037
  • 3
  • 33
  • 51
  • @breadkun, start with the section from the Swing tutorial on [Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for information and working examples. You will also want to check out the table of contents for the section on `How to Use Tables`. – camickr Apr 21 '16 at 17:02