In need of serious JFrame/Stringbuffer Help!
Basicly, it opens a GUI with a few buttons like "previous, last, first, next, add, exit"
and i take 3 inputs from the user...from the JTextField. Those values are put in a file and are supposed to be read (i'm assuming that i use stringtokenizer) but right now i'm using stringbuffer to write to a file. I can't get it to write to the next line though.. it writes next to it..
I'm just hoping that i'm using the correct functions to do this job. Feedback greatly appreciated, i'm really worried about the stringbuffer and having it write the way it should. I didn't do the reading yet.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
publicclass BookMaintenanceextends JFrame
{
private JTextField author;
private JTextField title;
private JTextField publisher;
private JButton add;
private JButton exit;
private JButton first;
private JButton previous;
private JButton next;
private JButton last;
private Container container;
public BookMaintenance()
{
author =new JTextField(10);
title =new JTextField(10);
publisher =new JTextField(10);
add =new JButton("Add");
exit =new JButton("Exit");
first =new JButton("First");
previous =new JButton("Prev");
next =new JButton("Next");
last =new JButton("Last");
last.setSize(30,10);
JPanel jPanel1 =new JPanel();
jPanel1.add(new JLabel("Title: "));
jPanel1.add(title);
JPanel jPanel2 =new JPanel();
jPanel2.add(new JLabel("Author: "));
jPanel2.add(author);
JPanel jPanel3 =new JPanel();
jPanel3.add(new JLabel("Publisher: "));
jPanel3.add(publisher);
JPanel jPanel4 =new JPanel();
jPanel4.add(first);
jPanel4.add(next);
jPanel4.add(previous);
jPanel4.add(last);
JPanel jPanel5 =new JPanel();
jPanel5.add(add);
jPanel5.add(exit);
container = this.getContentPane();
GridLayout gLayout =new GridLayout(5,1);
container.setLayout(gLayout);
container.add(jPanel1);
container.add(jPanel2);
container.add(jPanel3);
container.add(jPanel4);
container.add(jPanel5);
ActionListener actionListener =new ButtonListener();
first.addActionListener(actionListener);
next.addActionListener(actionListener);
previous.addActionListener(actionListener);
last.addActionListener(actionListener);
add.addActionListener(actionListener);
exit.addActionListener(actionListener);
}
class ButtonListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent event)
{
if(event.getSource() == add)
{
StringBuffer stringBuffer =new StringBuffer();
stringBuffer.append(title.getText());
stringBuffer.append(" ");
stringBuffer.append(author.getText());
stringBuffer.append(" ");
stringBuffer.append(publisher.getText());
stringBuffer.append(" ' ");
RandomAccessFile randomAccessFile =null;
try
{
randomAccessFile =new RandomAccessFile("file.txt","rw");
System.out.println(stringBuffer.toString());
randomAccessFile.seek(randomAccessFile.length());
randomAccessFile.writeBytes(stringBuffer.toString() );
randomAccessFile.close();
}
catch(IOException ioe){}
}
}
}
publicstaticvoid main(String args[])
{
BookMaintenance frame =new BookMaintenance();
frame.setTitle("Book Maintenance");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 250);
frame.setVisible(true);
// frame.setResizable(false);
}
}
[6365 byte] By [
DenisKa] at [2007-11-27 9:36:05]

So this has nothing to do with the JFrame but instead your problem is writing to a file. Perhaps you should take a look at Sun's IO tutorial.
well, i also wish to know if i'm doing it right... or if theres a simpler way.and also.. aligning my JTextFields and JButtons to the left side instead of center..
The tutorial will probably answer that.
can someone atleast tell me how to make a new line when writing stringbuffer. the /n doesn't do the job.
Probably because you should be using \n.
Why use a RandomAccessFile when the only RAF feature you are using is appending to the end of the file? Why not open the file in append mode?
Not sure how to do it. But, i was thinking about putting it into an arraylist that way i can use the "prev, first, last, next" buttons easier. But again, i don't know how to store the values correctly...
ok let me clear it up
i use arraylist
i write a author, publisher, title in the gui window..
push add
store author, publisher, title in the arraylist..[0]
then add another author, publisher, title.
store to [1].. and etc.
now then i use the prev,next,last,first buttons i can make it easier.
i just can't seem to be able to store it the way i want to..
That is all very interesting but has nothing to do with reading and writing to a file. Do you have a specific problem?
Well i can't read/write the file unless i do this. But i have to do these steps first. Can you help?The program isn't as simple as just reading and writing to a file.
Yes we can help but still don't know what the problem is. Can you ask a specific question? Currently you post appears as "Here's my code, fix it for me"
No no, it's nothing like that. Actually, if i could edit my first post i would.. My problem is saving title, author, publisher values into an arraylist. Basicly, the gui opens. has 3 JTextFields
1. Title: _
2. Author: _
3. Publisher: _
theres JButtons under that which are
Prev, First, Next, Last
Add, Exit
Now, when the user clicks add, i want title, author, publisher to be tossed into an arraylist. then i can write it to a file, then read and grab the contents of it after. Not sure if this explains it better?.. I have NO idea how to do this though...
Check out the demos I gave in this thread: http://forum.java.sun.com/thread.jspa?threadID=5190911&start=0&tstart=0
I have, I've tried many different ways.. but it's hard because with your example, you added it by itself.. and i don't know how to change it to take the input from the user instead of what you gave it.
Lookin at your code.. how would i replace this with input from the user? like add the title, author, publisher in there?
items = new ArrayList<InventoryItem>();
items.add(new InventoryItem("Java Programming", "A book about java programming."));
items.add(new InventoryItem("Monkey Soup", "A delicious Dinner Meal."));
items.add(new InventoryItem("Red", "My Favorite Color"));
items.add(new InventoryItem("Light Bulb", "An incandescent miracle."));
items.add(new InventoryItem("Caleb Payne", "Your god."));
items.add(new InventoryItem("You", "Caleb's bitch."));
Message was edited by:
DenisK
If you are using a GUI then you need to have textfields where they can enter input. You then get what has been entered into those fields and do what you like.
Then here is a little demo that takes use input, adds it to an ArrayList, then spits out all entries in the ArrayList:
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person() { }
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String toString() {
return "Name: " + name + "\nAge: " + age;
}
}
class Test extends JFrame {
private JLabel nameLabel;
private JLabel ageLabel;
private JTextField nameField;
private JTextField ageField;
private JButton addButton;
private JButton showAllButton;
private ArrayList<Person> people;
public Test() {
initComponents();
}
private void initComponents() {
nameLabel = new JLabel("Name:");
ageLabel = new JLabel("Age:");
nameField = new JTextField();
ageField = new JTextField();
addButton = new JButton("Add");
showAllButton = new JButton("Show All");
people = new ArrayList<Person>();
nameLabel.setSize(50, 20);
ageLabel.setSize(50, 20);
nameField.setSize(100, 20);
ageField.setSize(50, 20);
addButton.setSize(100, 20);
showAllButton.setSize(100, 20);
nameLabel.setLocation(5, 5);
ageLabel.setLocation(5, 30);
nameField.setLocation(55, 5);
ageField.setLocation(55, 30);
addButton.setLocation(5, 55);
showAllButton.setLocation(110, 55);
this.getContentPane().setLayout(null);
this.getContentPane().add(nameLabel);
this.getContentPane().add(ageLabel);
this.getContentPane().add(nameField);
this.getContentPane().add(ageField);
this.getContentPane().add(addButton);
this.getContentPane().add(showAllButton);
this.pack();
this.setSize(220, 115);
this.setLocationRelativeTo(null);
this.setTitle("Demo");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
addPerson();
}
});
showAllButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
showAll();
}
});
}
private void addPerson() {
people.add(new Person(nameField.getText(), Integer.parseInt(ageField.getText())));
nameField.setText("");
ageField.setText("");
nameField.requestFocus();
}
private void showAll() {
for( Person p : people ) System.out.println(p);
}
public static void main(String[] argv) {
new Test().setVisible(true);
}
}
Thanks this will help a lot. Is there a reason it doesn't run?
> Thanks this will help a lot. Is there a reason it> doesn't run?It does.
Hm. Why am i getting a...Exception in thread "main" java.lang.NoSuchMethodError: mainPress any key to continue . . .
> Hm. Why am i getting a...
> Exception in thread "main"
> java.lang.NoSuchMethodError: main
> Press any key to continue . . .
are you running the class called "Test" or the class called "Person?" Person has no main method. Test does. (Run the Test.class file)
Oh ok, I had it saved in Person.java
> Oh ok, I had it saved in Person.javaThat would be a problem ;-)I take it everything worked out for you, then?
Your GUI code should be separate from your "back-end" code, in this case, the code that does file I/O. You should be able to test the file I/O without a GUI. When you realize this way makes for less work and better programming, then, Grasshopper, you will have learned.
One more question. When it stores it to the ArrayList.. and i close the program.. the values get erased right? I kind of need it to be stored perm..
> One more question. When it stores it to the
> ArrayList.. and i close the program.. the values get
> erased right? I kind of need it to be stored perm..
Yes - the values get "erased" when the program exits. My suggestion would be to look up serialization and the "serializable interface."
Happy coding.
Take the I/O tutorial: http://java.sun.com/docs/books/tutorial/essential/io/index.html
Yeah i need to look into Serializable. Teacher didn't get into it yet.
By the way: Navy_coder. I can't seem to figure out why the previous button is acting weird. It doesn't give the previous one.. Not sure.. but it's weird. When i click last, then try Prev. It doesn't work right. But if i go from Next to Prev, It works.
private void goPrev() {
if( currIndex > 0 ) {
currIndex--;
titleField.setText(people.get(currIndex).getTitle());
authorField.setText(people.get(currIndex).getAuthor());
publisherField.setText(people.get(currIndex).getPublisher());
}
}
why dont you create a button by the name of read file and write file with a textfield where user can specify the file name?
Here's everything. The buttons seem to get confused when clicked sometimes.. Now i'm trying to figure out the update... mhm..
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.io.Serializable;
class Person {
private String title;
private String author;
private String publisher;
public Person(String title, String publisher, String author) {
this.title = title;
this.author = author;
this.publisher = publisher;
}
public Person() { }
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public String toString() {
return "Title: " + title + "\nAuthor: " + author + "\nPublisher" + publisher +"\n";
}
}
class Test extends JFrame {
private JLabel titleLabel;
private JLabel authorLabel;
private JLabel publisherLabel;
private JTextField titleField;
private JTextField authorField;
private JTextField publisherField;
private JButton addButton;
private JButton showAllButton;
private JButton exitButton;
private JButton firstButton;
private JButton lastButton;
private JButton prevButton;
private JButton nextButton;
private JButton updateButton;
private JButton deleteButton;
private ArrayList<Person> people;
private int currIndex = 0;
public Test() {
initComponents();
}
private void initComponents() {
titleLabel = new JLabel("Title:");
authorLabel = new JLabel("Author:");
publisherLabel = new JLabel("Publisher:");
titleField = new JTextField();
authorField = new JTextField();
publisherField = new JTextField();
addButton = new JButton("Add");
showAllButton = new JButton("Show All");
exitButton = new JButton("Exit");
firstButton = new JButton("First");
lastButton = new JButton("Last");
prevButton = new JButton("Prev");
nextButton = new JButton("Next");
updateButton = new JButton("Update");
deleteButton = new JButton("Delete");
people = new ArrayList<Person>();
titleLabel.setSize(100, 20);
authorLabel.setSize(100, 20);
publisherLabel.setSize(100, 20);
titleField.setSize(200, 20);
authorField.setSize(200, 20);
publisherField.setSize(200, 20);
addButton.setSize(80, 20);
showAllButton.setSize(83, 20);
firstButton.setSize(65,20);
nextButton.setSize(65,20);
prevButton.setSize(65,20);
lastButton.setSize(65,20);
exitButton.setSize(65,20);
updateButton.setSize(74,20);
deleteButton.setSize(74,20);
titleLabel.setLocation(5, 5);
authorLabel.setLocation(5, 30);
publisherLabel.setLocation(5, 55);
titleField.setLocation(65, 5);
authorField.setLocation(65, 30);
publisherField.setLocation(65, 55);
addButton.setLocation(5, 80);
updateButton.setLocation(90, 80);
deleteButton.setLocation(169, 80);
exitButton.setLocation(248, 80);
firstButton.setLocation(15, 105);
prevButton.setLocation(155, 105);
nextButton.setLocation(85, 105);
lastButton.setLocation(225, 105);
showAllButton.setLocation(115, 130);
this.getContentPane().setLayout(null);
this.getContentPane().add(titleLabel);
this.getContentPane().add(authorLabel);
this.getContentPane().add(publisherLabel);
this.getContentPane().add(titleField);
this.getContentPane().add(authorField);
this.getContentPane().add(publisherField);
this.getContentPane().add(firstButton);
this.getContentPane().add(nextButton);
this.getContentPane().add(prevButton);
this.getContentPane().add(lastButton);
this.getContentPane().add(exitButton);
this.getContentPane().add(addButton);
this.getContentPane().add(deleteButton);
this.getContentPane().add(updateButton);
this.getContentPane().add(showAllButton);
this.pack();
this.setSize(326, 190);
this.setLocationRelativeTo(null);
this.setTitle("Book Maintenance");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
firstButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
goFirst();
}
});
lastButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
goLast();
}
});
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
goNext();
}
});
prevButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
goPrev();
}
});
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
addBook();
}
});
showAllButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
showAll();
}
});
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
exit();
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
delete();
}
});
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
update();
}
});
}
private void addBook() {
people.add(new Person(titleField.getText(), authorField.getText(), publisherField.getText()));
titleField.setText("");
authorField.setText("");
publisherField.setText("");
titleField.requestFocus();
}
private void showAll() {
for( Person p : people ) System.out.println(p);
}
private void exit() {
System.exit(0);
}
private void goLast() {
titleField.setText(people.get(people.size() - 1).getTitle());
authorField.setText(people.get(people.size() - 1).getAuthor());
publisherField.setText(people.get(people.size() - 1).getPublisher());
}
private void goFirst() {
titleField.setText(people.get(0).getTitle());
authorField.setText(people.get(0).getAuthor());
publisherField.setText(people.get(0).getPublisher());
}
private void goNext() {
if( currIndex < people.size() - 1 ) {
currIndex++;
titleField.setText(people.get(currIndex).getTitle());
authorField.setText(people.get(currIndex).getAuthor());
publisherField.setText(people.get(currIndex).getPublisher());
}
}
private void goPrev() {
if( currIndex > 0 ) {
currIndex--;
titleField.setText(people.get(currIndex).getTitle());
authorField.setText(people.get(currIndex).getAuthor());
publisherField.setText(people.get(currIndex).getPublisher());
}
}
private void delete() {
people.remove(currIndex);
if( currIndex > 0 ) {
currIndex--;
titleField.setText(people.get(currIndex).getTitle());
authorField.setText(people.get(currIndex).getAuthor());
publisherField.setText(people.get(currIndex).getPublisher());
} else {
titleField.setText("");
authorField.setText("");
publisherField.setText("");
titleField.requestFocus();
}
}
private void update() {
}
public static void main(String[] argv) {
new Test().setVisible(true);
}
}
Anyone got an example of an update? For example. Having a few fields, changing one of them. Clicking update and it updates it?
What do you mean by update?
Basicly, i have 3 fields. I input data into those fields, click add. It adds it to the array list. If later i want to "edit" it. I go back to it.. change what ever field i wish to edit, and they push the update button and it changes it. This way i dont have to delete and re-add it.
Changes what?We can't read your mind. You have to provide as much information as possible.
Changes the field that i edited.
Title: Woow
Author: Denis
Publisher: flounder
click "add".. now it's stored in 0 on the array list.
lets say the author changes.. i go to it. change author from Denis to Jacob
so now it looks like this
Title: Woow
Author: Jacob
Publisher: flounder
i click "update" and it stores Jacob instead of Denis
I think I know what you want but you still haven't describe the problem well. You have a bunch of objects in an ArrayList and you want to change the Author of a particular object Is this correct?
So you would need to search your List for the correct object and call the setAuthor or setPublisher or setWhatever method on it.
Right, it's kind of confusing trying to write it. But you have the right idea. I tried doing it the way you described. But.. got lostI'm going to atry a few ways.
I assume you have retrieved the object somehow to display the details in the first place. So you should have some reference to that object. So all you would need to do is work out which field has been changed and call the correct method on the object to update it.
I got this working, but it kind of doesn't do what i want it to. It removes it, and re-adds it. But, it moves it to the end of the arraylist. I want it to stay in its current state. Like, when i update index 0, it re-adds it, but places it at the end. Mhm..
private void update() {
people.remove(currIndex);
people.add(new Person(titleField.getText(), authorField.getText(), publisherField.getText()));
people.remove(currIndex);And you are really confused to as why it removes it? Come on!!!Do as I said, just call an appropriate setX method.
No, i'm not curious as to why it removes it. I know why. You mis-read my post.It removes and re-adds the new one. But it moves it to the end of the arraylist opposed to keeping it as it. I have tries the set methods and they haven't worked.
No I didn't misread your post. It removes the object and moves it to the end because you tell it to. That is what your code does. If you don't want it to do that then don't REMOVE it and ADD a new object. The add method should always add a new object to the end of the list.
Yeah, i understand, i wan't to edit it but haven't thought of a way other than this. You recommended the set methods. I had it just setting plain text then getting it.
ex.
titleField.setText("");
authorField.setText("");
publisherField.setText("");
titleField.getText();
authorField.getText();
publisherField.getText();
any suggestions?
?That's change the textfields in your GUI and has nothing to do with your object!You need something likecurrentObject.setAuthor(authorField.getText());
Something like this?Person p1 = new Person();p1.setAuthor(authorField.getText());p1.setTitle(titleField.getText());p1.setPublisher(publisherField.getText());
NO!You don't need a new object. You just need to modify the existing one.
I dont have a current object..
OMG!The friggin object that you are trying to update. Are you this thick or just trying to piss me off?
Not trying to piss you offi triedpeople.setTitle(titleField.getText());person.setTitle(titleField.getText());currIndex.setTitle(titleField.getText());titleField.setTitle(titleField.getText());i can't seem to find it.
how do you identify which object you wish to change / update?
Still don't got it working
Wheres navy_coder when i need him :(