Welcome to Dream.In.Code
Become a Java Expert!

Join 150,189 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,036 people online right now. Registration is fast and FREE... Join Now!




Holding Strings In a Text Area

 
Reply to this topicStart new topic

Holding Strings In a Text Area, Topic merged

Badboy69
24 Sep, 2008 - 02:34 AM
Post #1

New D.I.C Head
*

Joined: 30 Jul, 2008
Posts: 9

Hi there i have an application i will attach the source code for it basically what i would like for it to do is if i close the application it should hold the the information in the text area without deleting it and starting it empty again how do i hold the information in there also how would i code a clear butrton to clear a screen that i specifically want to clear lets sat for instance i want clear text area one how will i code a button like that your help will be greatly appreciated

Source Code For reservations class
CODE

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

    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");

    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 amountLabel = new Label("Amount Outstanding");
        TextField amountField = new TextField(15);
        Label numLabel = new Label("Number in Party");
        Choice numberOfGuests = 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);

    public Reservations()
    {
        this.setLayout(new BorderLayout());
            roomPanel.setLayout(new GridLayout(2,4,10,10));
            buttonPanel.setLayout(new FlowLayout());
            inputPanel.setLayout(new FlowLayout());


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

        }

        buttonPanel.add(bookButton);

        inputPanel.add(custNameLabel);
        inputPanel.add(nameField);
        inputPanel.add(custPhoneLabel);
        inputPanel.add(phoneField);
        inputPanel.add(amountLabel);
        inputPanel.add(amountField);
        inputPanel.add(numLabel);
        inputPanel.add(numberOfGuests);
            for(int i=1; i<=4;i++)
                numberOfGuests.add(String.valueOf(i));
        inputPanel.add(nonSmoking);
        inputPanel.add(smoking);

        add(buttonPanel, BorderLayout.SOUTH);
        add(inputPanel, BorderLayout.CENTER);
        add(roomPanel, BorderLayout.NORTH);

        bookButton.addActionListener(this);

        addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            }
            );
        }

        public static void main(String[] args)
        {
            Reservations f = new Reservations();
            f.setBounds(200,200,600,300);
            f.setTitle("Hotel Reservations");
            f.setVisible(true);
        }

        public void actionPerformed(ActionEvent e)
        {
            if (hidden.getState())
            {
                JOptionPane.showMessageDialog(null,"You must select Nonsmoking or smoking.","Error",JOptionPane.ERROR_MESSAGE);
            }

            else
            {
                int available = room.bookRoom(smoking.getState());

                if (available > 0)
                {
                    roomDisplay[available].setBackground(lightRed);
                    roomDisplay[available].setText(
                                                    roomDisplay[available].getText() +
                                                    "\n" +
                                                    nameField.getText() +
                                                    "\n" +"R"+"\n"+
                                                    amountField.getText()+
                                                    "Outstanding"+
                                                    phoneField.getText() +
                                                    "\nparty of " +
                                                    numberOfGuests.getSelectedItem()
                                                );

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

    void clearFields()
    {
        nameField.setText("");
        phoneField.setText("");
        amountField.setText("");
        numberOfGuests.select(0);
        nameField.requestFocus();
        hidden.setState(true);
    }
}

Source Code For rooms class

ublic class Rooms
{
    //declare class variables
    int numSmoking;
    int numNonSmoking;
    boolean occupied[];

    public Rooms(int non, int sm)
    {
        occupied = new boolean[sm+non];
        for(int i=0; i<(sm+non); i++)
        occupied[i] = false;
        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])
            {
                occupied[i] = true;
                roomNumber = i+1;
                i = end;
            }
        }
        return roomNumber;
    }
}

*next time please use code tags: code.gif - 1lacca
User is offlineProfile CardPM
+Quote Post

Badboy69
RE: Holding Strings In A Text Area
24 Sep, 2008 - 06:29 AM
Post #2

New D.I.C Head
*

Joined: 30 Jul, 2008
Posts: 9

Hi there i have an application i will attach the source code for it basically what i would like for it to do is if i close the application it should hold the the information in the text area without deleting it and starting it empty again how do i hold the information in there also how would i code a clear butrton to clear a screen that i specifically want to clear lets sat for instance i want clear text area one how will i code a button like that your help will be greatly appreciated

Source Code For reservations class
java

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

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");

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 amountLabel = new Label("Amount Outstanding");
TextField amountField = new TextField(15);
Label numLabel = new Label("Number in Party");
Choice numberOfGuests = 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);

public Reservations()
{
this.setLayout(new BorderLayout());
roomPanel.setLayout(new GridLayout(2,4,10,10));
buttonPanel.setLayout(new FlowLayout());
inputPanel.setLayout(new FlowLayout());


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

}

buttonPanel.add(bookButton);

inputPanel.add(custNameLabel);
inputPanel.add(nameField);
inputPanel.add(custPhoneLabel);
inputPanel.add(phoneField);
inputPanel.add(amountLabel);
inputPanel.add(amountField);
inputPanel.add(numLabel);
inputPanel.add(numberOfGuests);
for(int i=1; i<=4;i++)
numberOfGuests.add(String.valueOf(i));
inputPanel.add(nonSmoking);
inputPanel.add(smoking);

add(buttonPanel, BorderLayout.SOUTH);
add(inputPanel, BorderLayout.CENTER);
add(roomPanel, BorderLayout.NORTH);

bookButton.addActionListener(this);

addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}

public static void main(String[] args)
{
Reservations f = new Reservations();
f.setBounds(200,200,600,300);
f.setTitle("Hotel Reservations");
f.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if (hidden.getState())
{
JOptionPane.showMessageDialog(null,"You must select Nonsmoking or smoking.","Error",JOptionPane.ERROR_MESSAGE);
}

else
{
int available = room.bookRoom(smoking.getState());

if (available > 0)
{
roomDisplay[available].setBackground(lightRed);
roomDisplay[available].setText(
roomDisplay[available].getText() +
"\n" +
nameField.getText() +
"\n" +"R"+"\n"+
amountField.getText()+
"Outstanding"+
phoneField.getText() +
"\nparty of " +
numberOfGuests.getSelectedItem()
);

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

void clearFields()
{
nameField.setText("");
phoneField.setText("");
amountField.setText("");
numberOfGuests.select(0);
nameField.requestFocus();
hidden.setState(true);
}
}

Source Code For rooms class

ublic class Rooms
{
//declare class variables
int numSmoking;
int numNonSmoking;
boolean occupied[];

public Rooms(int non, int sm)
{
occupied = new boolean[sm+non];
for(int i=0; i<(sm+non); i++)
occupied[i] = false;
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])
{
occupied[i] = true;
roomNumber = i+1;
i = end;
}
}
return roomNumber;
}
}


Mod Edit: Please use code tags when posting your code. Code tags are used like so => code.gif

Thanks,
PsychoCoder smile.gif
User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: Holding Strings In A Text Area
24 Sep, 2008 - 03:39 PM
Post #3

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
well in order to store information after your application is closed i suppose the easiest way would be to persist it to a text file. and then read the information back from that file when your program starts back up
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Holding Strings In A Text Area
24 Sep, 2008 - 04:16 PM
Post #4

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
Mach1 is correct. When an application closes all active memory associated with it is removed and thus you lose your data. Saving the data to the disk in some format and reading it back, is the only way to accomplish what you want.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Holding Strings In A Text Area
24 Sep, 2008 - 05:38 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Please avoid double posting mad.gif
Topic merged

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:12AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month