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 JTextFi开发者_JS百科eld("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
精彩评论