Reputation: 11
I have build a Java GUI which will add contact data to an ArrayList
. The JTable
will the update from the ArrayList
as well. However after adding new item my JTable
doesn't get update. I have used
table.repaint()
and table.fireTableDataChanged()
. Here is the code. I don't know what is wrong. Try to add new contact and see the change. It is nothing.
public class TableDemo extends JPanel implements ActionListener {
private JLabel name, contact;
private JTextField nameField, contactField;
private static JButton addButton, ok;
JTable table;
JFrame myFrame;
MyTableModel myModel;
public TableDemo() {
super(new GridLayout(1, 0));
myModel = new MyTableModel();
myModel.fireTableDataChanged();
table = new JTable(myModel);
table.setFillsViewportHeight(true);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
// Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
// Add the scroll pane to this panel.
add(scrollPane);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = { "Full Name", "Contact Number" };
ArrayList dataList = new ArrayList();
public MyTableModel() {
}
public MyTableModel(Contact c) {
dataList.add(c);
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return dataList.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
Contact widget = (Contact) dataList.get(row);
switch (col) {
case 0:
return widget.getFullName();
case 1:
return widget.getPhoneNumber();
default:
return null;
}
}
public void setValueAt(Contact c, int row, int column) {
Contact co = (Contact) dataList.get(row);
switch (column) {
}
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
return false;
}
public void addContact(Contact c) {
dataList.add(c);
fireTableDataChanged();
}
}
public void setValueAt(Object value, int row, int col) {
// dataList = value;
myModel.fireTableCellUpdated(row, col);
}
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
addButton = new JButton("Add");
JPanel myPanel = new JPanel();
myPanel.add(addButton);
addButton.addActionListener(new TableDemo());
frame.setLayout(new GridLayout(2, 1));
frame.setResizable(false);
frame.setSize(500, 500);
frame.add(myPanel);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
newFrame();
}
public void newFrame() {
myFrame = new JFrame();
JPanel myPanel = new JPanel();
name = new JLabel("Full Name");
nameField = new JTextField(10);
contact = new JLabel("Contact Number");
contactField = new JTextField(10);
ok = new JButton("OK");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myModel.addContact(new Contact(nameField.getText(),
contactField.getText()));
myFrame.dispose();
}
});
myPanel.add(name);
myPanel.add(nameField);
myPanel.add(contact);
myPanel.add(contactField);
myPanel.add(ok);
myFrame.add(myPanel);
myFrame.setVisible(true);
myFrame.setSize(250, 250);
}
}
public class Contact {
String fullName, phoneNumber;
public Contact(String fullName, String phoneNumber)
{
this.fullName = fullName;
this.phoneNumber = phoneNumber;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
>public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
Upvotes: 0
Views: 3709
Reputation: 4572
when you open your new JFrame
to display the Textfields for User Input, you create a new TableDemo
object, with a new MyTableModel
in it, so your TableModel
will fireTableDataChanged()
but not for the JTable
you would like.
I created a small example for you, to have a look how it can work.
The MainApp class:
package de.professional_webworkx.blog.contactmanager;
import de.professional_webworkx.blog.contactmanager.view.frame.MainFrame;
import javax.swing.SwingUtilities;
/**
*
* @author Patrick Ott <[email protected]>
*/
public class ContactManager {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainFrame mainFrame = new MainFrame();
}
});
}
}
And now the Domainclass Contact
package de.professional_webworkx.blog.contactmanager.domain;
import java.io.Serializable;
public class Contact implements Serializable {
private String firstName;
private String lastName;
protected Contact() {}
public Contact(final String firstName, final String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return firstName + " " + lastName;
}
}
The MainFrame
package de.professional_webworkx.blog.contactmanager.view.frame;
import de.professional_webworkx.blog.contactmanager.business.listener.AddContactListener;
import de.professional_webworkx.blog.contactmanager.domain.Contact;
import de.professional_webworkx.blog.contactmanager.view.table.model.ContactTableModel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MainFrame extends JFrame {
private JTable contactTable;
private JScrollPane scrollPane;
private ContactTableModel tableModel;
private List<Contact> contactList = new ArrayList<Contact>();
private JButton addContact;
public MainFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Contact List Manager");
JPanel contactTablePanel = initComponents();
addContact = new JButton("add new Contact");
addContact.addActionListener(new AddContactListener(contactTable));
this.getContentPane().add(addContact, BorderLayout.NORTH);
this.getContentPane().add(contactTablePanel, BorderLayout.CENTER);
this.setSize(new Dimension(1024, 768));
this.setVisible(true);
}
private JPanel initComponents() {
JPanel panel = new JPanel();
contactTable = new JTable(new ContactTableModel());
scrollPane = new JScrollPane(contactTable);
panel.add(scrollPane);
return panel;
}
}
The ContactTableModel, in your case it is the MyTableModel
package de.professional_webworkx.blog.contactmanager.view.table.model;
import de.professional_webworkx.blog.contactmanager.domain.Contact;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class ContactTableModel extends AbstractTableModel {
private final String[] columnNames = {"Firstname", "Lastname"};
List<Contact> contactList;
public ContactTableModel() {
contactList = new ArrayList<>();
contactList.add(new Contact("Patrick", "Ott"));
}
@Override
public int getRowCount() {
return contactList.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Contact contact = contactList.get(rowIndex);
switch(columnIndex) {
case(0):
return contact.getFirstName();
case(1):
return contact.getLastName();
}
return null;
}
public void addContact(final Contact contact) {
contactList.add(contact);
fireTableDataChanged();
}
}
The AddContactListener class
package de.professional_webworkx.blog.contactmanager.business.listener;
import de.professional_webworkx.blog.contactmanager.domain.Contact;
import de.professional_webworkx.blog.contactmanager.view.dialogs.AddContactDialog;
import de.professional_webworkx.blog.contactmanager.view.table.model.ContactTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTable;
public class AddContactListener implements ActionListener {
private JTable table;
public AddContactListener(final JTable table) {
this.table = table;
}
@Override
public void actionPerformed(ActionEvent e) {
ContactTableModel model = (ContactTableModel)this.table.getModel();
AddContactDialog dialog = new AddContactDialog();
String firstName = dialog.getFirstName();
String lastName = dialog.getLastName();
model.addContact(new Contact(firstName, lastName));
}
}
Do not open a new JFrame, instead use for example your own JDialog implementation.
AddContactDialog
package de.professional_webworkx.blog.contactmanager.view.dialogs;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AddContactDialog extends JDialog {
JButton ok, close;
JTextField firstNameField;
JTextField lastNameField;
String firstName;
String lastName;
private final AddContactDialog dialog;
public AddContactDialog() {
this.dialog = this;
this.setLayout(new BorderLayout(10, 10));
this.setModal(true);
this.setTitle("create new Contact");
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.setSize(new Dimension(350, 200));
this.getContentPane().add(initInputPanel(), BorderLayout.NORTH);
this.getContentPane().add(initDialogPanel(), BorderLayout.SOUTH);
this.pack();
this.setVisible(true);
}
/*
inits the TextField Panel
*/
private JPanel initInputPanel() {
JPanel panel = new JPanel(new GridLayout(2, 2));
firstNameField = new JTextField();
lastNameField = new JTextField();
panel.add(new JLabel("Firstname: "));
panel.add(firstNameField);
panel.add(new JLabel("Lastname: "));
panel.add(lastNameField);
return panel;
}
/*
inits the ButtonPanel
*/
private JPanel initDialogPanel() {
JPanel panel = new JPanel(new GridLayout(1, 2));
ok = new JButton("save");
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstName = firstNameField.getText();
lastName = lastNameField.getText();
dialog.setVisible(false);
}
});
close = new JButton("cancel");
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});
panel.add(ok);
panel.add(close);
return panel;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Hope it helps to solve your problem.
Patrick
Upvotes: 1