Holding text in a Text Area

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Badboy112
    New Member
    • Oct 2008
    • 8

    #1

    Holding text in a Text Area

    Hi guys

    i have used a data file like you guys have asked me to but im not entirely sure how to use the DataInputStream
    What i would like to do for this assigment is import the text from the data file and keep that text in the specific text area even after i closed it and then i would like to hold that same text in the text Area after i have opened it and my clear button doesnt seem to clear the specific field i am asking it to clear any suggestions on this annoying button :D
    thak you guys

    First sext of code
    Code:
    public class Rooms
    {
    	//declare class variables
    	int numSmoking;
    	int numNonSmoking;
    	boolean occupied[];
    
    	public Rooms(int non, int sm)
    	{
    		//construct an array of boolean values equal to thr total number of rooms
    		occupied = new boolean[sm+non];
    		for (int i=0; i<(sm+non); i++)
    			occupied[i] = false;//set each occupied room to false
    			//initialize the number of smoking and nonSmoking rooms
    			numSmoking = sm;
    			numNonSmoking = non;
    
    	}
    
    	public int bookRoom(boolean smoking)
    	{
    		int begin, end, roomNumber = 0;
    
    		if (!smoking)
    		{
    			begin = 0;
    			end = numNonSmoking;
    		}
    		else
    		{
    			begin = numNonSmoking;
    			end = numSmoking+numNonSmoking;
    		}
    
    		for(int i=begin; i<end; i++)
    		{
    			if(!occupied[i])//if room is not occupied
    			{
    				occupied[i] = true;
    				roomNumber = i+1;
    				i = end; // to exit loop
    			}
    		}
    		return roomNumber;
    	}
    }
    
    
    second set of code
    
    import java.io.*;
    import javax.swing.JOptionPane;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class Reservations extends Frame implements ActionListener
    {
    	Color lightRed = new Color(255, 90, 90);
    	Color lightGreen = new Color(140, 215, 40);
    	DataOutputStream output;
    	DataInputStream input;
    
    	Rooms room = new Rooms(5,3);
    
    	Panel roomPanel = new Panel();
    		TextArea roomDisplay[] = new TextArea[9];
    
    	Panel buttonPanel = new Panel();
    		Button bookButton = new Button("Book Room");
    		Button clearButton = new Button("Clear Field");
    
    	Panel inputPanel = new Panel();
    		Label custNameLabel = new Label("Name");
    		TextField nameField = new TextField(15);
    		Label custPhoneLabel = new Label("Phone Number");
    		TextField phoneField = new TextField(15);
    		Label numLabel = new Label("Number in Party");
    		Choice clearChoice = new Choice();
    		CheckboxGroup options = new CheckboxGroup();
    			Checkbox nonSmoking = new Checkbox("NonSmoking",false,options);
    			Checkbox smoking = new Checkbox("Smoking",false,options);
    			Checkbox hidden =  new Checkbox("",true,options);
    		Label clearLabel = new Label("Clear Which field: ");
    
    public Reservations()
    {
    	//set Layouts for frame and three panels
    	this.setLayout(new BorderLayout());
    		roomPanel.setLayout(new GridLayout(2,4,10,10));
    		buttonPanel.setLayout(new FlowLayout());
    		inputPanel.setLayout(new FlowLayout());
    
    	//add components to room panel
    	for (int i=1; i<9; i++)
    	{
    		roomDisplay[i] = new TextArea(null,3,5,3);
    		if(i<6)
    			roomDisplay[i].setText("Room"+ i +" NonSmoking");
    		else
    			roomDisplay[i].setText("Room"+ i +" Smoking");
    		roomDisplay[i].setEditable(false);
    		roomDisplay[i].setBackground(lightGreen);
    		roomPanel.add(roomDisplay[i]);
    	}
    
    	//add components to button panel
    	buttonPanel.add(bookButton);
    	buttonPanel.add(clearLabel);
    	buttonPanel.add(clearChoice);
    		for(int i = 1; i<=8; i++)
    		clearChoice.add(String.valueOf(i));
    	buttonPanel.add(clearButton);
    
    	//add components to input panel
    	inputPanel.add(custNameLabel);
    	inputPanel.add(nameField);
    	inputPanel.add(custPhoneLabel);
    	inputPanel.add(phoneField);
    	inputPanel.add(numLabel);
    	inputPanel.add(nonSmoking);
    	inputPanel.add(smoking);
    
    
    	//add panels to frame
    	add(buttonPanel, BorderLayout.SOUTH);
    	add(inputPanel, BorderLayout.CENTER);
    	add(roomPanel, BorderLayout.NORTH);
    
    	bookButton.addActionListener(this);
    	clearButton.addActionListener(this);
    	clearButton.setActionCommand("Clear Field");
    
    	String filename = "Party Room";
    
    	try
    	{
    		output = new DataOutputStream(new FileOutputStream("Party Room"));
    	}//end of try
    	catch(IOException io)
    	{
    
    	}
    
    	try
    	{
    		input = new DataInputStream(new FileInputStream("Party Rooml"));
    	}
    
    	catch(IOException io)
    	{
    	}
    
    
    	//overiding the windowClosing() method will allow the user to click the close button
    	addWindowListener(
    		new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				System.exit(0);
    			}
    		}
    	);
    }//end constructor method
    
    
    public void actionPerformed(ActionEvent e)
    {
    	String arg = e.getActionCommand();
    	if (arg == "Clear Field");
    	{
    		for(int i= 1; i<=21; i++)
    			{
    
    				clearChoice.getSelectedItem();
    				roomDisplay[i].setText("Room" + i);
    				roomDisplay[i].setBackground(lightGreen);
    				roomDisplay[i].setEditable(false);
    			}
    	}
    	if (hidden.getState())
    	{
    		JOptionPane.showMessageDialog(null,"You must select Smoking or NonSmoking","Error",JOptionPane.ERROR_MESSAGE);
    	}
    	else
    	{
    		int available = room.bookRoom(smoking.getState());
    
    		if (available > 0) // room is available
    		{
    				try
    				{
    
    					output.writeUTF(nameField.getText());
    					output.writeUTF(phoneField.getText());
    				}//end of try
    				catch(IOException c)
    				{
    					System.exit(1);
    				}//end of catch
    				try
    				{
    					roomDisplay[available].setBackground(lightRed);
    					roomDisplay[available].setText(input.readUTF(nameField.getText));
    					roomDisplay[available].setText(input.readUTF(phoneField.getText));
    
    				}//end of try
    					catch(IOException d)
    						{
    							System.exit(1);
    						}//end of try
    
    			clearFields();
    		}
    		else
    		{
    			if(smoking.getState())
    				JOptionPane.showMessageDialog(null,"Smoking is full","Error",JOptionPane.INFORMATION_MESSAGE);
    			else
    				JOptionPane.showMessageDialog(null,"NonSmoking is full","Error",JOptionPane.INFORMATION_MESSAGE);
    			hidden.setState(true);
    		}//end of else block that checks the available room number
    	}//end of else block that checks the state of the hidden button option
    
    }//end of action performed method
    
    	//reset the text fields
    	void clearFields()
    	{
    		nameField.setText("");
    		phoneField.setText("");
    		clearChoice.select(0);
    		nameField.requestFocus();
    		hidden.setState(true);
    	}
    
    public static void main(String[] args)
    {
    	Reservations f = new Reservations();
    	f.setBounds(200,200,900,600);
    	f.setTitle("Reserve a Party Room");
    	f.setVisible(true);
    }//end of main method
    }
    Last edited by JosAH; Oct 17 '08, 04:38 PM. Reason: added [code] ... [/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Better use the Swing components instead of those old AWT components. A
    JTextArea can read/write it contents from/to Readers/Writers.

    kind regards,

    Jos

    Comment

    • Badboy112
      New Member
      • Oct 2008
      • 8

      #3
      Originally posted by JosAH
      Better use the Swing components instead of those old AWT components. A
      JTextArea can read/write it contents from/to Readers/Writers.

      kind regards,

      Jos
      Hey Jos thanks a million man but will it hold the information even after i close the application and then open it again

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Badboy112
        Hey Jos thanks a million man but will it hold the information even after i close the application and then open it again
        Nope, those components aren't psychic, i.e. they don't know when they are
        supposed to load or save their content. You have to tell them that one way
        or another.

        That could be a nice little project: some sort of persistency 'framework' to which
        objects can register. You just push that little framework and all registered objects
        load and save whatever they have to load or save. That would be quite doable:
        a few interfaces and a bit of implementation:

        Code:
        public interface Persistable {
           public void load(InputStream is) throws IOException;
           public void save(OutputStream os) throws IOException;
        }
        ... but I get carried away; I leave that little project up to you and to anyone else
        who reads this ;-)

        kind regards,

        Jos

        Comment

        • Badboy112
          New Member
          • Oct 2008
          • 8

          #5
          Originally posted by JosAH
          Nope, those components aren't psychic, i.e. they don't know when they are
          supposed to load or save their content. You have to tell them that one way
          or another.

          That could be a nice little project: some sort of persistency 'framework' to which
          objects can register. You just push that little framework and all registered objects
          load and save whatever they have to load or save. That would be quite doable:
          a few interfaces and a bit of implementation:

          Code:
          public interface Persistable {
             public void load(InputStream is) throws IOException;
             public void save(OutputStream os) throws IOException;
          }
          ... but I get carried away; I leave that little project up to you and to anyone else
          who reads this ;-)

          kind regards,

          Jos

          Thanx Jos youve been a tremenedous help

          Comment

          Working...