How to let the button set to it "latest" text because i suppose to set the button's text to "booked" and the foreground to red. However, everytime i re-run the program, the button re-set. I know i should do something on constructor, but how?

Here is my coding.. Thanks for advance =)...

for(int i=0; i<10; i++){
				fcButton[i] = new JButton();
				if(fcButton[i].getText() != "Booked"){
					fcButton[i].setText("Seat"+(i+1));
					buttonPanelL.add(fcButton[i]);
					fcButton[i].addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							ButtonActionPerformed(e);
						}
					});
				}
			}
			for(int i=10; i<20; i++){
				fcButton[i] = new JButton();
				if(fcButton[i].getText() != "Booked"){
					fcButton[i].setText("Seat"+(i+1));
					buttonPanelR.add(fcButton[i]);
					fcButton[i].addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							ButtonActionPerformed(e);
						}
					});
				}
			}

public void ButtonActionPerformed(ActionEvent e){
				for(int i=0; i<20; i++){
					if(e.getSource() == fcButton[i]){
						if(fcButton[i].getText() != "Booked"){
							str = fcButton[i].getText();
							fcButton[i].setText("Booked");
							fcButton[i].setForeground(Color.red);
							JOptionPane.showMessageDialog(null,"Your seat is: "+str);
						}
						else{
							JOptionPane.showMessageDialog(null,"THIS PLACE WAS BOOKED");
						}
					}
				}
		}

Recommended Answers

All 8 Replies

How to let the button set to it "latest" text because i suppose to set the button's text to "booked" and the foreground to red. However, everytime i re-run the program, the button re-set. I know i should do something on constructor, but how?

Here is my coding.. Thanks for advance =)...

for(int i=0; i<10; i++){
				fcButton[i] = new JButton();
				if(fcButton[i].getText() != "Booked"){
					fcButton[i].setText("Seat"+(i+1));
					buttonPanelL.add(fcButton[i]);
					fcButton[i].addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							ButtonActionPerformed(e);
						}
					});
				}
			}
			for(int i=10; i<20; i++){
				fcButton[i] = new JButton();
				if(fcButton[i].getText() != "Booked"){
					fcButton[i].setText("Seat"+(i+1));
					buttonPanelR.add(fcButton[i]);
					fcButton[i].addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							ButtonActionPerformed(e);
						}
					});
				}
			}

public void ButtonActionPerformed(ActionEvent e){
				for(int i=0; i<20; i++){
					if(e.getSource() == fcButton[i]){
						if(fcButton[i].getText() != "Booked"){
							str = fcButton[i].getText();
							fcButton[i].setText("Booked");
							fcButton[i].setForeground(Color.red);
							JOptionPane.showMessageDialog(null,"Your seat is: "+str);
						}
						else{
							JOptionPane.showMessageDialog(null,"THIS PLACE WAS BOOKED");
						}
					}
				}
		}

I dont know what you are tyring to do. Is this an Airplane seat? or a bus? 20 seats sounds like a bus. Anyways, your program always goes backt to its state if you re-run it. I dont know but,I think you should make a text file that would store all the button changes. so your JButton classes ,for example. can access that file each time it runs. Youre already probably familiar on how to use the File class and bufferedReader to access text files or binary files right? its alot of work but thats the first thing that I thought of. Im sure there is an easier way, I just dont know what it is.

I dont know what you are tyring to do. Is this an Airplane seat? or a bus? 20 seats sounds like a bus. Anyways, your program always goes backt to its state if you re-run it. I dont know but,I think you should make a text file that would store all the button changes. so your JButton classes ,for example. can access that file each time it runs. Youre already probably familiar on how to use the File class and bufferedReader to access text files or binary files right? its alot of work but thats the first thing that I thought of. Im sure there is an easier way, I just dont know what it is.

Huh? i don't understand..>.<'' can u give me an example or any tutorial to refer?

He means that you have to save the state somehow. These easiest way to do that is to write to and read from a file.

Huh? i don't understand..>.<'' can u give me an example or any tutorial to refer?

Here is an example of reading from a text file. Try writing a text in notepad and save it somewhere you want. Then in your code, specify that path to the File Contstructor like you see below. ex. new File("c:/blah/blah/blah.txt")..

just copy this code and just change the file path pointing at your saved .txt file.

import java.io.*;
import java.util.*;


//This app reads the contents in a .txt file.

public class ReadFileTEst
{
 public static void main(String[] args)throws IOException
 {
  

   	//File Class. You have to import "java.io" package. Check java API for more details.

   	//the argument in the File Constructor could be a file object or a String Path like in this example.

   		File fileDB= new File("C:/My Documents/sample.txt");
   		ArrayList<String> wordStorage= new ArrayList<String>();


   // Creats a stream that reads a textFile.
   		BufferedReader in =new BufferedReader(new FileReader(fileDB));


  	try
  	{

  	  if(fileDB.exists())
  	  {


       String line= in.readLine();
       while(line !=null)
       {
		   System.out.println(line);

		   line = in.readLine();
	   }

   }
   else
   System.out.println(fileDB.getPath() + "does not exist");
   }
   catch(IOException ioe)
   {
	   System.out.println(ioe +" thrown and caught");
   }
   finally
   {
      in.close();
   }



}
}

So for this kind of code style, your button configuration wont change because its saved in a file. Add a code in your JButton to access the .txt file.
Now, to write to a file. Thats a diffrent proccess.Hope this helps you a bit.. If you need help writing to a .txt file, just ask. We all help each other here.

Thanks for your reply.erm..i don't know how to write the button configurations to txt file.>.<..do you mind to teach?
Btw the steps is it like:
1)set the buttons to disable before the buttons configurations updated
2)read the file
3)set the buttons to enable
4)construct GUI.

Anyone? Sorry but i am damn stuck! Just how to save the button configuration..>.<''

Well. what have you tried? What have you thought about trying? You've been given suggestions that you need to write the data to a file and read it back when you set up the buttons, so have you tried doing that at all?

Thanks for your reply.erm..i don't know how to write the button configurations to txt file.>.<..do you mind to teach?
Btw the steps is it like:
1)set the buttons to disable before the buttons configurations updated
2)read the file
3)set the buttons to enable
4)construct GUI.

It is possible that we can write the whole thing for you. But that would prevent you from learning other things (Plus, there is giong to be alot of writing for this, hehe) . Im not a pro but I'm going help if you can show me what you have writen so far.

So The JButton constructor accepts a string ,right? Write a code that reads a .txt file. For example, if your .txt file contains a text that says " Booked ". Read that text file and assign it as a String object. Then, Use that String object as an argument in the JButton constructor.

This technique is just an example to solve your initial problem ,though, of not saving the state of your program when it re-runs. Write to a file for changes, then when you re-run your program, your code will read from the same file so its previous state is the output. Got the idea?..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.