Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey guys I am having trouble getting my table to work with a java GUI if anything you can take a look at my code

Hey guys I am having trouble getting my table to work with a java GUI if anything you 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! PLEASE do this in Eclipse! I have noticed people using other programs and I am still getting a LOT of errors 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 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 createListEmployees(){

List retVal = new ArrayList();

Employee( String name, String job, int age, String email, ArrayList 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 phoneNumbers;

public Employee(String name, String job, int age, String email, ArrayList 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 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 listEmployees;

public EmployeeTableModel(List 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);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Makers And Takers The Rise Of Finance And The Fall Of American Business

Authors: Rana Foroohar

1st Edition

0553447238, 978-0553447231

Students also viewed these Databases questions