I have a problem when storing data (written to a GUI) in a JTable I have already created. The problem is, when I click the add button in the GUI, instead of displaying it in the JTable, it opens another window and displays it there. So it adds, but it doesnt display it to the table. Heres the part of the code that display the info in another window:
import java.lang.Object;
import java.io.*;
import java.util.*;
public class MemberManager {
File custFile;
private Member m1 =
new Member("M0001", "Mark", "Abela", "13/05/90", "121 Kent Street", "21469432", "99456209");
private Member m2 =
new Member("M0002", "John", "Sant", "25/11/84", "55 Golden Road", "21226932", "79513625");
private ArrayList<Member> memMemb = new ArrayList<Member>();
public MemberManager(){
memMemb.add(m1);
memMemb.add(m2);
}
public String[][] getMembers(){
int pos = 0;
//Creating the table with the players data
String data [][] = new String[memMemb.size()][8];
for (Object obj : memMemb){
Member mem = (Member)(obj);
data[pos][0] = mem.getID();
data[pos][1] = mem.getName();
data[pos][2] = mem.getSurname();
data[pos][3] = mem.getDob();
data[pos][4] = mem.getAddress();
data[pos][5] = mem.getTel();
data[pos][6] = mem.getMob();
}
return data;
}
public void addMember(String id, String name, String surname, String dob, String address, String tel, String mob){
Member tempMem = new Member();
tempMem.setID(id);
tempMem.setName(name);
tempMem.setSurname(surname);
tempMem.setDob(dob);
tempMem.setAddress(address);
tempMem.setTel(tel);
tempMem.setMob(mob);
memMemb.add(tempMem);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream("Member Data");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(memMemb);
oos.flush();
}
catch(IOException e) {
e.printStackTrace();
}
finally {
try {
fos.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
try {
// read back
fis = new FileInputStream("Member Data");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
memMemb = (ArrayList<Member>) obj;
ArrayList<Member> listFromFile = (ArrayList) obj;
for (Member member: listFromFile) {
System.out.println(member.toString());
}
}catch (IOException io){
System.err.println("Cannot access or write to file");
}catch (Exception e){
e.printStackTrace();
}
finally {
try {
fis.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
The JTable im using is the default table model thats in a different package. I used mouseListener on the button to add the data... here's part of the code...:-
memAddOk.addMouseListener(this);
memAddExit.addMouseListener(this);
}
public void mouseClicked(MouseEvent e){
if (e.getSource().equals(memAddOk)){
id = textBox[0].getText();
name = textBox[1].getText();
surname = textBox[2].getText();
dob = textBox[3].getText();
address = textBox[4].getText();
tel = textBox[5].getText();
mob = textBox[6].getText();
MemberManager tempData = new MemberManager();
tempData.addMember(id, name, surname, dob, address, tel, mob);
} else
if (e.getSource().equals(memAddExit)) {
this.dispose();
}
}
How do I get this code to store in the JTable?
The problem is, when I click the add button in the GUI, instead of displaying it in the JTable, it opens another window and displays it there.
It's just doing what you tell it to do:
public void mouseClicked(MouseEvent e){
if (e.getSource().equals(memAddOk)){
id = textBox[0].getText();
// .... etc..
// !!!! *** Here you create a new MemberManager object *** !!!!
MemberManager tempData = new MemberManager();
tempData.addMember(id, name, surname, dob, address, tel, mob);
} else
//... etc..
Solution:
Don't create a new MemberManager object in your listener.
Instead make sure that your listener class has a reference to the currently displayed MemberManager object so you can add a Patient to it.
You often get this reference by passing a MemberManager into the listener's constructor or via a setter method parameter.
Again, don't use MouseListeners on JButtons but instead use ActionListeners as that's what they're specifically built for. MouseListeners can cause strange behaviors including but not limited to a button that still works despite being disabled.
1) use SwingWorker or Runnable#Thread for loading value on the background from File IO or Database
2) define JTable and DefaultTableModel once time, start background task for loading data from File or Database, simple example
Related
My program is a simple chatroom. the values written from the user by the text field in the registration section and creates an account. I have a class called "Account" that takes the same values in the input and it contains the same fields. All accounts need to be saved and loaded for later implementation.
I used the code below (this is part of the program code) but there is a problem. I printed the values after loading and found that it was reporting null values. Then after adding a new account to the array list all the values in the list were changed to the added value. For example, I create and save an account with name "a" and email "a", then run the program again.
Adding the account with name and email "B" here prints two accounts B. what is the problem?
String email= t4.getText();
String password=t5.getText();
String name=t1.getText();
String family=t2.getText();
String phoneNumber=t3.getText();
try {
FileInputStream f = new FileInputStream("accounts.txt");
ObjectInputStream obj = new ObjectInputStream(f);
int size=obj.readInt();
for (int i = 0; i <size ; i++) {
accounts.add((Account)obj.readObject());
}
} catch (Exception e){
e.printStackTrace();
}
for (int i = 0; i <accounts.size() ; i++) {
System.out.println(accounts.get(i).name+" "+accounts.get(i).email);
}
Account account = new Account(name,family,phoneNumber,email,password);
accounts.add(account);
try{
FileOutputStream f=new FileOutputStream("accounts.txt",false);
ObjectOutputStream obj=new ObjectOutputStream(f);
obj.writeInt(accounts.size());
for (Account a:accounts) {
obj.writeObject(a);
}
} catch (Exception e){
e.printStackTrace();
}
for (int i = 0; i <accounts.size() ; i++) {
System.out.println(accounts.get(i).name+" "+accounts.get(i).email);
}
//class account
class Account implements Serializable {
static String name, family, phoneNumber, email, password;
static ArrayList<Post> posts = new ArrayList();
static ArrayList<Account> following = new ArrayList();
static ArrayList<Account> follower = new ArrayList();
Account(String n, String f, String pn, String e, String ps) {
name = n;
family = f;
phoneNumber = pn;
email = e;
password = ps;
}
void follow(Account user) {
following.add(user);
user.follower.add(this);
}
void post(String data) {
Post post = new Post(data, new Date(), this);
posts.add(post);
for (Account account : follower) {
account.posts.add(post);
}
}
}
All the members of the Account class are static, meaning that they belong to the class instead of a specific instance. Make them non-static and you should be good to go.
As a part of my assignment I had to store objects of an array in a flat-file and retrieve them when certain criteria was met. I can save the objects fine but when retrieving them I have an issue with getting more than one value, I understand what is going wrong but I am struggling to find a solution. Here is the concept of whats happening.
Button no 10,A (R1S10 in the code)is my testing button, When I click it it creates an event that I will show below.
Click event for button 10A -
private void R1S10ActionPerformed(java.awt.event.ActionEvent evt) {
seats.add(seat1);
if (R1S10.getBackground().equals(Color.red) &&(IsSeatBooked().equals("true"))){
Component frame = null;
JOptionPane.showMessageDialog(frame, "Seat UnBooked");
seat1.setBooked("false");
seat1.setName("");
R1S10.setBackground(Color.yellow);
try {
reader();
writer();
//String booked = "true";
//Pass String booked into csv file
} catch (IOException ex) {
Logger.getLogger(SeatingPlan.class.getName()).log(Level.SEVERE, null, ex);
}
}
else{
Component frame = null;
String name = JOptionPane.showInputDialog(frame, "Please enter name of Customer booking");
if (name.isEmpty()) {
JOptionPane.showMessageDialog(frame, "No value entered");
} else if (name != null) {
seat1.setName(name);
seat1.setBooked("true");
R1S10.setBackground(Color.red);
JOptionPane.showMessageDialog(frame, "Your Booking has been placed");
try {
writer();
reader();
//String booked = "true";
//Pass String booked into csv file
} catch (IOException ex) {
Logger.getLogger(SeatingPlan.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Followed by the screen below -
Outcome -
And when the button is pressed again -
I am using three methods in this SeatingPlan.java - writer(),reader() and IsSeatBooked().
SeatingPlan -
public class SeatingPlan extends javax.swing.JFrame {
/**
* Creates new form SeatingPlan
*/
String seatNo, name, bookedSeat;
FileWriter fileWriter = null;
List<Seat> seats = new ArrayList<Seat>();
//Seat Object Declaration
Seat seat1 = new Seat("R1S10","","false");
Seat seat2 = new Seat("R1S9", "", "false");
String fileName = "seat.csv";
writer -
public void writer() throws IOException {
//Delimiter used in CSV file
final String NEW_LINE_SEPARATOR = "\n", COMMA_DELIMITER = ",";
//CSV file header
final String FILE_HEADER = "seatID,name,booked";
//fileName = System.getProperty("user.home") + "/seat.csv";
try {
fileWriter = new FileWriter(fileName);
//Write the CSV file header
fileWriter.append(FILE_HEADER.toString());
//Add a new line separator after the header
fileWriter.append(NEW_LINE_SEPARATOR);
//Write a new student object list to the CSV file
for (Seat seat : seats) {
fileWriter.append(String.valueOf(seat.getSeatID()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(seat.getName());
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(seat.isBooked());
fileWriter.append(NEW_LINE_SEPARATOR);
}
System.out.println("CSV file was created successfully !!!");
} catch (Exception e) {
System.out.println("Error in CsvFileWriter !!!");
e.printStackTrace();
} finally {
fileWriter.flush();
fileWriter.close();
}
}
reader -
public void reader() {
//Delimiter used in CSV file
final String COMMA_DELIMITER = ",";
//Student attributes index
final int SEAT_ID_IDX = 0;
final int SEAT_NAME_IDX = 1;
final int SEAT_BOOKED = 2;
//private static final int STUDENT_LNAME_IDX = 2;
BufferedReader fileReader = null;
try {
//Create a new list of student to be filled by CSV file data
List<Seat> seats = new ArrayList<>();
String line = "";
//Create the file reader
fileReader = new BufferedReader(new FileReader(fileName));
//Read the CSV file header to skip it
fileReader.readLine();
//Read the file line by line starting from the second line
while ((line = fileReader.readLine()) != null) {
//Get all tokens available in line
String[] tokens = line.split(COMMA_DELIMITER);
if (tokens.length > 0) {
//Create a new seat object and fill his data
Seat seat = new Seat(tokens[SEAT_ID_IDX],
tokens[SEAT_NAME_IDX], tokens[SEAT_BOOKED]);
seats.add(seat);
seatNo = tokens[SEAT_ID_IDX];
//System.out.println("Seat Number: " + seatNo);
bookedSeat = tokens[SEAT_BOOKED];
}
}
//Print the new student list
for (Seat seat : seats) {
System.out.println(seat.toString());
}
} catch (Exception e) {
System.out.println("Error in CsvFileReader !!!");
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
System.out.println("Error while closing fileReader !!!");
e.printStackTrace();
}
}
}//end reader
SeatingPlan - This if where I have tried to have the arguments controlling the outcome but IsBooked is colliding when multiple seats are selected.
public SeatingPlan() throws IOException {
setVisible(true);
initComponents();
//reader();
ColourSectionGold();
ColourSectionBronze();
reader();
if(R1S10.getBackground().equals(Color.yellow) && (IsSeatBooked().equals("true"))){ R1S10.setBackground(Color.red);}
//if(R1S9.getBackground().equals(Color.yellow) && (IsSeatBooked().equals("true2"))){ R1S9.setBackground(Color.red);}
}
IsSeatBooked -
public String IsSeatBooked(){
return bookedSeat;
}//end IsSeatBooked
Im using the method above as my argument to see whether a seat is booked or not, but when a new seat is click it sets the whole value of 'bookedSeat' - which leaves the system not working correctly. I understand the code is not very efficient but is there any temporary fix for this problem, if I have explained it correctly.
Also I will include my class for Seat -
public class Seat {
private String seatID;
private String booked;
private String name;
private int price;
public Seat(String seatID,String name,String booked){
this.seatID = seatID;
this.booked = "";
this.name = name;
this.price = price;
}
public String getSeatID() {
return seatID;
}
public void setSeatID(String seatID) {
this.seatID = seatID;
}
public String isBooked() {
return booked;
}
public void setBooked(String booked) {
this.booked = booked;
}
public String getStatus(){
return booked;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setPrice() {
this.price = price;
}}//end class Seat
And a look at the CSV file that is created -
I wish to be able to click more than one button and save its state, Button 10 works fine at the moment, but as IsBooked only has one value at a time it clashes.
If you took the time to check this out, I appreciate it. Any constructive criticism is helpful and any ideas would be great!
Thanks,
Paddy.
Too much code to look at to see exactly what you are doing.
Instead of using your csv file, you could create a Properties file. The Propertiesfile will store the data in the form of:
key:data
So in your case the key would be the id: A1, A2... and the data would be the name of the person who booked the seat.
So the file would start out as empty. When you create the GUI you would create a loop that checks each id to see if an entry is found in the Properties field. If it is found then you display the seat as taken, otherwise it is empty.
Then whenever you want to book a seat you just use the setProperty(...) method.
The Properties class has load(...) and store(...) methods.
So the Properties class allows you to easily manage a flat file database with minimal effort.
Note, you would never have variable names like R1S10. That would requirement 100 different variables with if/else statements. Instead you would extend JButton and pass in the row and seat as parameters the button. Then in the ActionListener for the button you can access the row/seat information to built the ID used as the key for the properties file.
Edit:
Couldn't quite make the loop that checks if the ID is in the properties file.
If the property is null, the seath is empty.
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Properties properties = new Properties();
properties.setProperty("A2", "Smith");
properties.setProperty("C3", "Jones");
String[] rows = { "A", "B", "C", "D" };
int seats = 4;
for (int row = 0; row < rows.length; row++)
{
for (int seat = 1; seat <= seats; seat++)
{
String key = rows[row] + seat;
String property = properties.getProperty( key );
System.out.println(key + " : " + property);
}
}
}
}
I am creating my first GUI its a phonebook where users can add, show all, and delete contacts stored in a txt file. My problem here is I'm already able to write the datas to the txt file but I dont know to how delete a contact. Sorry guys newbee here.
This is how I put the data in a txt file and read it back.
private void ADD_ActionPerformed(java.awt.event.ActionEvent evt) {
inputs ++;
String[] name;
String[] number;
String[] email;
name = new String[inputs];
number = new String[inputs];
email = new String[inputs];
//arrays the inputs
for (int i = 0; i < inputs; i++) {
name [i] = name_field.getText();
number[i] = number_field.getText();
email [i] = email_field.getText();
}
name_field.setText("");
number_field.setText("");
email_field.setText("");
try (PrintWriter write = new PrintWriter(new BufferedWriter(new FileWriter("database.txt", true)))){
for (int i = 0; i < 1; i++) {
write.write("Name: "+name[i] + "\nNumber: " + number[i] + "\nEmail: " + email[i] +"\n\n");
}
} catch (IOException e) {
System.err.println("ERROR");
}
}
private void SHOW_ActionPerformed(java.awt.event.ActionEvent evt) {
try {
File f = new File("database.txt");
Scanner sc = new Scanner(f);
while (sc.hasNextLine()) {
String data = sc.nextLine();
contact_area.append("\n " + data);
}
} catch (IOException e) {
System.out.println("ERROR");
}
}
private void DELETE_ActionPerformed(java.awt.event.ActionEvent evt) {
// :( i dunno how to start
}
Unless you want to write code to search through the text file and delete that line, the easiest way to do it would be to read the entire .txt file, and add each contact to an ArrayList so that the user can add, edit, and delete contacts as they choose.
Easiest way to do that would be to make a class called "Contact" which contains a name, number, and email in each instance. Then you can manage each contact individually in the ArrayList. Here is what it would look like:
package contactbook;
public class Contact {
private String name, number, email;
public Contact(String name, String number, String email){
this.name = name;
this.number = number;
this.email = email;
}
//put setters and getters here
}
Then, when you start your program create an ArrayList like so:
ArrayList<Contact> ListOfContacts = new ArrayList<>();
//Read the text file here and create a new contact for each one read
//Display all the existing contacts on the GUI
After that, your ActionListener for the button that adds a contact would look like this:
ActionListener forAddButton = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
name = name_field.getText();
number = number_field.getText();
email = email_field.getText();
Contact newContact = new Contact(name, number, email);
ListOfContacts.add(newContact);
}
}
Display the contacts using a JTable and a delete button to delete the selected contact from the corresponding ArrayList element. You would do this by calling JTable.getSelectedRow() on your table model to get the correct contact in your ArrayList:
ActionListener forDeleteButton = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
ListOfContacts.remove(myTable.getSelectedRow());
}
};
Then add a "save" button that overwrites the text file with the ArrayList ListOfContacts. Hope this helps.
I have created an object that i then added into an ArrayList. I then saved this arraylist into a .dat file using ObjectOutputStream. I know that the object has been created correctly because the next screen in the program loads directly from the object and the object can be read correctly from the file afterwards. The issue I am running into is that it appears that the object is saving, but it is not willing to be edited. Instead of editing the object in the arraylist, it is creating a new object and saving it again each time.
The code below shows the save function that is performed every time someone finishes the last screen. I am trying to make it so it will check to see if the student exists already in the array, and if so just edit the object. If the student doesn't exist, I want it to take selectedStudent (the object used for all GUI functions) and add it to the array "students" and write the array to the file, overwriting all previous data.
#SuppressWarnings("unchecked")
public static void saveNew() throws ClassNotFoundException, IOException{
int exists = -1;
try{
FileInputStream fis = new FileInputStream("records.dat");
ObjectInputStream in = new ObjectInputStream(fis);
students = (ArrayList<Student>) in.readObject();
in.close();
fis.close();
}
catch(IOException e){
e.printStackTrace();
}
try{
FileOutputStream fos = new FileOutputStream("records.dat", false);
ObjectOutputStream out = new ObjectOutputStream(fos);
for(int i = 0; i<students.size(); i++){
if(students.get(i).getID().equals(selectedStudent.getID())){
exists = i;
}
}
if(exists<0){
students.add(selectedStudent);
}
else{
students.set(i, selectedStudent);
}
out.writeObject(students);
out.close();
}
catch(IOException e){
e.printStackTrace();
}
}
Edit: I noticed that the variable exists was not being used to search for the object which was mistake number one, but I still have the issue where the saved object will not be changed until the method is called a second time. It seems to find it when it is run again, but when it is run the first time it will just create a new student with the edited name.
For example, the first student is created and saved. A second student is the created and saved. When the second student is being edited (without closing the program and restarting) it will, instead of editing the object in the file, create a new student object with the same information directly below the first. If the edit function is run a second time, the second student file is edited correctly but leaves the first as it was.
For a start I would edit these lines
if(students.get(i).getID().equals(selectedStudent.getID())){
exists = i;
}
to
if(students.get(i).getID().equals(selectedStudent.getID())){
System.out.println ("Got it");
exists = i;
break;
}
just to make sure it is working.
Also, you want to change the use of i to exists
else{
students.set(i, selectedStudent); // change to exists
}
I think you must check your variables (if you're reusing any) and initialization code.
The snippet you've posted seems to be fine, so I can't find the error on it.
Here goes a quite similar code that works. I hope it helps.
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class Persistence {
public static void main(String[] args) throws Exception {
File f = new File("records.dat");
// f.delete();
if (!f.exists()){
f.createNewFile();
}
Persistence p = new Persistence();
if (p.peek() == null){
p.init(); //persist an empty list
}
p.saveNew(new Student("ID1","Some Value")); //must insert
p.saveNew(new Student("ID1","Some Other Value")); //must edit
p.saveNew(new Student("ID2","Some Value")); //must insert
ArrayList<Student> list = p.peek();
System.out.println(list);
}
private void save(ArrayList<Student> list) throws Exception{
FileOutputStream fos = new FileOutputStream("records.dat",false);//don't append
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(list);
out.flush();
out.close();
fos.close();
}
private void init() throws Exception{
save(new ArrayList<Student>());
}
private ArrayList<Student> peek() throws Exception{
FileInputStream fis = new FileInputStream("records.dat");
try{
ObjectInputStream in = new ObjectInputStream(fis);
ArrayList<Student> students = (ArrayList<Student>) in.readObject();
return students;
}catch(EOFException eof){
return null;
}finally{
fis.close();
}
}
public void saveNew(Student s) throws Exception {
ArrayList<Student> students = peek();
int editIndex = -1;
for(int i=0;i<students.size();i++){
if (students.get(i).getID().equals(s.getID())){
editIndex = i;
break;
}
}
if (editIndex != -1){
students.set(editIndex, s); //replace
}else{
students.add(s); //add
}
save(students);
}
}
where
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String ID;
private String s;
public Student(String ID, String s) {
this.ID = ID;
this.s = s;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
#Override
public String toString() {
return "Student [ID=" + ID + ", s=" + s + "]";
}
}
I'm trying to take data from a CSV file, parse it to a two dimensional array and then return that to a GUI that displays it in a JTable. Doesn't seem to be going too well!
METHOD TO GET FROM CSV
static String[][] readGUIFromPropertyFile(String sFileName, String userName) throws FileNotFoundException
{
String thisLine;
String tempArray[][] = new String[20][4];
int i = 0;
BufferedReader reader = new BufferedReader(new FileReader(sFileName));
try
{
while((thisLine = reader.readLine()) != null)
{
String propertyDetails[] = thisLine.split(",");
if (propertyDetails[0].equals(userName))
{
tempArray[i][0] = propertyDetails[1];
tempArray[i][1] = propertyDetails[2];
tempArray[i][2] = propertyDetails[3];
tempArray[i][3] = propertyDetails[4];
tempArray[i][4] = propertyDetails[5];
i++;
}
}
return tempArray;
}
catch(IOException e)
{
System.out.print("\nProperties do not exist\n");
e.printStackTrace();
}
finally{
try
{ reader.close();
}catch (IOException e){}}
return tempArray;
}
}
CODE FOR GUI
else if (event.getSource() == reload)
{
try {
data = CSVWrite.readGUIFromPropertyFile(propertyFile, userName);
JOptionPane.showMessageDialog(frame, "Properties Loaded");
userPropertyView.add(displayProperties);
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(frame, "Properties Not Loaded");
e.printStackTrace();
}
}
I have it working with a command interface so I know the code is working, but I have to implement both a GUI and a command line. I can write from GUI to CSV, no issue, but having trouble displaying it. I've looked into ArrayLists and I actually have a lecture on them tomorrow so that's also a possibility.
I can't use OpenCSV as I have to use default libraries for this.
put String tempArray[][] as JTables constructor JTable(Object[][] rowData, Object[] columnNames) directly
better way should be to add a new row to the TableModel, not replace the JTable on runtime, more in tutorial Creating a Table Model