I am currently creating, or more trying to create the GUI for my project. What I need to do now, is create a JTable that reads the data from a LinkedList. I have a custom TableAbstractModel for this.
public class StudentTableModel extends AbstractTableModel {
public static final String[] columnNames = { "ID", "First Name",
"Last Name" };
private LinkedList<Student> data;
public StudentTableModel(LinkedList<Student> data) {
this.data = data;
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public String getColumnName(int column) {
return columnNames[column];
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Student student = data.get(rowIndex);
if (student == null) {
return null;
}
switch (columnIndex) {
case 0:
return student.getID();
case 1:
return student.getFirstname();
case 2:
return student.getLastname();
default:
return null;
}
}
}
Now I was trying to build a very basic layout consisting just of the table, and the appropriate JTextfields and JButtons to test if the table actually works.
public class GUI extends JFrame {
private JTextField StudentID;
private JTextField Firstname;
private JTextField Lastname;
private JTextField Group;
private JButton Add;
private static Database DB; // << Does this have to be static? Eclipse tells me it needs to be, but I am not sure.
private JTable StudentTable;
public GUI(Database DB) {
super("Theatre Management");
setExtendedState(JFrame.MAXIMIZED_BOTH);
this.DB = DB;
LinkedList<Student> data = null;
setLayout(new FlowLayout());
StudentID = new JTextField("Enter Student ID", 10);
Firstname = new JTextField("Enter First Name");
Lastname = new JTextField("Enter Last Name");
Group = new JTextField("Enter Group ID");
Add = new JButton("Add Student");
StudentTable = new JTable();
StudentTable.setModel(new StudentTableModel(data));
JScrollPane scrollPane = new JScrollPane(StudentTable);
StudentTable.setFillsViewportHeight(true);
add(StudentTable);
add(StudentID);
add(Firstname);
add(Lastname);
add(Group);
add(Add);
add(scrollPane);
addStudentEvent add = new addStudentEvent();
Add.addActionListener(add);
}
public static void createAndShow() {
GUI Database = new GUI(DB);
Database.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Database.setVisible(true);
}
public static void main(String[] args) {
GUI.createAndShow();
}
private class addStudentEvent implements ActionListener {
public void actionPerformed(ActionEvent event) {
int G = Integer.parseInt(Group.getText());
int ID = Integer.parseInt(StudentID.getText());
String First = Firstname.getText();
String Last = Lastname.getText();
DB.getGroup(G).addStudent(ID, First, Last);
DB.addStudent(ID, First, Last);
}
}
}
Now, the problem is, as soon as I try to run the GUI, I just get a whole bunch of errors.
Exception in thread "main" java.lang.NullPointerException
at theatremanagement.StudentTableModel.getRowCount(StudentTableModel.java:29)
at javax.swing.JTable.getRowCount(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.createTableSize(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.getPreferredSize(Unknown Source)
at javax.swing.JComponent.getPreferredSize(Unknown Source)
at java.awt.FlowLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at java.awt.Container.validateUnconditionally(Unknown Source)
at java.awt.Window.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at theatremanagement.GUI.createAndShow(GUI.java:59)
at theatremanagement.GUI.main(GUI.java:64)
I am really stuck right now and have no clue how to fix that. If anybody could show me what I am doing wrong, that would be great. On other thing, I have to LinkedList in my program, in different classes. How does the TableModel know which one I want? Do I need to add anything to the TableModel, to make the table automatically update as soon as a add or delete a student?
Update:
private JTable setStudentList(LinkedList<Student> Studentlist) {
JTable StudentTable = new JTable();
StudentTable.setModel(new StudentTableModel(Studentlist));
JScrollPane scrollPane = new JScrollPane(StudentTable);
StudentTable.setFillsViewportHeight(true);
return StudentTable;
Does this method work to create the table with the linked list? Would I then call for the method as
setStudentList(Studentlist);
If I initiliazed an empty LinkedList as such:
LinkedList<Student> StudentList = new LinkedList<Student>();
Thanks for the help.
The LinkedList is never initalised...
LinkedList<Student> data = null;
// ... ///
StudentTable.setModel(new StudentTableModel(data));
Meaning that when getRowCount is called, the data object is null, hence the NullPointerException
Updated
You have any number of options that you can use to pass a reference to main LinkedList into the UI, but it will come down to how your work flow works.
You could...
Pass the reference to the UI via it's constructor....
public GUI(Database DB, LinkedList<Student> students) {...}
You could...
Pass the LinkedList in via a method in you GUI class...
public class GUI extends JFrame {
//...//
public GUI(Database DB) {...}
public void setStudent(LinkedList<Student> students) {
StudentTable.setModel(new StudentTableModel(students));
}
}
Updated with example
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
public class PassTable {
public static void main(String[] args) {
new PassTable();
}
public PassTable() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private ExampleTable example;
public TestPane() {
setLayout(new BorderLayout());
example = new ExampleTable();
add(example);
JButton add = new JButton("Add");
add(add, BorderLayout.SOUTH);
add.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
LinkedList<Person> people = new LinkedList<Person>();
people.add(new Person("A", "A", 1));
people.add(new Person("B", "B", 2));
people.add(new Person("C", "C", 3));
people.add(new Person("D", "D", 4));
example.setPeople(people);
}
});
}
}
public class ExampleTable extends JPanel {
private JTable table;
public ExampleTable() {
this(new LinkedList<Person>());
}
public ExampleTable(LinkedList<Person> people) {
setLayout(new BorderLayout());
table = new JTable(new SampleTableModel(people));
add(new JScrollPane(table));
}
public void setPeople(LinkedList<Person> people) {
table.setModel(new SampleTableModel(people));
}
}
public class SampleTableModel extends AbstractTableModel {
private LinkedList<Person> data;
public SampleTableModel(LinkedList<Person> data) {
this.data = data;
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public String getColumnName(int column) {
String name = "";
switch (column) {
case 0:
name = "First name";
break;
case 1:
name = "Last name";
break;
case 2:
name = "Age";
break;
}
return name;
}
#Override
public int getColumnCount() {
return 3;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
Person person = data.get(rowIndex);
switch (columnIndex) {
case 0:
value = person.getFirstName();
break;
case 1:
value = person.getLastName();
break;
case 2:
value = person.getAge();
break;
}
return value;
}
}
public class Person {
private String firstName;
private String lastName;
private int age;
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public int getAge() {
return age;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
}
Related
I have created a JTable with some information about formula 1 racing car drivers. The below code is for the JTable
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.event.*;
public class Test1 extends JFrame implements ActionListener {
JButton button;
Test1() {
//setBounds(100, 100, 500, 400);
JFrame frame = new JFrame();
button = new JButton();
//button.setBounds(50, 50, 20, 10);s
button.setText("Random Race");
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(new Model1Data());
frame.setBounds(100, 100, 500, 400);
JPanel panel = new JPanel();
frame.add(panel);
panel.add(new JScrollPane(table));
panel.add(button);
//add(new J
frame.setVisible(true);
//this.add(button);
pack();
}
#Override
public void actionPerformed(ActionEvent e) {
Formula1ChampionsManager d= new Formula1ChampionsManager();
d.button();
}
}
This is the code for the Model1Data. This is the code for the table to update its cells.
import javax.swing.table.AbstractTableModel;
public class Model1Data extends AbstractTableModel implements ChampionsManager {
String colNames[] = { "Name", "Team", "No of first Places", "Total Points" };
Class<?> colClasses[] = { String.class, String.class, Integer.class, Integer.class };
public int getRowCount() {
return myDrivers.size();
}
public int getColumnCount() {
return colNames.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return myDrivers.get(rowIndex).getName();
}
if (columnIndex == 1) {
return myDrivers.get(rowIndex).getTeam();
}
if (columnIndex == 2) {
return myDrivers.get(rowIndex).getfirstPlace();
}
if (columnIndex == 3) {
return myDrivers.get(rowIndex).totalPoints();
}
return null;
}
public String getColumnName(int columnIndex) {
return colNames[columnIndex];
}
public Class<?> getColumnClass(int columnIndex) {
return colClasses[columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 0) {
myDrivers.get(rowIndex).setName((String) aValue);
}
if (columnIndex == 1) {
myDrivers.get(rowIndex).setTeam((String) aValue);
}
if (columnIndex == 2) {
myDrivers.get(rowIndex).setfirstPlace((Integer) aValue);
}
if (columnIndex == 3) {
myDrivers.get(rowIndex).settotalPoints((Integer) aValue);
}
//fireTableCellUpdated(rowIndex, columnIndex);
}
}
This what the table GUI looks like Table GUI. When I click the button the values for No of first places and Total Points are changed randomly. But the table doesn't get updated with the values. If I pull on the side of the frame it gets updated. How do I get it to update when I click the button?
The JTable listens for changes in the TableModel. Hence class AbstractTableModel has methods such as fireTableCellUpdated(rowIndex, columnIndex) (which you commented out in your code - why?). If you override method setValueAt then you need to call that method, otherwise the JTable will not update when its data is changed. I'm guessing that changing the size of the JFrame causes a repaint of the JTable which causes the JTable to reload the data from its model (but I could be wrong).
Your class Model1Data should extend DefaultTableModel, rather than AbstractTableModel, since that class already contains all the functionality you require. The only method that class Model1Data needs to override is getColumnClass (which it already does). You also need to override at least one constructor. Since you didn't post a minimal, reproducible example, I'm guessing that this constructor may be appropriate. Hence the code for class Model1Data should be:
public class Model1Data extends javax.swing.table.DefaultTableModel {
Class<?> colClasses[] = { String.class, String.class, Integer.class, Integer.class };
public Model1Data(Object[] columnNames, int rowCount) {
super(columnNames, rowCount);
}
public Class<?> getColumnClass(int columnIndex) {
return colClasses[columnIndex];
}
}
And in class Test1 (which does not need to extend JFrame, by the way) you can create the JTable like so:
JTable table = new JTable(new Model1Data(new String[]{ "Name", "Team", "No of first Places", "Total Points" }, 0));
Edit
Here's my take on what you are trying to do. The below code can be copied as is, compiled and run. Just click on the Random Race button and the data in the JTable will update.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Test1 {
private JTable table;
private void buildAndDisplayGui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTable(), BorderLayout.CENTER);
frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JButton createButton(String text, int mnemonic, ActionListener listener) {
JButton button = new JButton(text);
button.setMnemonic(mnemonic);
button.addActionListener(listener);
return button;
}
private JPanel createButtonsPanel() {
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(createButton("Random Race", KeyEvent.VK_R, this::randomRace));
return buttonsPanel;
}
private JScrollPane createTable() {
table = new JTable(new Model1Data());
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
return scrollPane;
}
/**
* Simulates a race and randomly determines placings of the drivers and assigns appropriate
* points scores.
*
* #param event - encapsulates button press event
*/
private void randomRace(ActionEvent event) {
Formula1ChampionsManager.simulate();
TableModel tm = table.getModel();
if (tm instanceof Model1Data) {
Model1Data model = (Model1Data) tm;
model.setRowCount(0);
model.init();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new Test1().buildAndDisplayGui();
});
}
}
class Model1Data extends DefaultTableModel {
private static final String[] COLUMN_NAMES = {"Name", "Team", "No of first Places", "Total Points"};
public Model1Data() {
super(COLUMN_NAMES, 0);
init();
}
public void addDriver(F1Driver driver) {
addRow(new Object[]{driver.getName(),
driver.getTeam(),
driver.getFirstPlace(),
driver.getTotalPoints()});
}
public void init() {
Formula1ChampionsManager.getDrivers()
.stream()
.forEach(this::addDriver);
}
}
class Formula1ChampionsManager {
private static List<F1Driver> drivers;
static {
drivers = List.of(new F1Driver("Lewis Hamilton", F1Teams.MERCEDES),
new F1Driver("Max Verstappen",F1Teams.RED_BULL),
new F1Driver("Charles Leclerc",F1Teams.FERRARI),
new F1Driver("Lando Norris",F1Teams.MCLAREN),
new F1Driver("Fernando Alonso",F1Teams.ALPINE),
new F1Driver("Pierre Gasly",F1Teams.ALPHATAURI),
new F1Driver("Sebastian Vettel",F1Teams.ASTON_MARTIN),
new F1Driver("Nicholas Latifi",F1Teams.WILLIAMS),
new F1Driver("Valtteri Bottas",F1Teams.ALFA_ROMEO),
new F1Driver("Mick Schumacher",F1Teams.HAAS));
}
public static List<F1Driver> getDrivers() {
return drivers;
}
public static void simulate() {
List<F1Driver> result = new ArrayList<>(drivers);
Collections.shuffle(result);
F1Driver first = result.get(0);
first.update(F1Driver.FIRST);
F1Driver second = result.get(1);
second.update(F1Driver.SECOND);
F1Driver third = result.get(2);
third.update(F1Driver.THIRD);
F1Driver fourth = result.get(3);
fourth.update(F1Driver.FOURTH);
F1Driver fifth = result.get(4);
fifth.update(F1Driver.FIFTH);
F1Driver sixth = result.get(5);
sixth.update(F1Driver.SIXTH);
F1Driver seventh = result.get(6);
seventh.update(F1Driver.SEVENTH);
F1Driver eighth = result.get(7);
eighth.update(F1Driver.EIGHTH);
F1Driver ninth = result.get(8);
ninth.update(F1Driver.NINTH);
F1Driver tenth = result.get(9);
tenth.update(F1Driver.TENTH);
}
}
enum F1Teams {
MERCEDES("Mercedes"),
RED_BULL("Red Bull"),
FERRARI("Ferrari"),
MCLAREN("McLaren"),
ALPINE("Alpine"),
ALPHATAURI("AlphaTauri"),
ASTON_MARTIN("Aston Martin"),
WILLIAMS("Williams"),
ALFA_ROMEO("Alfa Romeo"),
HAAS("Haas");
private String name;
private F1Teams(String name) {
this.name = name;
}
#Override // java.lang.Enum
public String toString() {
return name;
}
}
class F1Driver {
public static final int FIRST = 25;
public static final int SECOND = 18;
public static final int THIRD = 15;
public static final int FOURTH = 12;
public static final int FIFTH = 10;
public static final int SIXTH = 8;
public static final int SEVENTH = 6;
public static final int EIGHTH = 4;
public static final int NINTH = 2;
public static final int TENTH = 1;
private String name;
private F1Teams team;
private int firstPlace;
private int totalPoints;
public F1Driver(String name, F1Teams team) {
this(name, team, 0, 0);
}
public F1Driver(String name, F1Teams team, int firstPlace, int totalPoints) {
Objects.requireNonNull(name, "Missing name.");
this.name = name;
this.team = team;
this.firstPlace = firstPlace;
this.totalPoints = totalPoints;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public F1Teams getTeam() {
return team;
}
public void setTeam(F1Teams team) {
this.team = team;
}
public int getFirstPlace() {
return firstPlace;
}
public void setFirstPlace(int firstPlace) {
this.firstPlace = firstPlace;
}
public int getTotalPoints() {
return totalPoints;
}
public void setTotalPoints(int totalPoints) {
this.totalPoints = totalPoints;
}
#Override // java.lang.Object
public boolean equals(Object obj) {
boolean equal = this == obj;
if (!equal) {
if (obj instanceof F1Driver) {
F1Driver other = (F1Driver) obj;
equal = name != null &&
name.equals(other.name) &&
team == other.team;
}
}
return equal;
}
#Override // java.lang.Object
public int hashCode() {
return (String.valueOf(name) + String.valueOf(team)).hashCode();
}
#Override // java.lang.Object
public String toString() {
return String.format("%s [%s] %d [%d]", name, team, totalPoints, firstPlace);
}
public void update(int points) {
totalPoints += points;
if (points == FIRST) {
firstPlace++;
}
}
}
Here's how it looks when I run the above code.
I've a BeanTableModel that extends a RowTableModel. And a XTableColumnModel that extends DefaultTableColumnModel. Both Models came from this site: BeanTableModel and RowTableModel source
I can create the table and display data. I can update the table with new data but I CAN NOT update the table with new data from a different class.
E.g, I've a Names.class and I send a List<Names> to the table to print and it does so as expected. When I try to update the table with a different List<Names> it does update as well but when I try to update the table with a list of Dogs it will not update, in this case if I send a List<Dogs> it does not update the table and this is the main question here, how can I update the table with different class objects?!
Here is a brief runnable I've creating to replicate this error:
NOTE: It seems I can't post all the code in here due to the char limit on the post due to the BeanTableModel, RowTableModel and XTableColumnModel being quite big classes. Therefore I've left a link to my dropbox account where you can download all the source files for this short runnable jar.
Main class:
import javax.swing.SwingUtilities;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Design();
}
});
}
}
Design class:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Design extends JFrame {
private LeftPanel leftPanel;
private TablePanel tablePanel;
public Design() {
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
setMinimumSize(new Dimension(600, 400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
leftPanel = new LeftPanel();
tablePanel = TablePanel.getInstance();
add(leftPanel, BorderLayout.NORTH);
add(tablePanel, BorderLayout.SOUTH);
}
}
LeftPanel class
public class LeftPanel extends JPanel {
private JButton startButton;
private JButton editWithDogButtton;
private JButton editWithPerson;
private TablePanel tablePanel;
public LeftPanel() {
initComponents();
tablePanel = TablePanel.getInstance();
}
public void initComponents() {
startButton = new JButton("Create table!");
editWithDogButtton = new JButton("Edit with dog!");
editWithPerson = new JButton("Edit with person!");
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
add(startButton, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
add(editWithDogButtton, constraints);
constraints.gridx = 1;
constraints.gridx = 2;
add(editWithPerson, constraints);
startButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
List<Person> listPerson = new ArrayList<>();
listPerson.add(new Person("John", "Spencer"));
listPerson.add(new Person("Mike", "Strada"));
listPerson.add(new Person("Johan", "Anderson"));
ArrayList<String> columnLabels = new ArrayList<>();
columnLabels.add("First name");
columnLabels.add("Last name");
tablePanel.createTable(Person.class, listPerson, true, true, columnLabels);
}
});
editWithDogButtton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
List<Dog> listDog = new ArrayList<>();
listDog.add(new Dog("Bob", "German Sheppard"));
listDog.add(new Dog("Laika", "Bulldog"));
ArrayList<String> columnLabels = new ArrayList<>();
columnLabels.add("Dog's Name");
columnLabels.add("Race");
tablePanel.updateTable(listDog, columnLabels); // It doesn't work...!
}
});
editWithPerson.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
List<Person> listPerson2 = new ArrayList<>();
listPerson2.add(new Person("Jessica", "Carlton"));
listPerson2.add(new Person("Sthephanie", "Oujur"));
listPerson2.add(new Person("Angela", "Parker"));
ArrayList<String> columnLabels = new ArrayList<>();
columnLabels.add("First Name");
columnLabels.add("Last Name");
tablePanel.updateTable(listPerson2, columnLabels); // It works!
}
});
}
public void createTable(Class c, List data) {
JTable table;
BeanTableModel beanTableModel;
XTableColumnModel columnModel;
beanTableModel = new BeanTableModel(c, data);
columnModel = new XTableColumnModel();
table = new JTable(beanTableModel);
table.setColumnModel(columnModel);
table.createDefaultColumnsFromModel();
}
}
TablePanel Class
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.table.TableColumn;
public class TablePanel extends JPanel {
private JTable table;
private JFileChooser fileChooser;
private BeanTableModel beanTableModel;
private XTableColumnModel columnModel;
private Class c;
private Class classAncestor;
private List dataList;
private static TablePanel instance;
public synchronized static TablePanel getInstance()
{
if(instance == null)
{
instance = new TablePanel();
}
return instance;
}
private TablePanel() {
// fileChooser = new JFileChooser();
}
public void setTable(JTable table) {
this.table = table;
}
public void setBeanTableModel(BeanTableModel beanTableModel) {
this.beanTableModel = beanTableModel;
}
public void setColumnModel(XTableColumnModel columnModel) {
this.columnModel = columnModel;
}
public void setC(Class c) {
this.c = c;
}
public void setData(List data) {
this.dataList = data;
}
public List getDataList() {
return dataList;
}
public void createTable(Class c, List data, boolean toolBarUp,
boolean toolBarBottom, ArrayList<String> labelsCheckBox) {
beanTableModel = new BeanTableModel(c, data);
columnModel = new XTableColumnModel();
table = new JTable(beanTableModel);
table.setColumnModel(columnModel);
table.createDefaultColumnsFromModel();
if (toolBarUp == true) {
final JToolBar toolBarTop = new JToolBar();
JButton reset = new JButton("Reset");
reset.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for (Component c : toolBarTop.getComponents()) {
if (c instanceof JCheckBox) {
JCheckBox checkBox = (JCheckBox) c;
checkBox.setSelected(false);
columnModel.setAllColumnsVisible();
}
}
int numberOfColumn = columnModel.getColumnCount();
for (int aux = 0; aux < numberOfColumn; aux++) {
int num = columnModel.getColumnCount();
TableColumn column = columnModel.getColumnByModelIndex(aux);
columnModel.setColumnVisible(column, true);
}
}
});
toolBarTop.add(reset);
// Create a JCheckBox for each column
for (int i = 0; i < labelsCheckBox.size(); i++) {
final int index = i;
toolBarTop.add(new JCheckBox(new AbstractAction(labelsCheckBox.get(i)) {
#Override
public void actionPerformed(ActionEvent e) {
TableColumn column = columnModel.getColumnByModelIndex(index);
boolean visible = columnModel.isColumnVisible(column);
columnModel.setColumnVisible(column, !visible);
}
}));
}
setLayout(new BorderLayout());
add(toolBarTop, BorderLayout.NORTH);
add(new JScrollPane(table), BorderLayout.CENTER);
revalidate();
repaint();
}
if (toolBarBottom == true) {
final JToolBar toolBarDown = new JToolBar();
toolBarDown.add(new JButton(new AbstractAction("Save Table") {
#Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}));
add(toolBarDown, BorderLayout.SOUTH);
}
}
public void fireTableDataChanged() {
table.setModel(beanTableModel);
table.revalidate();
table.repaint();
}
public void updateTable(List l, List<String> columnNames) {
beanTableModel.updateTable(l, columnNames);
}
}
Dog Class:
public class Dog {
private String name;
private String race;
public Dog(String name, String race)
{
this.name = name;
this.race = race;
}
public void setName(String name) {
this.name = name;
}
public void setRace(String race) {
this.race = race;
}
public String getName() {
return name;
}
public String getRace() {
return race;
}
}
PErson Class
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Then you just need to add the BeanTableModel, RowTableModel and XTableColumnModel that you can find in the link above. Or you can download all the code above from the following dropbox link: All the necessary code - 14KB file size
So, if you run the code you'll see 3 JButtons, if you click on the "Create Table!" button it does work fine, the table is created.
If you click on the "Edit with person!" it will edit the table with a different List<Person> and it will work fine. But when you click "Edit with dog!" it will try to edit the table this time with a List<Dog> and not updating the list. It is generated an exception.
Here is the exception output:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: java.lang.ClassCastException#6a22778a
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at MainPackage.BeanTableModel.getValueAt(BeanTableModel.java:229)
at javax.swing.JTable.getValueAt(JTable.java:2720)
at javax.swing.JTable.prepareRenderer(JTable.java:5718)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JViewport.paint(JViewport.java:731)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Again I apologise for this quite big code but I'm really stuck in here and I need help to figure out what is wrong.
-------------------------- EDIT ---------------------------------
Here is the updateTable() method code:
public void updateTable(List l, List<String> columnNames)
{
super.setDataAndColumnNames(l, columnNames);
}
which calls this on RowTableModel Class:
protected void setDataAndColumnNames(List<T> modelData, List<String> columnNames) {
this.modelData = modelData;
this.columnNames = columnNames;
columnClasses = new Class[getColumnCount()];
isColumnEditable = new Boolean[getColumnCount()];
fireTableStructureChanged();
}
---------------------------- EDIT 2 ------------------------------
Here is my 'new' class LeftPanel:
public class LeftPanel extends JPanel {
private JButton startButton;
private JButton editWithDogButtton;
private JButton editWithPerson;
private TablePanel tablePanel;
private JTable table;
private BeanTableModel beanTableModel;
private XTableColumnModel columnModel;
public LeftPanel() {
initComponents();
tablePanel = TablePanel.getInstance();
table = new JTable();
columnModel = new XTableColumnModel();
}
public void initComponents() {
startButton = new JButton("Create table!");
editWithDogButtton = new JButton("Edit with dog!");
editWithPerson = new JButton("Edit with person!");
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
add(startButton, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
add(editWithDogButtton, constraints);
constraints.gridx = 1;
constraints.gridx = 2;
add(editWithPerson, constraints);
startButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
List<Person> listPerson = new ArrayList<>();
listPerson.add(new Person("John", "Spencer"));
listPerson.add(new Person("Mike", "Strada"));
listPerson.add(new Person("Johan", "Anderson"));
ArrayList<String> columnLabels = new ArrayList<>();
columnLabels.add("First name");
columnLabels.add("Last name");
tablePanel.createTable(Person.class, listPerson, true, true, columnLabels);
}
});
editWithDogButtton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
List<Dog> listDog = new ArrayList<>();
listDog.add(new Dog("Bob", "German Sheppard"));
listDog.add(new Dog("Laika", "Bulldog"));
ArrayList<String> columnLabels = new ArrayList<>();
columnLabels.add("Dog's Name");
columnLabels.add("Race");
// Note to this code below
BeanTableModel<Dog> dogModel = new BeanTableModel<>(Dog.class);
dogModel.insertRows(0, listDog);
table.setModel(dogModel);
table.setColumnModel(columnModel);
table.createDefaultColumnsFromModel();
}
});
editWithPerson.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
List<Person> listPerson2 = new ArrayList<>();
listPerson2.add(new Person("Jessica", "Carlton"));
listPerson2.add(new Person("Sthephanie", "Oujur"));
listPerson2.add(new Person("Angela", "Parker"));
ArrayList<String> columnLabels = new ArrayList<>();
columnLabels.add("First Name2");
columnLabels.add("Last Name2");
tablePanel.updateTable(listPerson2, columnLabels); // It works!
}
});
}
public void createTable(Class c, List data) {
beanTableModel = new BeanTableModel(c, data);
table.setModel(beanTableModel);
table.setColumnModel(columnModel);
table.createDefaultColumnsFromModel();
}
}
Now there is no Exception when editWithDogButtton is pressed but nothing happens with the JTable. I think it may be due to a new table being created or hidden or something but I'm not sure...
A BeanTableModel can only contain objects of one type.
You created the TableModel to hold Person objects, so you can't just add a Dog object to the model.
If you are trying to remove all the Person object in the model and replace them with Dog objects, then you would need to create a completely new BeanTableModel for the Dog class.
Edit:
You create a new model with code like the following:
BeanTableModel<Dog> dogModel = new BeanTableModel<Dog>(Dog.class);
dogModel.insertRows(...);
table.setModel( dogModel );
Then in your case because you are using XTableColumnModel you may need to do extra work, I'm not sure how it works.
Also, I'm not sure why you created an updateTable() method in your TablePanel class. The RowTableModel has methods that allows you to dynamically add data to the model. There was no need to add you own updateTable() method to the BeanTableModel.
I'm having a little scenario here that maybe a duplicate. I have JTable where i show some data, i have a mouselistener that listens for right clicks on the table and displays a popup menu with one menuitem. My goal is that when the user clicks the menuitem, i get the values from the table and feed them into a custom dialog which has some fill in fields, so that the user doesn't have to feed the whole dialog by hand since i will feed the dialog with the values selected on the table. but my problem is that the menuitem actionevent doesn't have a way of getting the point so that i can use table.getRowAt(). i have read another comment (check here https://stackoverflow.com/a/4122082/1788917) where i have been told that i can save the row number in a variable that can then be accessed by the actionlistener for the menuitem.
i want it to look like this
theScrollPane.getViewport().add(myJTable, null);
JMenuItem menuItem = new JMenuItem(clickMe);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuItemActionPerformed(e);
}
});
MouseListener popupListener = new PopupListener();
popupMenu.add(menuItem);
myJTable.addMouseListener(popupListener);
protected void menuItemActionPerformed (ActionEvent e) {
// what to do here to get the table row data
// and send it to my custom dialog via it's constructor ???
}
// a simple nested class for the popup
class PopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
int row = myJTable.rowAtPoint(e.getPoint());
int selectedRow = myJTable.getSelectedRow();
// just to make sure the popup appears only where the row is
// selected
if (row == selectedRow) {
showPopup(e);
}
}
public void mouseReleased(MouseEvent e) {
int row = myJTable.rowAtPoint(e.getPoint());
int selectedRow = myJTable.getSelectedRow();
if (row == selectedRow) {
showPopup(e);
}
}
private void showPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
}
so my question is, is saving the row number the only way that i can do this or is there a better way?
i have read another comment (check here https://stackoverflow.com/a/4122082/1788917) where i have been told that i can save the row number in a variable
You read the wrong comment.
If you read the answer above that link (ie. the one with 7 votes) you will see the solution is to select the row you clicked on BEFORE showing the popup menu. Then in your menu item Action you can reference
table.getSelectedRow();
I have tried your case referring to this case and I am able to get the row and show it in a JDialog. I just modified my old code to do this. I haven't thoroughly tested.
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class AllTableExample {
private JTabbedPane tabbedPane;
private JTable allTable;
private AllTableModel allTableModel;
public AllTableExample() {
List<TableData> data = new ArrayList<TableData>();
data.add(new TableData("John1", "A", "Maths", "Hellen1"));
data.add(new TableData("John2", "A", "Maths", "Hellen2"));
data.add(new TableData("John3", "A", "Maths", "Hellen3"));
data.add(new TableData("John4", "A", "Maths", "Hellen4"));
data.add(new TableData("John5", "A", "Maths", "Hellen5"));
allTableModel = new AllTableModel(data);
}
public void createUI() {
JFrame frame = new JFrame();
tabbedPane = new JTabbedPane();
tabbedPane.add("All", getAllTablePanel());
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Table Model Example.");
frame.pack();
frame.setVisible(true);
}
private JPanel getAllTablePanel() {
JPanel panel = new JPanel();
allTable = new JTable(allTableModel);
JScrollPane scroll = new JScrollPane(allTable);
panel.add(scroll);
allTable.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
int r = allTable.rowAtPoint(e.getPoint());
if (r >= 0 && r < allTable.getRowCount()) {
allTable.setRowSelectionInterval(r, r);
} else {
allTable.clearSelection();
}
int rowindex = allTable.getSelectedRow();
if (rowindex < 0)
return;
if (e.getComponent() instanceof JTable) {
JDialog dialog = new JDialog();
int selectedRow = allTable.getSelectedRow();
dialog.setTitle("Edit Row: " + selectedRow);
TableData data = ((AllTableModel) allTable.getModel()).getTableData().get(selectedRow);
List<TableData> tempData = new ArrayList<TableData>();
tempData.add(data);
AllTableModel tempModel = new AllTableModel(tempData);
JTable table = new JTable(tempModel);
dialog.add(new JScrollPane(table));
dialog.setVisible(true);
}
}
});
return panel;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new AllTableExample().createUI();
}
};
EventQueue.invokeLater(r);
}
}
class AllTableModel extends AbstractTableModel {
List<TableData> tableData = new ArrayList<TableData>();
Object[] columnNames = {"Name", "Grade", "Subject", "Staff"};
public AllTableModel(List<TableData> data) {
tableData = data;
}
public List<TableData> getTableData() {
return tableData;
}
#Override
public String getColumnName(int column) {
return columnNames[column].toString();
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public int getRowCount() {
return tableData.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
TableData data = tableData.get(rowIndex);
switch (columnIndex) {
case 0:
return data.getName();
case 1:
return data.getGrade();
case 2:
return data.getSubject();
case 3:
return data.getStaff();
default:
return null;
}
}
}
class TableData {
private String name;
private String grade;
private String subject;
private String staff;
public TableData(String name, String grade, String subject, String staff) {
super();
this.name = name;
this.grade = grade;
this.subject = subject;
this.staff = staff;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getStaff() {
return staff;
}
public void setStaff(String staff) {
this.staff = staff;
}
}
Note: Store the row number of the table on which the pop-up opened such that after the changes in the popup dialog we have to update the original row.
The following code does not work on Mac OS X 10.6.8 but does on everything else I test, Mac OS X Lion and Windows 7. The obvious explanation would be differences in Apple's java. The table data does not show properly, if at all, on Mac. Here is the code
package com.dramble.dance;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
public class TableDemo extends JPanel {
public TableDemo() {
super(new GridLayout(1,0));
TableModel tableModel = new TableModel();
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
for(int i = 0 ; i <= 1900 ; i ++) {
DataRow row = new DataRow(i,"Firstname"+i,"Lastname"+i);
tableModel.appendRow(row);
tableModel.fireTableRowsInserted(i, i);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableDemo newContentPane = new TableDemo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private class DataRow {
private String fname, lname;
private Integer id;
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public Integer getId() {
return id;
}
public DataRow(Integer id, String fname, String lname) {
super();
this.fname = fname;
this.lname = lname;
this.id = id;
}
}
private class TableModel extends AbstractTableModel {
List<DataRow> data;
private String[] columnNames = {"ID #","First Name","Last Name"};
private Class[] columnTypes = {int.class, String.class, String.class};
public TableModel() {
this.data = new ArrayList<DataRow>();
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public String getColumnName(int col) {
return columnNames[col];
}
#Override
public int getRowCount() {
return this.data.size();
}
#Override
public Object getValueAt(int row, int col) {
DataRow dataRow = this.data.get(row);
Object returnValue = null;
switch (col) {
case 0:
returnValue = dataRow.getId();
break;
case 1:
returnValue = dataRow.getFname();
break;
case 2:
returnValue = dataRow.getLname();
break;
}
return returnValue;
}
public void appendRow(DataRow row) {
this.data.add(row);
}
#Override
public boolean isCellEditable(int rowIndex, int vColIndex) {
return false;
}
#Override
public Class getColumnClass(int col) {
return columnTypes[col];
}
}
}
Here is an image of the expected behavior as exhibited in Windows
Here is the same code on Mac OS X 10.6.8
The problem lies in the AbstractTableModel, which came as a surprise to me for some reason, I figured it would be EDT and thread related for sure. The problem is in my array for the columnTypes and using int.class. Changing this to Integer.class fixed my issue.
Absent a complete example, it's hard to guess. What you have looks OK, but two thing stand out:
I'm wary of frame.setContentPane(newContentPane), which replaces the frame's content pane with your TableDemo. As TableDemo is a JPanel, it inherits a UI delegate that varies by platform. Instead, consider frame.add(newContentPane), which forwards to the content pane.
Your appendRowData() method in DanceTableModel should fireTableRowsInserted(), so callers can't forget to do so.
I've been playing around with this for a while now with no success. I've had a look at ListModel but have struggled to implement it into my current project.
I have a Producer class thread adding elements to an ArrayList in the Model. This works fine and the ArrayList is being updated at runtime. My problem is, I then want the new objects added to the ArrayList to be added to the JList in the View class. I can't see how to incorperate ListModel or DefaultListModel into my current setup. Help much appreciated.
public class Person
{
private String name;
private int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return this.name;
}
}
public class Producer extends Thread
{
private Model model;
public Producer(Model model)
{
this.model = model;
}
public void run()
{
Person fred = new Person("Fred Flintstone", 37);
Person wilma = new Person("Wilma Flintstone", 18);
Person pebbles = new Person("Pebbles Flintstone", 15);
Person dino = new Person("Dino Flintstone", 45);
Person barney = new Person("Barney Rubble", 76);
Person betty = new Person("Betty Rubble", 76);
Person bamm = new Person("Bamm-Bamm Rubble", 76);
try
{
model.addPerson(fred);
Thread.sleep(1500);
model.addPerson(wilma);
Thread.sleep(1500);
model.addPerson(pebbles);
Thread.sleep(1500);
model.addPerson(dino);
Thread.sleep(1500);
model.addPerson(barney);
Thread.sleep(1500);
model.addPerson(betty);
Thread.sleep(1500);
model.addPerson(bamm);
}
catch(Exception e)
{
System.out.println("Error adding Person object to Model.people
ArrayList" + e);
}
}
}
public class Model
{
private List <Person> people;
public Model()
{
people = new ArrayList<Person>();
}
public List<Person> getPeople()
{
return people;
}
public void addPerson(Person aPerson)
{
people.add(aPerson);
System.out.println("Person object added to people list:" + aPerson);
}
public void removePerson(Person aPerson)
{
people.remove(aPerson);
}
}
public class View extends JFrame
{
private JPanel topPanel, botPanel;
private JList peopleList;
private JScrollPane scrollPane;
private Model model;
public View(Model model)
{
this.model = model;
setSize(200, 220);
setTitle("View");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
topPanel = new JPanel();
botPanel = new JPanel();
peopleList = new JList(model.getPeople().toArray());
scrollPane = new JScrollPane(peopleList);
topPanel.setLayout(new GridLayout(1, 1));
topPanel.add(scrollPane);
topPanel.setBorder(BorderFactory.createTitledBorder
(BorderFactory.createEtchedBorder(), "People list"));
Container cp = getContentPane();
cp.add(topPanel, BorderLayout.NORTH);
cp.add(botPanel, BorderLayout.SOUTH);
}
}
public class Main
{
public static void main(String[] args)
{
Model model = new Model();
View theView = new View(model);
theView.setVisible(true);
Producer producer = new Producer(model);
producer.start();
}
}
Use appropriate MVC-pattern here: Views listen for changes in the model and update according to notifications from the model:
Add PropertyChangeSupport on your model and fire PropertyChangeEvent's when modifying your model
Add the view as a PropertyChangeListener of your model
React to the notifications accordingly (if a Person is added to your model, when you receive the event that says "New person added", add it to the ListModel of your peopleList, same for "Person removed", etc...)
http://docs.oracle.com/javase/7/docs/api/java/beans/PropertyChangeSupport.html
Note: Since your model will be modified by another Thread than the EDT (Event dispatching Thread), make sure that you modify your UI on the EDT (Take a look at SwingUtilities.isEventDispatchingThread() and SwingUtilities.invokeLater())
UPDATE:
Here is a snippet that illustrates what I was trying to explain above (sorry if it is a bit lengthy but I tried to stay as close as possible to your original code):
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class Main {
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
#Override
public String toString() {
return this.name;
}
}
public class Producer extends Thread {
private Model model;
public Producer(Model model) {
this.model = model;
}
#Override
public void run() {
Random random = new Random();
Person fred = new Person("Fred Flintstone", 37);
Person wilma = new Person("Wilma Flintstone", 18);
Person pebbles = new Person("Pebbles Flintstone", 15);
Person dino = new Person("Dino Flintstone", 45);
Person barney = new Person("Barney Rubble", 76);
Person betty = new Person("Betty Rubble", 76);
Person bamm = new Person("Bamm-Bamm Rubble", 76);
while (true) {
try {
model.addPerson(fred);
Thread.sleep(1500);
model.addPerson(wilma);
Thread.sleep(1500);
model.addPerson(pebbles);
Thread.sleep(1500);
model.addPerson(dino);
Thread.sleep(1500);
model.addPerson(barney);
Thread.sleep(1500);
model.addPerson(betty);
Thread.sleep(1500);
model.addPerson(bamm);
while (model.getPeople().size() > 0) {
Person p = model.getPeople().get(random.nextInt(model.getPeople().size()));
model.removePerson(p);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public class Model {
private static final String PEOPLE = "people";
private List<Person> people;
private PropertyChangeSupport propertyChangeSupport;
public Model() {
people = new ArrayList<Person>();
propertyChangeSupport = new PropertyChangeSupport(this);
}
public PropertyChangeSupport getPropertyChangeSupport() {
return propertyChangeSupport;
}
public List<Person> getPeople() {
return people;
}
public void addPerson(Person aPerson) {
people.add(aPerson);
System.out.println("Person object added to people list:" + aPerson);
getPropertyChangeSupport().firePropertyChange(PEOPLE, null, aPerson);
}
public void removePerson(Person aPerson) {
people.remove(aPerson);
getPropertyChangeSupport().firePropertyChange(PEOPLE, aPerson, null);
}
}
public class View extends JFrame implements PropertyChangeListener {
private JPanel topPanel, botPanel;
private JList peopleList;
private JScrollPane scrollPane;
private Model model;
private DefaultListModel peopleListModel;
public View(Model model) {
this.model = model;
setSize(200, 220);
setTitle("View");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
topPanel = new JPanel();
botPanel = new JPanel();
peopleListModel = new DefaultListModel();
for (Person p : model.getPeople()) {
peopleListModel.addElement(p);
}
peopleList = new JList(peopleListModel);
model.getPropertyChangeSupport().addPropertyChangeListener(Model.PEOPLE, this);
scrollPane = new JScrollPane(peopleList);
topPanel.setLayout(new GridLayout(1, 1));
topPanel.add(scrollPane);
topPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "People list"));
Container cp = getContentPane();
cp.add(topPanel, BorderLayout.NORTH);
cp.add(botPanel, BorderLayout.SOUTH);
}
#Override
public void propertyChange(final PropertyChangeEvent evt) {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
propertyChange(evt);
}
});
return;
}
if (evt.getSource() == model) {
if (Model.PEOPLE.equals(evt.getPropertyName())) {
if (evt.getOldValue() != null && evt.getNewValue() == null) {
peopleListModel.removeElement(evt.getOldValue());
} else if (evt.getOldValue() == null && evt.getNewValue() != null) {
peopleListModel.addElement(evt.getNewValue());
}
}
}
}
}
public static void main(String[] args) {
new Main().init();
}
private void init() {
final Model model = new Model();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
View theView = new View(model);
theView.setVisible(true);
}
});
Producer producer = new Producer(model);
producer.start();
}
}