JTable repetitive rows - java

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;
}

Related

JTableModel rows wrong added

this is my TableModel:
public class ScheduledRecordsTableModel extends AbstractTableModel {
private String[] headers = {"Interpret", "Titel"};
private List<ScheduledRecord> scheduledRecords;
public ScheduledRecordsTableModel(List<ScheduledRecord> recordsList) {
super();
this.scheduledRecords = recordsList;
}
#Override
public int getRowCount() {
return scheduledRecords.size();
}
#Override
public int getColumnCount() {
return 2;
}
#Override
public String getColumnName(int column) {
return headers[column];
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
ScheduledRecord r = scheduledRecords.get(rowIndex);
switch (columnIndex) {
case 0:
return r.getActor();
case 1:
return r.getTitle();
default:
return null;
}
}
public void addRow(ScheduledRecord r) {
ScheduledRecord toAdd = null;
for (Iterator<ScheduledRecord> recordIterator = scheduledRecords.iterator(); recordIterator.hasNext(); ) {
ScheduledRecord record = recordIterator.next();
if (record.equals(r)) {
throw new IllegalArgumentException("Scheduled Record " + r.toString() + " already exists");
} else {
toAdd = record;
}
}
scheduledRecords.add(toAdd);
}
public void removeRow(ScheduledRecord r) {
scheduledRecords.remove(r);
fireTableDataChanged();
}
public ScheduledRecord getScheduledRecordFromIndex(int index) {
return scheduledRecords.get(index);
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
here is how i add new entries:
private void saveScheduledRecord() {
if (checkInputValues()) {
WebradioPlayer.addScheduledRecord(new ScheduledRecord(titleField.getText(), artistField.getText()));
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "Please enter a valid artist/title", "Invalid input", JOptionPane.ERROR_MESSAGE);
throw new IllegalArgumentException("artist or title input did not match the specifications");
}
}
and here the addScheduledRecord method:
public static synchronized boolean addScheduledRecord(ScheduledRecord record) {
RecorderController.getInstance().addScheduledRecord(record);
Gui.getInstance().getRecorderTab().getScheduledRecordsWindow().getTable().getScheduledRecordsTableModel().addRow(record);
Gui.getInstance().getRecorderTab().getScheduledRecordsWindow().getTable().getScheduledRecordsTableModel().fireTableDataChanged();
databaseConnector.addScheduledRecord(record);
return true;
}
If i add an entry, the 'previous' one is added to the table, however if i close the table and open it again it is all correct. RecorderController just holds an own list for other purposes (this list is modified in another way)
Does anyone see my mistake here?
This should fix the addRow method (no need to iterate, while there is a contains method):
public void addRow(ScheduledRecord r) {
if (scheduledRecords.contains(r)) {
throw new IllegalArgumentException("Scheduled Record " + r.toString() + " already exists");
}
scheduledRecords.add(r);
}

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

Initialize column names in AbstractTableModel

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];
}

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...

Add Row to JTable and File

I use this method to add a new row to my jtable and file too.
But when i clicked to add button, That new record added to jtable, but when i see the text file, I found something like this:
myproject.Library.BookInformation#9899472
where is my mistake?
My code:
public class MainS extends JFrame{
final AllBooks allBooks=new AllBooks();
final JTable Btable=new JTable(allBooks);
public MainS(){
JButton AddBookButton=new JButton("Add New Book");
AddBookButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
AddDialogS adddialog=new AddDialogS(MainS.this);
adddialog.setVisible(true);
BookInformation B_info=adddialog.getBookInfos();
if(B_info != null){
allBooks.AddRow(B_info);
}
}
});
JPanel Bpanel=new JPanel();
Bpanel.setLayout(new FlowLayout());
JScrollPane sp=new JScrollPane(Btable);
Bpanel.add(sp);
Bpanel.add(AddBookButton);
this.add(Bpanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(300, 60, 550, 550);
this.setVisible(true);
}
public static void main(String[] args){
new MainS();
}
}
second class for add new record:
public class AddDialogS extends JDialog{
BookInformation bookinform=new BookInformation();
public AddDialogS(JFrame owner){
super(owner,"Add New Book!", true);
JButton OkButton=new JButton("Ok");
final JTextField nameTF=new JTextField(10);
JLabel namelbl=new JLabel("bookname");
final JTextField dateTF=new JTextField(10);
JLabel datelbl=new JLabel("bookDate");
final JTextField idTF=new JTextField(10);
JLabel IDlbl=new JLabel("bookId");
OkButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
bookinform.setBookName(nameTF.getText().trim());
bookinform.setBookDate(String.valueOf(dateTF.getText().trim()));
bookinform.setBookID(String.valueOf(idTF.getText().trim()));
AddDialogS.this.dispose();
}
});
JPanel panel=new JPanel(new FlowLayout());
panel.add(OkButton);
panel.add(nameTF);
panel.add(dateTF);
panel.add(idTF);
panel.add(namelbl);
panel.add(datelbl);
panel.add(IDlbl);
this.add(panel);
this.setBounds(10, 30, 400, 500);
}
public BookInformation getBookInfos(){
return bookinform;
}
}
My table model Class:
public class AllBooks extends AbstractTableModel{
BookInformation Binfos1=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(initializeBookInfos(line));
}
br.close();
}
catch(IOException ioe){
}
}
public static BookInformation initializeBookInfos(String str){
BookInformation Binit=new BookInformation();
String[] bookCellArray=str.split(" ");
Binit.setBookName(bookCellArray[0]);
Binit.setBookDate(bookCellArray[1]);
Binit.setBookID(bookCellArray[2]);
return Binit;
}
public void AddRow(BookInformation bookinfo){
if(AddToFile(bookinfo)){
Bdata.add(bookinfo);
fireTableRowsInserted(Bdata.size()-1, Bdata.size()-1);
}
else{
JOptionPane.showMessageDialog(null, "Unable Add To File"+bookinfo.getBookName());
}
}
public boolean AddToFile(String bookinfos){
try{
PrintWriter Bpw=new PrintWriter(new FileOutputStream("AllBookRecords.txt",true));
Bpw.println(bookinfos);
return true;
}
catch(IOException ioe){
return false;
}
}
#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 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 for help.
Looks like what you get in the file is a result of method toString() invocation on an object of this class: myproject.Library.BookInformation.
So the quickest fix in your case would be to override toString() method for BookInformation to return what you need.
public String toString() {
return getBookName(); // Or whatever you see fit.
}
Even if later you'll change your code not to rely on toString() a meaningful implementation is not going to hurt.
Unlike in another answer you do NOT have to change code for AddToFile if you override toString(). However, if you don't modify code for BookingInformation, then you would have to craft string value when you call AddToFile similar to what was suggested:
AddToFile(bookinfo.getBookName()) // Or whatever you see fit.
Another even better way would be to modify AddToFile method to accept BookingInformation as a parameter.
public boolean AddToFile(BookingInformation bookinfos){
try{
PrintWriter Bpw=new PrintWriter(new FileOutputStream("AllBookRecords.txt",true));
Bpw.println(bookinfos.getBookName()); // Or whatever you see fit.
return true;
}
catch(IOException ioe){
return false;
}
}
In your BookInformation class add a toString method like this
public String toString(){
return BookID + " " + BookDate + " " + BookName;
}
and then call AddToFile() like this
AddToFile(bookinfo.toString())
from AddRow method.
There are any number solutions you could try.
You could modify your AddToFile method to write to format the properties of the object as you need it.
This ensures that the format that the model writes the file in is what the model expects when it reads it back it.
public boolean AddToFile(BookInformation bookinfos){
try{
PrintWriter Bpw=new PrintWriter(new FileOutputStream("AllBookRecords.txt",true));
StringBuilder sb = new StringBuilder(64);
sb.append(bookinfos.getBookID()).append(";");
sb.append(bookinfos.getBookName()).append(";");
sb.append(bookinfos.getBookID()).append(";");
Bpw.println(sb.toString());
return true;
}
catch(IOException ioe){
return false;
}
}
You can of course define your own format and delimiters. This has the benefit of allowing the model to control it's own format and does not effect any other part of the program, like using toString() would.
Alternativly, you could write a read/write method in the BookInformation class, passing in the PrintWriter and allowing the BookInformation to determine the format that the data should be maintained in.
This has the benefit of allowing other parts of the program to save the object in a uniform manner and if the format ever changes (ie you add new fields), it changes in one location

Categories

Resources