Initialize column names in AbstractTableModel - java

I want to initialize my model. Here is an extract of my main class where I create my GUI items:
public class IceIHM extends JFrame{
List<String> listInit = new ArrayList<String>() {
{ add("None"); add("None"); add("None"); add("None"); add("None");add("None"); add("None"); }};
String[] tableauObjectTitle = {"Name (OBJECT)","Extended Name","Reference", "Entity Type","Relation", "Extended Relation","PMID"};
String[] tableauSubjectTitle = {"Name (SUBJECT)","Extended Name","Reference", "Entity Type","Relation", "Extended Relation","PMID"};
List<List<String>> dataObject = new ArrayList<List<String>>(){
{ add(listInit); add(listInit);}};
List<List<String>> dataSubject = new ArrayList<List<String>>(){
{ add(listInit); add(listInit);}};
ModelTableau modelObject = new ModelTableau(dataObject,tableauObjectTitle);
ModelTableau modelSubject = new ModelTableau(dataSubject, tableauSubjectTitle);
JTable tableauObject = new JTable(modelObject);
JTable tableauSubject = new JTable(modelSubject);
Here is my class ModelTableau:
public class ModelTableau extends AbstractTableModel{
private List<List<String>> data = new ArrayList<List<String>>();
private String[] headers;;
public ModelTableau(List<List<String>> data, String[] headers){
super();
this.data=data;
this.headers=headers;
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return headers.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get(rowIndex).get(columnIndex);
}
public void removeRow(int row) {
data.remove(row);
fireTableRowsDeleted(row, row);
}
public void addRow(List<String> listData) {
data.add(listData);
fireTableRowsInserted(data.size()-1, data.size()-1);
}
public void setColumnName(int i, String name) {
headers[i] = name;
fireTableStructureChanged();
}
}
Data are well initialized, but not my headers. Can anyone help me to find my mistake?

Override getColumnName(), as shown here, and return headers[col].
#Override
public String getColumnName(int col) {
return headers[col];
}

Related

Copy the list of pojo to clipboard

Lang: Java
We are trying to copy the list of pojo to the clipboard in the table format.
Bullet point: what we are trying to achieve here :
1. convert list of pojo into table format
2. If the user copy it in some excel sheet then the it should be copied easily or even if the user try to copy in notepad it should print in the table format.
3. Add some meta data to clipboard to determine the pojo when we will import the table again.
For converting the list of pojo to table format i have used the jtable but i am not able to export all the jtable content to clipboard.
can anyone suggest if i should follow the jtable approach and copy the table to clipboard or any other solution is also available.
Update: as suggested in the comment I tried using the flavours
public class ClipboardTest implements ClipboardOwner {
public static void main(String[] args) {
ClipboardTest clipboardTest = new ClipboardTest();
clipboardTest.copyToClipboard();
//clipboardTest.getFromClipboard();
}
public void copyToClipboard() {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Pojo data = new Pojo("1", "2", "ame2", "2", "2");
MyObjectSelection dataSelection = new MyObjectSelection(data);
StringSelection selection = new StringSelection("testing string");
clipboard.setContents(dataSelection, ClipboardTest.this);
System.out.println("copied to clipboard");
}
public void getFromClipboard() {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable clipboardContent = clipboard.getContents(this);
DataFlavor[] flavors = clipboardContent.getTransferDataFlavors();
System.out.println("flavors.length = " + flavors.length);
for (int i = 0; i < flavors.length; i++) {
System.out.println("flavor[" + i + "] = " + flavors[i]);
}
}
// ClipboardOwner implementation
#Override
public void lostOwnership(Clipboard clipboard, Transferable transferable) {
System.out.println("ClipboardTest: Lost ownership");
}
}
---
myobjectselection.java
public class MyObjectSelection implements Transferable, ClipboardOwner {
private static DataFlavor dmselFlavor = new DataFlavor(Pojo.class,
"Test data flavor");
private Pojo selection;
public MyObjectSelection(Pojo selection) {
this.selection = selection;
}
// Transferable implementation
#Override
public DataFlavor[] getTransferDataFlavors() {
System.out.println("getTransferDataFlavors");
DataFlavor[] ret = { dmselFlavor };
return ret;
}
#Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return dmselFlavor.equals(flavor);
}
#Override
public synchronized Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return this.selection;
} else {
throw new UnsupportedFlavorException(dmselFlavor);
}
}
// ClipboardOwner implementation
#Override
public void lostOwnership(Clipboard clipboard, Transferable transferable) {
System.out.println("MyObjectSelection: Lost ownership");
}
}
--
pojo.java
public class Pojo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String name1;
private String name2;
private String name3;
private String name4;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
public String getName2() {
return name2;
}
public void setName2(String name2) {
this.name2 = name2;
}
public String getName3() {
return name3;
}
public void setName3(String name3) {
this.name3 = name3;
}
public String getName4() {
return name4;
}
public void setName4(String name4) {
this.name4 = name4;
}
public Pojo(String name, String name1, String name2, String name3,
String name4) {
super();
this.name = name;
this.name1 = name1;
this.name2 = name2;
this.name3 = name3;
this.name4 = name4;
}
}
When i am trying to copy the string value to clipboard it is working but when i am trying to copy the pojo then is it not working.
For every flavor you want to support, you must provide methods to encode the object in the specified format.
This means you'll likely need to provide an encoder for plain text, html and CVS to cover the basics.
So, based on your Pojo, I wrote a Transferable that supports:
List (of Pojos)
HTML
CVS
Plain text
And serialised (as an additional to List, but it's essentially the same)
PojoTransferable
public class PojoTransferable implements Transferable {
public static final DataFlavor POJO_LIST_DATA_FLAVOR = new DataFlavor(List.class, "application/x-java-pojo-list;class=java.util.List");
public static final DataFlavor HTML_DATA_FLAVOR = new DataFlavor("text/html", "HTML");
public static final DataFlavor CSV_DATA_FLAVOR = new DataFlavor("text/csv", "CVS");
public static final DataFlavor PLAIN_DATA_FLAVOR = new DataFlavor("text/plain", "Plain text");
public static final DataFlavor SERIALIZED_DATA_FLAVOR = new DataFlavor(Pojo.class, "application/x-java-serialized-object; Pojo");
private static String[] HEADERS = new String[]{"name", "name1", "name2", "name3", "name4"};
private static Pojo POJO_HEADER = new Pojo("name", "name1", "name2", "name3", "name4");
private List<Pojo> pojos;
public PojoTransferable(List<Pojo> pojos) {
this.pojos = pojos;
}
#Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{POJO_LIST_DATA_FLAVOR, HTML_DATA_FLAVOR, CSV_DATA_FLAVOR, SERIALIZED_DATA_FLAVOR, PLAIN_DATA_FLAVOR};
}
#Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
boolean supported = false;
for (DataFlavor mine : getTransferDataFlavors()) {
if (mine.equals(flavor)) {
supported = true;
break;
}
}
return supported;
}
#Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
Object data = null;
if (POJO_LIST_DATA_FLAVOR.equals(flavor)) {
data = pojos;
} else if (HTML_DATA_FLAVOR.equals(flavor)) {
data = new ByteArrayInputStream(formatAsHTML().getBytes());
} else if (SERIALIZED_DATA_FLAVOR.equals(flavor)) {
data = pojos;
} else if (CSV_DATA_FLAVOR.equals(flavor)) {
data = new ByteArrayInputStream(formatAsCVS().getBytes());
} else if (PLAIN_DATA_FLAVOR.equals(flavor)) {
data = new ByteArrayInputStream(formatAsPlainText().getBytes());
} else {
throw new UnsupportedFlavorException(flavor);
}
return data;
}
protected String formatAsCVS(Pojo pojo) {
StringJoiner sj = new StringJoiner(",");
sj.add(pojo.getName());
sj.add(pojo.getName2());
sj.add(pojo.getName3());
sj.add(pojo.getName4());
return sj.toString();
}
public String formatAsCVS() {
StringBuilder sb = new StringBuilder(128);
sb.append(formatAsCVS(POJO_HEADER));
for (Pojo pojo : pojos) {
sb.append(formatAsCVS(pojo));
}
return "";
}
protected Map<Integer, Integer> columnWidthsFor(Pojo pojo) {
Map<Integer, Integer> columnWidths = new HashMap<>();
columnWidths.put(0, pojo.getName().length());
columnWidths.put(1, pojo.getName1().length());
columnWidths.put(2, pojo.getName2().length());
columnWidths.put(3, pojo.getName3().length());
columnWidths.put(4, pojo.getName4().length());
return columnWidths;
}
protected void apply(Map<Integer, Integer> pojoWidths, Map<Integer, Integer> columnWidths) {
for (int index = 0; index < 5; index++) {
int currentWidth = 2;
if (columnWidths.containsKey(index)) {
currentWidth = columnWidths.get(index);
}
int columnWidth = 2;
if (pojoWidths.containsKey(index)) {
columnWidth = pojoWidths.get(index);
}
columnWidths.put(index, Math.max(currentWidth, columnWidth));
}
}
protected String formatAsPlainText(Pojo pojo, String format) {
return String.format(format, pojo.getName(), pojo.getName1(), pojo.getName2(), pojo.getName3(), pojo.getName4());
}
public static String fill(int padding) {
return String.format("%" + padding + "s", "").replace(" ", "-");
}
public String formatAsPlainText() {
Map<Integer, Integer> columnWidths = new HashMap<>();
apply(columnWidthsFor(POJO_HEADER), columnWidths);
for (Pojo pojo : pojos) {
apply(columnWidthsFor(pojo), columnWidths);
}
StringJoiner sjFormat = new StringJoiner("|");
StringJoiner sjSep = new StringJoiner("+");
for (int index = 0; index < 5; index++) {
int currentWidth = 0;
if (columnWidths.containsKey(index)) {
currentWidth = columnWidths.get(index);
}
sjFormat.add(" %-" + currentWidth + "s ");
sjSep.add(fill(currentWidth + 2));
}
sjFormat.add("%n");
sjSep.add("\n");
String seperator = sjSep.toString();
String format = sjFormat.toString();
StringBuilder sb = new StringBuilder(128);
sb.append(formatAsPlainText(POJO_HEADER, format));
for (Pojo pojo : pojos) {
sb.append(seperator);
sb.append(formatAsPlainText(pojo, format));
}
return sb.toString();
}
public String formatAsHTML() {
StringBuilder sb = new StringBuilder(128);
sb.append("<html><body>");
sb.append("<table border='1'>");
sb.append("<tr>");
for (String header : HEADERS) {
sb.append("<th>").append(header).append("</th>");
}
sb.append("</tr>");
for (Pojo pojo : pojos) {
sb.append("<tr>");
sb.append("<td>").append(pojo.getName()).append("</td>");
sb.append("<td>").append(pojo.getName1()).append("</td>");
sb.append("<td>").append(pojo.getName2()).append("</td>");
sb.append("<td>").append(pojo.getName3()).append("</td>");
sb.append("<td>").append(pojo.getName4()).append("</td>");
sb.append("</tr>");
}
sb.append("</table>");
return sb.toString();
}
}
Test...
Then I wrote a really simple test...
List<Pojo> pojos = new ArrayList<>(25);
pojos.add(new Pojo("one", "two", "three", "four", "five"));
PojoTransferable pt = new PojoTransferable(pojos);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.setContents(pt, new ClipboardOwner() {
#Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
System.out.println("Lost");
}
});
try {
Object data = cb.getData(PojoTransferable.POJO_LIST_DATA_FLAVOR);
if (data instanceof List) {
List listOfPojos = (List)data;
System.out.println("listOfPojos contains " + listOfPojos.size());
for (Object o : listOfPojos) {
System.out.println(o);
}
}
} catch (UnsupportedFlavorException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
When I run it, it prints out...
listOfPojos contains 1
test.Pojo#5b480cf9
which tells use that the List flavor worked (for our needs), but then I opened up a text edit, Word and Excel and pasted the contents into them...
nb: Excel used the HTML formatting, go figure

Java Table model data disappearing

I've run into a very strange issue. I wrote a model for a table and for the most part it works perfect. When I update the row data via the setValueAt function it works and the data in the ArrayList is updated and complete. But when i run the getTotalPay function the same array is empty and i get a null value exception.
I've rewrote the code a number of different ways and tried a couple of different approaches to the problem but the problem persists. I know i've done something wrong i just cannot figure out what the problem is. After search the net and not finding a solution i'm posting this.
Things i've tried are replacing the variable, putting the data into two different variables.
Here is the model
public class WeeklyWagesModel extends AbstractTableModel {
private String defaultValue = "Click to edit";
private String[] headings = { "Date From", "Date To", "Wages", "Pay Date" };
private ArrayList<String[]> theData = new ArrayList<String[]>();
public WeeklyWagesModel() {
theData.add(createDefaultRow());
}
#Override
public int getColumnCount() {
return headings.length;
}
public String getColumnName(int col) {
return headings[col];
}
#Override
public int getRowCount() {
return theData.size();
}
#Override
public Object getValueAt(int row, int col) {
return theData.get(row)[col];
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object value, int row, int col) {
theData.get(row)[col] = String.valueOf(value);
if (row == (theData.size() - 1)) {
theData.add(createDefaultRow());
}
fireTableDataChanged();
}
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
public ArrayList<String[]> getData() {
return theData;
}
public String getTotalPay() {
System.out.println("Total Pay: " + theData.size());
BigDecimal total = BigDecimal.ZERO;
for (String[] eachRow : theData) {
if (StringUtils.isNumeric(eachRow[2])) {
total.add(new BigDecimal(eachRow[2]));
}
}
return NumberFormat.getCurrencyInstance().format(total);
}
public String getTotalWeeks() {
return "";
}
private String[] createDefaultRow() {
String[] newRow = new String[headings.length];
for (int i = 0; i < newRow.length; i++) {
newRow[i] = defaultValue;
}
return newRow;
}
}

JTable display data in arrayList

coding in Java Eclipse here. Making a booking system. The idea is to take the info from the database ,store it in the ArrayList and from the ArrayList show it in the GUI through JTable. Having some problems with the last part and just can't figure it out..
ArrayList:
import java.util.ArrayList;
public class CarList
{
private ArrayList<Car> cars;
public CarList()
{
cars = new ArrayList<Car>();
}
public int getNumberOfCars()
{
return cars.size();
}
public Car getCar(String CarMake)
{
for (int i = 0; i < cars.size(); i++)
{
if (cars.get(i).getMake() == CarMake)
{
return cars.get(i);
}
}
return null;
}
public int size()
{
return cars.size();
}
public void add(Car car)
{
if (!this.ModelExists(car.getModel()))
{
cars.add(car);
}
}
public Boolean ModelExists(String Model)
{
for (Car c : cars)
{
if (c.getModel().equals(Model))
{
return true;
}
}
return false;
}
public void remove(String CarMake)
{
for (int i = 0; i < cars.size(); i++)
{
if (cars.get(i).getMake() == CarMake)
{
cars.remove(i);
}
}
}
public String toString()
{
String returnStr = "";
for (int i = 0; i < cars.size(); i++)
{
Car temp = cars.get(i);
returnStr += temp + "\n";
}
return returnStr;
}
}
Adapter to get the data from the db to the arraylist:
public CarList getAllCars()
{
MyDatabase myDB = new MyDatabase();
CarList cars = new CarList();
try
{
myDB.openMySQLDatabase("db", "root", "");
String sql = "SELECT Make, Model, LicenseNumber, Color, Year," +
"HorsePower, TimeUntilService, ConsumptionPerKm," +
"NumberOfSeats, NumberOfDoors, Transmission, ClimateControl,Price "
+ "FROM cars";
System.out.println(sql);
Object[][] result = myDB.returnSQLQueryResult(sql);
for (int rows = 0; rows < result.length; rows++)
{
System.out.println("result row");
String make = (String) result[rows][0];
String model = (String) result[rows][1];
String licenseNumber = (String) result[rows][2];
String color = (String) result[rows][3];
int year = (int) result[rows][4];
String horsePower = (String) result[rows][5];
String timeUntilService = (String) result[rows][6];
String consumptionPerKm = (String) result[rows][7];
int numberOfSeats = (int) result[rows][8];
int numberOfDoors = (int) result[rows][9];
String transmission = (String) result[rows][10];
String climateControl = (String) result[rows][11];
int price = (int) result[rows][12];
cars.add(new Car(make, model, licenseNumber, color, year, horsePower,
timeUntilService, consumptionPerKm, climateControl, numberOfSeats, numberOfDoors, transmission, climateControl, price));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
finally
{
try
{
myDB.closeDatabase();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
System.out.println(cars.size());
return cars;
}
JTable:
panelBottomRight = new JPanel();
panelBottomRight.setLayout(new BorderLayout());
panelBottomRight.setBorder(new TitledBorder(BorderFactory
.createLineBorder(Color.black), "[Cars]", 2, 0));
tableBottomRightCenter = new JPanel();
tableBottomRightCenter.setLayout(new BorderLayout());
String[] columnNames = { "Make", "Model", "LicenseNumber", "Color",
"Year", "HorsePower", "TimeUntilService",
"ConsumptionPerKm", "NumberOfSeats", "NumberOfDoors",
"ClimateControl" };
CarList cars= new CarList();
String[][] data = {};
// Create table with database data
tableBottomR = new JTable(data, columnNames);
tableBottomR.setAutoCreateRowSorter(true);
tableBottomR.getTableHeader().setReorderingAllowed(false);
tableBottomR.setModel(new DefaultTableModel(data, columnNames)
{
#Override
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return false;
}
});
tableBottomRightCenter.add(tableBottomR, BorderLayout.CENTER);
scrollPane2 = new JScrollPane(tableBottomR);
scrollPane2
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
tableBottomRightCenter.add(scrollPane2);
panelBottomRight.add(tableBottomRightCenter, BorderLayout.CENTER);
There are a few things that jump out.
In you CarList, the getCar method is comparing object references instead of comparing the contents of the String
For String comparison, you should be using String#equals, for example...
public Car getCar(String CarMake) {
for (int i = 0; i < cars.size(); i++) {
//if (cars.get(i).getMake() == CarMake) {
if (cars.get(i).getMake().equals(CarMake)) {
return cars.get(i);
}
}
return null;
}
You don't seem to be using the getAllCars method to populate the table model, but are simply creating a series of empty table models.
Personally, I'm not a fan of DefaultTableModel, especially given the fact that you have a Car object and CarList object, i would require you to undo all this work to use it, instead, I prefer to create my own, specialised, implementation, which allows me to provide greater control, for example...
public class CarModel extends AbstractTableModel {
private String[] columnNames = { "Make", "Model", "LicenseNumber", "Color",
"Year", "HorsePower", "TimeUntilService",
"ConsumptionPerKm", "NumberOfSeats", "NumberOfDoors",
"ClimateControl" };
private CarList carList;
public CarModel(CarList list) {
carList = list;
}
public CarList getCarList() {
return carList;
}
#Override
public int getRowCount() {
return getCarList().getNumberOfCars();
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public String getColumnName(int column) {
return columnNames[column];
}
#Override
public Class<?> getColumnClass(int columnIndex) {
Class type = String.class;
switch (columnIndex) {
case 0:
type = String.class;
break;
// ...etc...
}
return type;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Car car = getCarList().getCarAt(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = car.getMake();
break;
//...etc...
}
return value;
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
This, obviously, will require you to add a getCarAt(int) method to your CarList to return the Car at the given index.
Then, you simply need to extract the data from the database and apply the resulting CarList to the table model, for example...
CarList carList = getAllCars();
CarTableModel model = new CarTableModel(carList);
Then, you just need to add it to your UI, for example...
JTable table = new JTable(model);
add(new JScrollPane(table));
Take a look at How to use tables for more details and examples...

JTable repetitive rows

My jtable should read a text file and show them.
It reads all data correctly, But just show last line record in file, in its all rows repetitive.
Where is my mistake?
My text file:
uiui 898 666999
vvvv 6666 7777
hfsn 5356 56
ds 232 2212
bbnn 2013 211
My AllBooks Class:
public class AllBooks extends AbstractTableModel{
BookInformation Binfos=new BookInformation();
String[] Bcol=new String[]{"Name","Date","Id"};
List<BookInformation> Bdata=new ArrayList<BookInformation>();
public AllBooks(){
try{
FileReader fr=new FileReader("AllBookRecords.txt");
BufferedReader br=new BufferedReader(fr);
String line;
while( (line=br.readLine()) !=null){
Bdata.add(initializeUserInfos(line));
}
br.close();
}
catch(IOException ioe){
}
}
public BookInformation initializeUserInfos(String str){
System.out.println(str);
String[] bookCellArray=str.split(" ");
Binfos.setBookName(bookCellArray[0]);
Binfos.setBookDate(bookCellArray[1]);
Binfos.setBookID(bookCellArray[2]);
return Binfos;
}
#Override
public String getColumnName(int col){
return Bcol[col];
}
#Override
public int getRowCount() {
if(Bdata !=null){
return Bdata.size();
}
else{
return 0;
}
}
#Override
public int getColumnCount() {
return Bcol.length;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
BookInformation binfo=Bdata.get(rowIndex);
Object value;
switch(columnIndex){
case 0:
value=binfo.getBookName();
break;
case 1:
value=binfo.getBookDate();
break;
case 2:
value=binfo.getBookID();
break;
default :
value="...";
}
return value;
}
}
My AllBooksM Class:
public class AllBooksM {
final AllBooks rbftl=new AllBooks();
final JFrame Bframe=new JFrame("All Book List");
final JTable Btable=new JTable(rbftl);
public AllBooksM(){
JPanel Bpanel=new JPanel();
Bpanel.setLayout(new FlowLayout());
JScrollPane sp=new JScrollPane(Btable);
Bpanel.add(sp);
Bframe.add(Bpanel);
Btable.setAutoCreateRowSorter(true);
Bframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Bframe.setBounds(300, 60, 550, 550);
Bframe.setResizable(false);
Bframe.setVisible(true);
}
public static void main(String[] args){
new AllBooksM();
}
}
My BookInformation Class:
public class BookInformation {
private String BookName;
private String BookDate;
private String BookID;
public String getBookName() {
return BookName;
}
public void setBookName(String book_name) {
this.BookName = book_name;
}
public String getBookDate() {
return BookDate;
}
public void setBookDate(String book_date) {
this.BookDate = book_date;
}
public String getBookID() {
return BookID;
}
public void setBookID(String Book_id) {
this.BookID = Book_id;
}
}
Thanks!
You're using the same BookInformation object with each iteration of the while loop and instead need to create a new one with each iteration. Else that same object will be held by all rows of the table model causing the same information will be displayed on every row.
For instance you can solve it by doing something like this.
public BookInformation initializeUserInfos(String str){
System.out.println(str);
String[] bookCellArray=str.split(" ");
// create and use a local BookInformation variable and object:
BookInformation bInfos = new BookInformation(); // *****
bInfos.setBookName(bookCellArray[0]);
bInfos.setBookDate(bookCellArray[1]);
bInfos.setBookID(bookCellArray[2]);
return bInfos;
}

Getting column names from an AbstractTableModel

I can't figure out something using the constructor JTable(TableModel dm).
I'm using a LinkedList to manage my data so, to display it, I extended AbstractTableModel:
public class VolumeListTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private LinkedList<Directory> datalist;
private Object[] columnNames= {"ID", "Directory", "Wildcard"};
public VolumeListTableModel(){
}
public void setDatalist(LinkedList<Directory> temp){
this.datalist = temp;
}
public LinkedList<Directory> getDatalist(){
return (LinkedList<Directory>) this.datalist.clone();
}
public Object[] getColumnNames() {
return this.columnNames;
}
#Override
public int getColumnCount() {
return Directory.numCols;
}
#Override
public int getRowCount() {
return this.datalist.size();
}
#Override
public Object getValueAt(int row, int col) {
Directory temp = this.datalist.get(row);
switch(col){
case 0:
return temp.getId();
case 1:
return temp.getPath();
case 2:
return temp.getWildcard();
default:
return null;
}
}
I'm doing something wrong because when I run my GUI I get column names labeled A,*B*,C.
There is no method in AbstractTableModel called getColumnNames, so I believe your method is being ignored. The actual method you want to override is the getColumnName method.
Try adding this method to your VolumeListTableModel class
public String getColumnName(int column) {
return columnNames[column];
}
You need to override the getColumnName method which in your case will simply
return columnNames[column];
You have to Override this method :
public String getColumnName(int column)
AbstractTableModel does not have a getColumnNames method, but it is easy to implement:
public class VolumeListTableModel extends AbstractTableModel {
// [...]
public String[] getColumnNames() {
String[] columnNames = new String[this.getColumnCount()];
for (int i = 0, columnCount = this.getColumnCount(); i < columnCount; i++) {
columnNames[i] = this.getColumnName(i);
}
return columnNames;
}
// [...]
}

Categories

Resources