Posting to array list - java

i am looking to pass values entered in my Textfields into an array list, although i cannot seem to do this. i am able to view the details which have been entered in the console when using
System.out.println(houses.get(1).getHouseNumber())
but this does not post the newly entered values into the arraylist in my code and i can not understand why.
ArrayList and ActionListener code
final ArrayList<House> houses = new ArrayList<House>();
houses.add(new House());
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
House house = new House();
house.setHouseNumber(houseNumber.getText());
house.setStreetName(streetName.getText());
house.setTown(town.getText());
house.setPostcode(postcode.getText());
houses.add(house);
System.out.println(houses.get(1).getHouseNumber());
System.out.println(houses.get(1).getStreetName());
System.out.println(houses.get(1).getTown());
System.out.println(houses.get(1).getPostcode());
}
});
GUI Code
public void go(){
frame = new JFrame();
panel = new JPanel();
HouseNumberLabel = new JLabel ("House Number");
houseNumber = new JTextField ("");
StreetNameLabel = new JLabel ("Street name");
streetName = new JTextField ("");
TownLabel = new JLabel ("Town");
town = new JTextField ("");
PostcodeLabel = new JLabel ("Postcode");
postcode = new JTextField ("");
BedsLabel = new JLabel ("Number of beds");
beds = new JTextField ("");
PriceLabel = new JLabel ("Price (£)");
price = new JTextField ("");
TypeLabel = new JLabel ("Building Type");
type = new JTextField ("");
button = new JButton("Submit");
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true)
House Class code
class House {
private String houseNumber;
private String streetName;
private String town;
private String postcode;
public String getHouseNumber() {
return houseNumber;
}
public void setHouseNumber(String houseNumber) {
this.houseNumber = houseNumber;
}

String houseNumber;
This variable is not needed, get rid of it;
JTextField HouseNumber1;
This does not follow proper java naming conventions. Variable names do not start with an upper cased character. The variaible name should be:
JTextField houseNumber;
Now in the ActionListener you get the text from the text field directly:
house.setHouseNumber( houseNumber.getText() );
Edit:
okButton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println( "ok" );
}
});

used it to print out the array and was presented with [demo2.House#e2eec8, demo2.House#aa9835].
House#e2eec8 and House#aa9835refer to the first and second house in the ArrayList, respectively. Try something like this instead:
System.out.println(houses.get(0).getHouseNumber());
Addendum:
I cannot see the details in the ArrayList in my code.
You can override toString() in House; that way you can just do this:
System.out.println(houses.get(0));
Addendum:
This does not post the newly entered values into the ArrayList.
System.out.println(houses.get(1).getHouseNumber());
Right, this always gets the second entry, starting from 0. The following will always get the last one sent to add().
System.out.println(houses.get(houses.size() - 1).getHouseNumber());
Addendum: You can loop through the houses like this.
for (House h : houses) {
System.out.print(h.getHouseNumber());
System.out.print(h.getStreetName());
...
}

Related

The reason why I cannot find the value that I enter first from JTextField

Here is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class Phone{
private String name;
private String phone_number;
private String address;
public Phone(String name,String phone_number, String address) {
this.name = name;
this.phone_number = phone_number;
this.address = address;
}
String getName() {return this.name;}
String getNumber() {return this.phone_number;}
String getAddress() {return this.address;}
}
public class Phone_Book extends JFrame{
private JTextArea ta = new JTextArea();
private JButton lookup = new JButton("lookup");
private JButton search = new JButton("search");
private JButton input = new JButton("input");
private JButton remove = new JButton("remove");
private JLabel name = new JLabel("name");
private JLabel phone_number = new JLabel("phone_number");
private JLabel address = new JLabel("address");
private JTextField name_input = new JTextField();
private JTextField phone_number_input = new JTextField();
private JTextField address_input = new JTextField();
private HashMap<String,Phone> hashPhoneBook = new HashMap<String, Phone>();
public Phone_Book() {
setTitle("Phone Book");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
//*********************PhoneBook Design ********************************************
JPanel buttonPanel = new JPanel();
buttonPanel.add(inquiry);
buttonPanel.add(search);
buttonPanel.add(input);
buttonPanel.add(remove);
buttonPanel.setLayout(new GridLayout(1,4));
buttonPanel.setSize(350,30);
buttonPanel.setLocation(670,70);
JPanel labelPanel = new JPanel();
labelPanel.add(name);
labelPanel.add(phone_number);
labelPanel.add(address);
labelPanel.setLayout(new GridLayout(3,1));
labelPanel.setSize(80,150);
labelPanel.setLocation(670,110);
JPanel textPanel = new JPanel();
textPanel.add(name_input);
textPanel.add(phone_number_input);
textPanel.add(address_input);
textPanel.setLayout(new GridLayout(3,1,0,25));
textPanel.setSize(260, 140);
textPanel.setLocation(750, 120);
JScrollPane js = new JScrollPane(ta);
js.setSize(600, 300);
js.setLocation(20, 10);
c.add(js);
c.add(buttonPanel);
c.add(labelPanel);
c.add(textPanel);
//********************** PhoneBook Function **************************************************
...
//---- problem occurs-------------------------------------
search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setText(" ");
Phone p = hashPhoneBook.get(name_input.getText());
if(p == null) ta.append(name_input.getText()+"doesn't exist\n");
else {
ta.append(p.getName()+" "+p.getNumber()+" "+p.getAddress()+"\n");
}
name_input.setText(" ");
}
});
//------------------------------------------------------
...
setSize(1100,400);
setVisible(true);
}
public static void main(String[] args) {
new Phone_Book();
}
}
Here is the problem.
I can see all values (including a first value that I first entered after running a program) when I click the 'lookup'button (I thought it isn't needed, I didn't put it in this code).
When I try to find or remove the first value from HashMap, it didn't work.
Only I got the 'null'
But, when I pressed the 'space' and entered the value, it worked well.
(for example, 'David' -----> ' David')
I wonder why this is happening?
It seems like the keys in your HashMap are not set up right. You haven't included the code where this is set.
IMO though, I'd implement the phone book with a simple line delimited text file & load all records in a string instead of using a HashMap for your phone book. The reason being that you can then use regular expressions to match case-insensitive and partial records. Using a HashMap, the key must be an exact match

ArrayList in ActionListener isnt working

i have made a java program named DoctorsCare in which patients can book their appointment with a doctor. So in the appointment panel I have included patient id, name, gender, date of birth, address and a brief patient history.
All these string values will be taken in an array list and will eventually be returned to a new tab (Doctors tab) after the appointment form is filled and the submit button is clicked. the the array list code I wrote has some problems but I can run the program.
I just need to know where I made the mistake and how can I return the array list values to the doctors tab.
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.Normalizer.Form;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.*;
public class NewPage extends JFrame implements ActionListener{
JFrame frame1= new JFrame();
JTextField id;
JLabel label = new JLabel("Patient ID:");
JLabel label2 = new JLabel("Name:");
JLabel label3 = new JLabel("Gender:");
JLabel label4 = new JLabel("Date of Birth:");
JLabel label5 = new JLabel("Address:");
JLabel label7 = new JLabel("Phone:");
JLabel label6= new JLabel("brief patient history:");
JPanel panel1 = new JPanel();
JTextArea phistry = new JTextArea(11,31);
JTextField text1 = new JTextField(20);
JTextField text2 = new JTextField(20);
JTextField text3 = new JTextField(20);
JTextField text4 = new JTextField(20);
JTextField text5 = new JTextField(20);
JTextField text6 = new JTextField();
JTextField text7 = new JTextField(20);
JButton button = new JButton("SUBMIT");
NewPage()
{
frame1.setTitle("Booking Appointment");
frame1.setVisible(true);
frame1.setSize(330,470);
frame1.add(panel1);
panel1.add(label);
panel1.add(text1);
panel1.add(label2);
panel1.add(text2);
panel1.add(label3);
panel1.add(text3);
panel1.add(label4);
panel1.add(text4);
panel1.add(label5);
panel1.add(text5);
panel1.add(label7);
panel1.add(text7);
panel1.add(label6);
panel1.add(phistry);
panel1.add(button);
button.setPreferredSize(new Dimension(160,40));
button.addActionListener(this);
}
\\the button code
public void actionPerformed(ActionEvent e) {
List<List<String>> model = new ArrayList<List<String>>();
text1.selectAll(); \\selects the input from user
text2.selectAll();
text3.selectAll();
text4.selectAll();
text5.selectAll();
text6.selectAll();
String ID = text1.getSelectedText(); \\initializes ID
String PName = text2.getSelectedText();
String Gender = text3.getSelectedText();
String DoB = text4.getSelectedText();
String Address = text5.getSelectedText();
//String phone = text7.getSelectedText();
String phistry = text6.getSelectedText();
//String phistry = text6.getSelectedText();
List<String> line = Arrays.asList(new String[]{ID, PName, Gender, DoB, Address, phistry});
model.add(line);
StringBuilder sb = new StringBuilder();
sb.append("ID\tFirst\tLast\tCourse\tYear\n");
for(List<String> input : model) {
for (String item : input) {
sb.append(item);
if (input.indexOf(item) == input.size()-1) {
sb.append("\n");
} else {
sb.append("\t");
}
}
}
}
}
First your List contains the model information, but you haven't set it in the text area you have like below in the actionPerformed method,
this.phistry.setText(sb.toString());
Assuming that you have the Doctors tab in a separate class like below (else you can set the model directly in the NewFrame,
class DoctorsPanel extends JPanel {
private List<List<String>> model;
JTextArea history;
public DoctorsPanel() {
model = new LinkedList<>();
history = new JTextArea(11, 31);
setLayout(new GridLayout(1, 1));
add(history);
}
public void setModel(List<List<String>> model) {
this.model = model;
setHistory();
}
private void setHistory() {
this.history.setText(getModelData());
}
private String getModelData() {
StringBuilder sb = new StringBuilder();
sb.append("ID\tFirst\tLast\tCourse\tYear\n");
for (List<String> input : model) {
for (String item : input) {
sb.append(item);
if (input.indexOf(item) == input.size() - 1) {
sb.append("\n");
} else {
sb.append("\t");
}
}
}
return sb.toString();
}
}
You can have your tabbed pane like below in the NewFrame,
JTabbedPane jTab = new JTabbedPane();
panel1 = new JPanel();
panel2=new DoctorsPanel();
jTab.add("Book", panel1);
jTab.add("Doctors", panel2);
frame1.add(jTab);
Then you can set it whenever the appointment is made. i.e. when an action performed on tab1 component
public void actionPerformed(ActionEvent e) {
List<List<String>> model = new ArrayList<List<String>>();
String ID = text1.getText();
String PName = text2.getText();
String Gender = text3.getText();
String DoB = text4.getText();
String Address = text5.getText();
String phistry = text6.getText();
List<String> line = Arrays.asList(new String[]{ID, PName, Gender, DoB, Address, phistry});
model.add(line);
StringBuilder sb = new StringBuilder();
sb.append("ID\tFirst\tLast\tCourse\tYear\n");
for(List<String> input : model) {
for (String item : input) {
sb.append(item);
if (input.indexOf(item) == input.size()-1) {
sb.append("\n");
} else {
sb.append("\t");
}
}
}
this.phistry.setText(sb.toString());//sets the text in tab1
panel2.setModel(model);//sets the model in Doctors panel
}
But I suggest to keep the variable model List at class NewFrame as a member rather than a local variable to hold the previously added information

Adding elements from JTextField to ArrayList

so I have an assignment that include GUI that contain:
JTextField that lets user enter an input into the text field
AddButton adds the input into an ArrayList
ListButton displays the content of ArrayList in the JTextArea
The problem is: When I enter [1,2,3] the output only show me the last element [3], how to fix this?
public class Example extends JFrame {
private final int WINDOW_WIDTH = 300;
private final int WINDOW_HEIGHT = 300;
private JPanel panel1, panel2;
private JLabel messageLabel;
private JTextField input;
private JTextArea output;
private JButton addButton, listButton;
String userInput;
ArrayList<String> list = new ArrayList<>();
public Example() {
setTitle("Array Exercise");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
panel1 = new JPanel();
panel1.setLayout(new GridLayout(6,1));
messageLabel = new JLabel("Input");
input = new JTextField(5);
addButton = new JButton("Add");
listButton = new JButton("List");
panel1.add(messageLabel);
panel1.add(input);
panel1.add(addButton);
panel1.add(listButton);
add (panel1, BorderLayout.WEST);
output = new JTextArea(12, 10);
panel2 = new JPanel();
panel2.add(output);
add(panel2, BorderLayout.EAST);
setVisible(true);
input.requestFocus();
ButtonListener bh = new ButtonListener();
addButton.addActionListener(bh);
listButton.addActionListener(bh);
}
private class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
userInput = input.getText();
if (e.getSource() == addButton) {
list.add(userInput);
output.setText(userInput + " added.");
input.setText("");
input.requestFocus();
}
else if (e.getSource() == listButton) {
list.stream().forEach(x-> output.setText(x));
}
}
}
The JTextField its text to a new text for each element in your list. What you have to do is something like this instead:
for(String s : list){
outPut.append(s + "\n");
}
//Use "\n" if you want each string on a new line. Otherwise, skip it
(I used this type of for-loop for clarity)
Now it "adds" (appends) each new String in your list. In your code, it sets the text to the first String. The sets the text to the second String (and so on). So remember to clear the area with perhaps setText(""); every time you want to print out, for example, a new list of Strings.
complementing Oskar's answer:
setTest() sets the whole text of the text area, overwriting the previous content. So the last value set is the (only) one that will be saved at the end.
Since already using Streams, you could use:
output.setText(list.stream().collect(Collectors.joining("\n")));

Attempting to get GUI to cycle through data starting back at the beginning

I am attempting to get the GUI to fully cycle through
but I run into an error after third next button click array element[2]. What I need to do is have the whole thing cycle through when clicking the next button. once it gets to the last iteration of the array will need to go back to the beginning. The thing also is the array is sorted alphabetically to begin with so it would need to start with titles [3] once it has cycled through. Thanks for all the help
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame implements ActionListener {
JButton next;
JButton previous;
JButton first;
JButton last;
private JLabel itmNum = new JLabel("Item Number: ", SwingConstants.RIGHT);
private JTextField itemNumber;
private JLabel proNm = new JLabel ("Product Name: ", SwingConstants.RIGHT);
private JTextField prodName;
private JLabel yr = new JLabel("Year Made: ", SwingConstants.RIGHT);
private JTextField year;
private JLabel unNum = new JLabel("Unit Number: ", SwingConstants.RIGHT);
private JTextField unitNumber;
private JLabel prodPrice = new JLabel("Product Price: ", SwingConstants.RIGHT);
private JTextField price;
private JLabel restkFee = new JLabel("Restocking Fee", SwingConstants.RIGHT);
private JTextField rsFee;
private JLabel prodInValue = new JLabel("Product Inventory Value", SwingConstants.RIGHT);
private JTextField prodValue;
private JLabel totalValue = new JLabel("Total Value of All Products", SwingConstants.RIGHT);
private JTextField tValue;
private double toValue;
Movies[] titles = new Movies[9];
int nb = 0;
public GUI()
{
super ("Inventory Program Part 5");
setSize(800,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();
first = new JButton("First");
previous = new JButton("Previous");
next = new JButton("Next");
last = new JButton("Last");
first.addActionListener(this);
previous.addActionListener(this);
next.addActionListener(this);
last.addActionListener(this);
//Movies[] titles = new Movies[9];
titles [0] = new Movies(10001, "King Arthur", 25 , 9.99, 2004, .05);
titles [1] = new Movies(10002,"Tron", 25, 7.99, 1982, .05);
titles [2] = new Movies(10003, "Tron: Legacy",25,24.99,2010,.05);
titles [3] = new Movies(10004,"Braveheart", 25,2.50,1995,.05);
titles [4] = new Movies(10005,"Gladiator",25,2.50,2000,.05);
titles [5] = new Movies(10006,"CaddyShack SE",25,19.99,1980,.05);
titles [6] = new Movies (10007,"Hackers",25,12.50,1995,.05);
titles [7] = new Movies (10008,"Die Hard Trilogy",25,19.99,1988,.05);
titles [8] = new Movies (10009,"Terminator",25,4.99,1984,.05);
Arrays.sort (titles, DVD.prodNameComparator);
itemNumber = new JTextField(Double.toString(titles[3].getitemNum()));
prodName = new JTextField(titles[3].getprodName());
year= new JTextField(Integer.toString(titles[3].getYear()));
unitNumber= new JTextField(Integer.toString(titles[3].getunitNum()));
price= new JTextField(Float.toString(titles[3].getprice()));
rsFee= new JTextField(Double.toString(titles[3].getRestkFee()));
prodValue= new JTextField(Double.toString(titles[3].getprodValue()));
tValue= new JTextField("2636");
nb=0;
next.addActionListener(this);
setLayout(new GridLayout(8,4));
add(itmNum);
add(itemNumber);
add(proNm);
add(prodName);
add(yr);
add(year);
add(unNum);
add(unitNumber);
add(prodPrice);
add(price);
add(restkFee);
add(rsFee);
add(prodInValue);
add(prodValue);
add(totalValue);
add(tValue);
add(first);
add(previous);
add(next);
add(last);
setLookAndFeel();
setVisible(true);
}
public void updateFields()
{
nb++;
itemNumber.setText(Double.toString(titles[nb].getitemNum()));
prodName.setText(titles[nb].getprodName());
year.setText(Integer.toString(titles[nb].getYear()));
unitNumber.setText(Integer.toString(titles[nb].getunitNum()));
price.setText(Double.toString(titles[nb].getprice()));
rsFee.setText(Double.toString(titles[nb].getRestkFee()));
prodValue.setText(Double.toString(titles[nb].getprodValue()));
}
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
if (source == next)
{
if (titles[nb]== titles[8])
{
titles[nb] = titles[0];
}
else {
nb++;
}
updateFields();
}
}
private void setLookAndFeel()
{
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.err.println("couln't use the system"+ "look and feel: " + e);
}
}
}
The reason you are advancing so quickly through the records is that you have added your ActionListener to your next JButton twice:
next.addActionListener(this);
This, in turn, increments your record index (nb) in the ActionListener as well as in your updateFields method. Remove this increment from one of these locations.
Also, when you check if you've reached the last title, you never reset your record index nb. You could do:
if (titles[nb] == titles[8]) {
nb = 0;
...

Using ArrayList in a JPanel

Class SampleFiveA extends JPanel. This contains textfields, one below the other, each of which has a label on the left. All textfields will be of the same width and positioned against the right border of the panel. SampleFiveA has only one constructor that accepts the following three parameters:
ArrayList names,
ArrayList values,
int cols
I so far created the sample username password screen in GUI but now I have a problem implementing an ArrayList in JPanel one for User Name and the other for password. Kind of stuck there for hours now cant find a proper example to do it. Below is the code I commented what I need to be done using ArrayList.
public class SampleFiveA extends JPanel {
ArrayList<String> names = new ArrayList<String>(); //the text for the labels
ArrayList<String> values = new ArrayList<String>(); // the initial contents of the text fields
int col ; //the number of columns used to set the width of each textfield
public SampleFiveA()
{
JPanel p = new JPanel();
p.setLayout(new GridLayout(2,2));
JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
p.add(lab1 = new JLabel("User Name"));
JTextField txt1 = new JTextField("User Name", JTextField.RIGHT);
p.add(txt1= new JTextField());
JLabel lab2 = new JLabel("Password ", JLabel.LEFT);
p.add(lab2 = new JLabel("Password"));
JPasswordField txt2 = new JPasswordField("*****",JPasswordField.RIGHT );
p.add(txt2 = new JPasswordField());
// names.add(lab1,lab2);// Not right but I want to put the label text to this arrayList
// values.add(txt1,txt2);//
add(p);
};
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.getContentPane().add(new SampleFiveA());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
};
};
you can use
names.add(txt1.getText());
values.add(txt2.getText());
but maybe you should think about a better data structure, e.g. a HashMap and
hashmap.put(txt1.getText(),txt2.getText())
(and you should do this based on some event,e.g. user presses a button, not in the constructor, as otherwise the value will be the one you set before)
Here's a start for you.
It adds a FocusListener to the text fields and makes sure that the content of the ArrayList is updated with the current value when the text field looses focus.
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class Main extends JPanel {
ArrayList<String> names = new ArrayList<String>(); // the text for the
// labels
ArrayList<String> values = new ArrayList<String>(); // the initial contents
// of the text fields
int col; // the number of columns used to set the width of each textfield
public Main() {
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 2));
names = new ArrayList<String>();
values = new ArrayList<String>();
JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
p.add(lab1);
JTextField txt1 = new JTextField("User Name", JTextField.RIGHT);
p.add(txt1);
names.add(lab1.getText());
values.add(txt1.getText());
JLabel lab2 = new JLabel("Password ", JLabel.LEFT);
p.add(lab2);
JPasswordField txt2 = new JPasswordField("*****", JPasswordField.RIGHT);
p.add(txt2);
names.add(lab2.getText());
values.add(txt2.getText());
// names.add(lab1,lab2);// Not right but I want to put the label text to
// this arrayList
// values.add(txt1,txt2);//
txt1.addFocusListener(new ArrayListFocusListener(txt1, values, 0));
txt2.addFocusListener(new ArrayListFocusListener(txt2, values, 1));
add(p);
// Start a thread to print the content of the list for 10 seconds
new Thread() {
public void run() {
for (int i = 0; i < 10; i++) {
try {
sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(values);
}
}
}.start();
};
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new Main());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
};
class ArrayListFocusListener implements FocusListener {
JTextField textField;
ArrayList<String> backingList;
int myIndex;
public ArrayListFocusListener(JTextField textField,
ArrayList<String> backingList, int myIndex) {
this.textField = textField;
this.backingList = backingList;
this.myIndex = myIndex;
}
public void focusGained(FocusEvent e) {
}
#Override
public void focusLost(FocusEvent e) {
backingList.set(myIndex, textField.getText());
}
}
};
I'm sure what you are trying to do. You either want to put the JLabel in an ArrayList or the text of that label.
If you want to put the whole JLabel in an ArrayList, you should make a ArrayList<JLabel>. But I take it you want to get the text from the JLabel, so you should write names.add(lab1.getText());.
The constructor you have made doesn't take any parameters. The parameters you have wrote are the instance variable, meaning those are the variables any instance of that class will have. If you want to pass parameters in your constructor you should do what thasc told you.
You write:
JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
p.add(lab1 = new JLabel("User Name"));
But since you are already creating the lab1 JLabel you could just write p.add(lab1).
And a final note I think SampleFiveA should better extend JFrame unless you want it to extend JPanel to use it somewhere else. If you need it to be standalone you should change that.
cheers

Categories