[RESOLVED] Convert to using a list instead of the model
Hi,
I need to know how to convert project to use a list of strings to populate the table without using the table model to populate the table with the data. This is my attempt at trying to make the project work without the table model but as you can see it has tons of errors.
Files in the DAO:
PencilDAO:
java Code:
package DAO;
import model.Pencil;
import model.*;
import java.sql.*;
import java.util.*;
/**
* Data Access Class for the Pencil database
*/
public class PencilDAO extends PencilsApplicationDAO{
/**
* The Prepared statement used for selecting Pencils
mainFrame.displayDatabaseUnExpectedError("Unable to Retrieve colour of pencils!");
} else {
mainFrame.displaynewListOfColours(colourList);
}
// Initial data of books is null until user makes a choice. This 'null' based
// model will at least allow the JTable column headings to be visible.
data = null;
//mainFrame.displaynewListOfColours;
}
/**
* Uses the DAO to connect to the database.
* Will display a popup message dialogue if the connection fails and then
* exit the application. Otherwise a connection is established and the instance
* variable is ready to be used.
*/
private void connectToDatabase() {
try {
PencilsApplicationDAO.connectToDatabase();
} catch (SQLException ex) {
mainFrame.displayDatabaseConnectionError();
System.exit(-1);
}
}
/**
* User (through view) has requested display of Books of a particular
* pubName. These will be retrieved from the table using the DAO methods. Then
* the view will be asked to update itself using the new BookJTableModel or
* display an error if something goes wrong.
* @param pubName The pubName of the books to be displayed
*/
public void displaycolourpencils(String colour) {
data = PencilDAO.retrievePencils(colour);
if (data == null) {
mainFrame.displayDatabaseUnExpectedError("Unable to Retrieve colour of pencils " + colour);
} else {
this.mainFrame.displayNewPencilData(data);
}
}
/**
* Get everything going
* @param args Not used
*/
public static void main(String args[]) {
new PencilApplication();
}
}
Thanks,
Nightwalker
Last edited by Nightwalker83; Jun 20th, 2012 at 01:59 AM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Re: [RESOLVED] Convert to using a list instead of the model
Files in the Model:
ColourComboBoxModel:
java Code:
package model;
import java.util.*;
import javax.swing.*;
/**
* A model for the colours JList
*
*/
public class ColourComboBoxModel extends AbstractListModel implements ComboBoxModel {
/**
* The list of colours
*/
private List<String> data;
private String selectedItem = "Make a Choice";
/**
* Initialise the data to the provided list
* @param data The list of colours
*/
public ColourComboBoxModel(List<String> data) {
this.data = data;
}
/**
*
* @return The number of colours to display
*/
public int getSize() {
if (data != null) {
return data.size();
} else {
return 0;
}
}
/**
*
* @param index The position within the list
* @return the colours to display at that position. This would be taken from
* the data list
*/
public Object getElementAt(int index) {
if (data == null) {
return "";
}
String cellData;
cellData = data.get(index);
return cellData;
}
public void setSelectedItem(Object anItem) {
this.selectedItem = (String) anItem;
}
public Object getSelectedItem() {
return this.selectedItem;
}
public void setData(List<String> colourList) {
this.data = colourList;
fireContentsChanged(this, 0, 0);
}
}
Pencil:
java Code:
package model;
public class Pencil {
public static final String DEF_COLOUR = "RED";
public static final int DEF_LENGTH = 0;
private String colour;
private int length;
public Pencil() {
this(DEF_COLOUR, DEF_LENGTH);
}
public Pencil(String colour, int length) {
this.colour = colour.toUpperCase();
this.length = length;
}
/**
* Get the value of length
*
* @return the value of length
*/
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour.toUpperCase();
}
public String toString() {
return super.toString() + "[" +
"colour=" + this.colour +
",length=" + this.length +
"]";
}
}
PencilJTableModel:
java Code:
package model;
import java.util.*;
import javax.swing.table.*;
/**
* A model for the Pencil JTable
*
*/
public class PencilJTableModel extends AbstractTableModel {
/**
* The list of Pencil objects used for the JTable display
*/
private String[] columnNames = new String[]{"Colour", "Length"};
private List<Pencil> data;
public PencilJTableModel() {
this(null);
}
public PencilJTableModel(List<Pencil> data) {
this.data = data;
}
/**
*
* @return The number of rows to be displayed which would be the size of the
* data list.
*/
public int getRowCount() {
if (data != null) {
return data.size();
} else {
return 0;
}
}
/**
*
* @return The number of columns to display
*/
public int getColumnCount() {
return columnNames.length;
}
/**
* Returns the value for the cell at rowIndex and columnIndex.
* Extracts the Pencil object at index rowIndex from the list data then
* extracts the Pencil data from that object according to the names in
* colNames
*
* @param rowIndex the rowIndex whose value is to be queried
* @param columnIndex the columnIndex whose value is to be queried
* @return the value Object at the specified cell
*/
public Object getValueAt(int rowIndex, int columnIndex) {
Pencil record;
Object cellData = null;
String colName;
if (data == null) {
return "";
}
record = data.get(rowIndex);
switch (columnIndex) {
case 0:
cellData = record.getColour();
break;
case 1:
cellData = record.getLength();
break;
default:
cellData = "unkown";
break;
}
return cellData;
}
/**
* Returns the column names for the table Pencil column 0 is Colour and
* column 1 is Length.
* If column is not 0 or 1 will return a null String.
*
* @param column the column being queried
* @return a string containing the name of column
*/
public String getColumnName(int column) {
return columnNames[column];
}
public void setData(List<Pencil> data) {
this.data = data;
fireTableDataChanged();
}
}
Last edited by Nightwalker83; Jun 20th, 2012 at 01:57 AM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672
Re: [RESOLVED] Convert to using a list instead of the model
Use my attempt at the above exercise I completed it by changing the following:
1. In the retrieveDistinctColours() method, since there is no question mark in the sql statement you don't need (can't use) the setString() method. This will crash it.
2. In the displaycolourpencils() method in PencilApplication.java, you're calling retrieveAllColourPencils() - you should be calling the retrievePencils method instead.
3. In the DAO using the variable selectColourPreparedStatement in two methods retrievePencils and retrieveDistinctColours. You need to have two Prepared Statements (one for each method)
4. In my database I had colour and length.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu. https://get.cryptobrowser.site/30/4111672