Hey guys I am having trouble getting my table to work with a java GUI if anything can take a look at my code and see where I am going wrong with lines 38-49 it would be greatly appreciated! Under the JTableSortingDemo class is where I am having erorrs in my code! Thanks! :)
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class JTableSortingDemo extends JFrame{
private JTable table;
public JTableSortingDemo(){
super("This is basic sample for your Week08 assignment");
List<Employee> listEmployees = createListEmployees();
EmployeeTableModel tableModel = new EmployeeTableModel(listEmployees);
table = new JTable(tableModel);
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 1){
JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
JOptionPane.showMessageDialog(null, "RowNum is " + row +
"ColumnNum is " + column);
JOptionPane.showMessageDialog(null, "Cell data is " + table.getModel().getValueAt(row, column));
}
}
});
table.setAutoCreateRowSorter(true);
add(new JScrollPane(table),BorderLayout.CENTER);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public List<Employee> createListEmployees(){
List<Employee> retVal = new ArrayList();
Employee( String name, String job, int age, String email, ArrayList<String> phoneNumbers)
retVal.add(new Employee("Bob", "Technician", 33, "bob@bobemail.com", ["555123123","5553213213"]));
retVal.add(new Employee("Jane", "Accountant", 33, "jane@mail.com", ["525123123","5553413213"]));
retVal.add(new Employee("John", "Staff", 33, "John@bobemail.com", ["55323123","555113213"]));
retVal.add(new Employee("Tim", "Technician", 33, "tim@bobemail.com", ["555123123","5553213213"]));
retVal.add(new Employee("Tom", "Technician", 33, "tom@bobemail.com", ["555123123","5553213213"]));
retVal.add(new Employee("Tyler", "Technician", 33, "tyler@bobemail.com", ["555123123","5553213213"]));
retVal.add(new Employee("Gabe", "Programmer", 33, "gabe@bobemail.com", ["556123123","5553713213"]));
retVal.add(new Employee("Anna", "Technician", 33, "anna@bobemail.com", ["585123123","5553273213"]));
retVal.add(new Employee("Sara", "Staff", 33, "sara@bobemail.com", ["515123123","5553217213"]));
retVal.add(new Employee("Phil", "Technician", 33, "phil@bobemail.com", ["535123123","5753213213"]));
return retVal;
}
public static void main(String[] args) {
new JTableSortingDemo().setVisible(true);
}
}
---- seperate class
import java.io.Serializable;
import java.util.*;
import java.util.UUID;
public class Employee implements Serializable {
private long uid;
private int index;
private String name;
private String email;
private String job;
private int age;
private ArrayList<String> phoneNumbers;
public Employee(String name, String job, int age, String email, ArrayList<String> phoneNumbers) {
super();
this.uid = UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE; // generates a random UUID
this.name = name;
this.job = job;
this.age = age;
this.email = email;
this.phoneNumbers = phoneNumbers;
this.index = 1; // defualt index, wh
}
@Override
public String toString() {
return "Employee [index=" + index + ", uid=" + uid +", name=" + name + ", job=" + job + ", age=" + age + ", email=" + email + ", phone=" + phoneNumbers.get(0) +"]";
}
public long getUID(){
return uid;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail(){
return email;
}
public void setPhoneNumbers(ArrayList<String> phoneNumbers){
this.phoneNumbers = phoneNumbers;
}
public String getPhone(){
return this.phoneNumbers.get(0); // returns the first number
}
/*
public void addPhoneNumber(String phoneNumber){
this.phoneNumber.push(phoneNumber);
}
*/
}
----- serperate class
import javax.swing.table.*;
import java.util.*;
public class EmployeeTableModel extends AbstractTableModel {
private static final int COLUMN_NO = 0;
private static final int COLUMN_NAME = 1;
private static final int COLUMN_JOB = 2;
private static final int COLUMN_AGE = 3;
private static final int COLUMN_PHONE = 4;
private static final int COLUMN_EMAIL = 5;
private String[] columnNames = {"No","Name","Job","Age","Phone","Email"};
private List<Employee> listEmployees;
public EmployeeTableModel(List<Employee> listEmployees){
this.listEmployees = listEmployees;
int indexCount = 1;
for(Employee employee: listEmployees){
employee.setIndex(indexCount++);
}
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return listEmployees.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Employee employee = listEmployees.get(rowIndex);
Object retVal = null;
switch(columnIndex){
case COLUMN_NO: retVal = employee.getIndex(); break;
case COLUMN_NAME: retVal = employee.getName(); break;
case COLUMN_JOB: retVal = employee.getJob(); break;
case COLUMN_AGE: retVal = employee.getAge(); break;
case COLUMN_PHONE: retVal = employee.getPhone(); break;
case COLUMN_EMAIL: retVal = employee.getEmail(); break;
default: throw new IllegalArgumentException("Invalid column index.");
}
return retVal;
}
@Override
public String getColumnName(int columnIndex){
return columnNames[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex){
if(listEmployees.isEmpty()) return Object.class;
else return getValueAt(0, columnIndex).getClass();
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex){
Employee employee = listEmployees.get(rowIndex);
if(columnIndex == COLUMN_NO){
employee.setIndex((int)value);
}
}
}
Hi,Let me know if you need more information/implementation:-
====================================================
Let me know if you need to display phones numbers too.
===================================================
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import java.io.Serializable;
import java.util.*;
import java.util.UUID;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
public class JTableSortingDemo extends JFrame {
private JTable table;
public JTableSortingDemo() {
super("This is basic sample for your Week08 assignment");
List<Employee> listEmployees = createListEmployees();
EmployeeTableModel tableModel = new EmployeeTableModel(listEmployees);
table = new JTable(tableModel);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
JTable target = (JTable) e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
JOptionPane.showMessageDialog(null, "RowNum is " + row +
"ColumnNum is " + column);
JOptionPane.showMessageDialog(null, "Cell data is " + table.getModel().getValueAt(row, column));
}
}
});
table.setAutoCreateRowSorter(true);
add(new JScrollPane(table), BorderLayout.CENTER);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public List<Employee> createListEmployees() {
List<Employee> retVal = new ArrayList();
// Employee( String name, String
job, int age, String email, ArrayList<String>
// phoneNumbers)
retVal.add(new
Employee("Bob", "Technician", 33, "bob@bobemail.com",
new ArrayList<>(Arrays.asList("55323123",
"555113213"))));
retVal.add(new
Employee("Jane", "Accountant", 33, "jane@mail.com",
new ArrayList<>(Arrays.asList("55323123",
"555113213"))));
retVal.add(new
Employee("John", "Staff", 33, "John@bobemail.com",
new ArrayList<>(Arrays.asList("55323123",
"555113213"))));
retVal.add(new
Employee("Tim", "Technician", 33, "tim@bobemail.com",
new ArrayList<>(Arrays.asList("555123123",
"5553213213"))));
retVal.add(new
Employee("Tom", "Technician", 33, "tom@bobemail.com",
new ArrayList<>(Arrays.asList("555123123",
"5553213213"))));
retVal.add(new
Employee("Tyler", "Technician", 33, "tyler@bobemail.com",
new ArrayList<>(Arrays.asList("555123123",
"5553213213"))));
retVal.add(new
Employee("Gabe", "Programmer", 33, "gabe@bobemail.com",
new ArrayList<>(Arrays.asList("556123123",
"5553713213"))));
retVal.add(new
Employee("Anna", "Technician", 33, "anna@bobemail.com",
new ArrayList<>(Arrays.asList("585123123",
"5553273213"))));
retVal.add(new
Employee("Sara", "Staff", 33, "sara@bobemail.com",
new ArrayList<>(Arrays.asList("515123123",
"5553217213"))));
retVal.add(new
Employee("Phil", "Technician", 33, "phil@bobemail.com",
new ArrayList<>(Arrays.asList("535123123",
"5753213213"))));
retVal.add(new Employee("Phil",
"Technician", 33, "phil@bobemail.com",
new ArrayList<>(Arrays.asList("535123123",
"5753213213")))); //Corrected Here.
return retVal;
}
public static void main(String[] args) {
new JTableSortingDemo().setVisible(true);
}
}
class Employee implements Serializable {
private long uid;
private int index;
private String name;
private String email;
private String job;
private int age;
private ArrayList<String> phoneNumbers;
public Employee(String name, String job, int age, String email, ArrayList<String> phoneNumbers) {
super();
this.uid = UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE; // generates a random UUID
this.name = name;
this.job = job;
this.age = age;
this.email = email;
this.phoneNumbers = phoneNumbers;
this.index = 1; // defualt index, wh
}
@Override
public String toString() {
return "Employee [index=" +
index + ", uid=" + uid + ", name=" + name + ", job=" + job + ",
age=" + age
+ ", email=" + email + ", phone=" +
phoneNumbers.get(0) + "]";//this one has to change
if you need all //numbers
}
public long getUID() {
return uid;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public void setPhoneNumbers(ArrayList<String> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
public String getPhone() {
return this.phoneNumbers.get(0); // returns the first number
}
/*
*
* public void addPhoneNumber(String
phoneNumber){
*
* this.phoneNumber.push(phoneNumber);
*
* }
*
*/
}
class EmployeeTableModel extends AbstractTableModel {
private static final int COLUMN_NO = 0;
private static final int COLUMN_NAME = 1;
private static final int COLUMN_JOB = 2;
private static final int COLUMN_AGE = 3;
private static final int COLUMN_PHONE = 4;
private static final int COLUMN_EMAIL = 5;
private String[] columnNames = { "No", "Name", "Job", "Age", "Phone", "Email" };
private List<Employee> listEmployees;
public EmployeeTableModel(List<Employee> listEmployees) {
this.listEmployees = listEmployees;
int indexCount = 1;
for (Employee employee : listEmployees) {
employee.setIndex(indexCount++);
}
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return listEmployees.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Employee employee = listEmployees.get(rowIndex);
Object retVal = null;
switch (columnIndex) {
case COLUMN_NO:
retVal =
employee.getIndex();
break;
case COLUMN_NAME:
retVal =
employee.getName();
break;
case COLUMN_JOB:
retVal =
employee.getJob();
break;
case COLUMN_AGE:
retVal =
employee.getAge();
break;
case COLUMN_PHONE:
retVal =
employee.getPhone();
break;
case COLUMN_EMAIL:
retVal =
employee.getEmail();
break;
default:
throw new
IllegalArgumentException("Invalid column index.");
}
return retVal;
}
@Override
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if
(listEmployees.isEmpty())
return
Object.class;
else
return
getValueAt(0, columnIndex).getClass();
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
Employee employee = listEmployees.get(rowIndex);
if (columnIndex == COLUMN_NO) {
employee.setIndex((int) value);
}
}
}
================================
OUTPUT:-
==========================




***Please Comment if required.
=================================
Please Comment if required.
==============================
Thanks
Hey guys I am having trouble getting my table to work with a java GUI if...
I have currently a functional Java progam with a gui. Its a
simple table of contacts with 3 buttons: add, remove, and edit.
Right now the buttons are in the program but they do not work yet.
I need the buttons to actually be able to add, remove, or edit
things on the table. Thanks so much.
Here is the working code so far:
//PersonTableModel.java
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class PersonTableModel extends AbstractTableModel
{
private static final int...
This is my current output for my program.
I am trying to get the output to look like
This is my program
Student.java
import java.awt.GridLayout;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
public class Student extends javax.swing.JFrame {
BufferedWriter outWriter;
StudentA s[];
public Student() {
StudentGUI();
}
private void StudentGUI() {
jScrollPane3 = new
javax.swing.JScrollPane();
inputFileChooser = new
javax.swing.JButton();
outputFileChooser = new
javax.swing.JButton();
sortFirtsName = new...
I am getting this Error can you please fix my Java
code.
import java.awt.Dialog;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fall_2017 {
public TextArea courseInput;
public Label textAreaLabel;
public JButton addData;
public Dialog confirmDialog;
HashMap<Integer, ArrayList<String>> students;
public Fall_2017(){
courseInput = new TextArea(20, 40);
textAreaLabel = new Label("Student's data:");
addData = new JButton("Add...
I need help with my java code i am having a hard time compiling it // Cristian Benitez import java.awt.Color; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JPanel; Face class public class FaceDraw{ int width; int height; int x; int y; String smileStatus; //default constructor public FaceDraw(){ } // Getters and Setters for width,height,x,y public int getWidth(){ return width; } public void setWidth(int width){ this.width=width; } public int getHeight(){ return height; } public void setHeight(int height){ this.height=height; } public int getX(){ return...
How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...
C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employees to be compared by the Name, Age, and Pay, respectively. Code: Employee.Core.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Employees { partial class Employee { // Field data. private string empName; private int empID; private float currPay; private int empAge; private string empSSN = ""; private string empBene...
Java Programming
Answer 60a, 60b, 60c, 60d. Show code & output.
public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return...
Java
Do 68a, 68b, 68c, 68d. Show code & output.
public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...
Hi, I am having trouble solving the constructor, it requires to ArrayList marks to be a deep copy of the instance variable. I have also provided the J-Unit test I have been provided. import java.util.*; public class GradebookEntry { private String name; private int id; private ArrayList<Double> marks; /** * DO NOT MODIFY */ public String toString() { String result = name+" (ID "+id+")\t"; for(int i=0; i < marks.size(); i++) { /** * display every mark rounded off to two...
//Graph Class:
import java.util.ArrayList;
//Graph is a class whose objects represent graphs.
public class Graph {
ArrayList<Node> nodeList;
ArrayList<Edge> edgeList;
public Graph() {
nodeList = new ArrayList<Node>();
edgeList = new ArrayList<Edge>();
}
public ArrayList<Node> getNodeList() {
return nodeList;
}
public ArrayList<Edge> getEdgeList() {
return edgeList;
}
public void addNode(Node n) {
nodeList.add(n);
}
public void addEdge(Edge e) {
edgeList.add(e);
}
public String toString() {
String s = "Graph g.\n";
if (nodeList.size() > 0) {
for (Node n : nodeList) {
// Print node info
String t = "\nNode " + n.getName() + ", abbrev " + n.getAbbrev() + ", value " + n.getVal() + "\n";
s = s.concat(t);
}
s = s.concat("\n");
}
return s;
}
}
// Node Class:
import java.util.ArrayList;
// Node is a class whose objects represent nodes (a.k.a., vertices) in the graph.
public class Node {
String name;
String val; // The value of the Node
String abbrev; // The abbreviation for the Node
ArrayList<Edge> outgoingEdges;
ArrayList<Edge> incomingEdges;
String color; //Create the color of the TYPE Node List
int start; //Create the Starting Time
int end; //Create the Ending Time
public Node( String theAbbrev ) {
setAbbrev( theAbbrev );
val = null;
name = null;
outgoingEdges = new ArrayList<Edge>();
incomingEdges = new ArrayList<Edge>();
}
public String getAbbrev() {
return abbrev;
}
public String getName() {
return name;
}
public String getVal() {
return val;
}
public ArrayList<Edge> getOutgoingEdges() {
return outgoingEdges;
}
public ArrayList<Edge> getIncomingEdges() {
return incomingEdges;...