问题
I been learning about java for some time now and even bought several books to help me along. i am now in at the point in my journey that i want to learn about collections. i am able to use array lists in a very basic ways but i would like to understand how they work when use in conjunction with J radio buttons and other swing components. I been working, watching YouTube videos and reading books such as "Introduction to Java Programming- Comprehensive, 10E" and "Java in a Nutshell - (6th E) O'Reilly " but i have not found the answer to my problem. i can get the predefined information to display in the J text area by using the following code individually. "Person" calls a second class where I have my setters and getters.
private void LuisActionPerformed(java.awt.event.ActionEvent evt) {
Person Luis = new Person("Luis ", "Peres\n", 1953);
Result.setText(String.valueOf(Luis));
}
But as you guys can see this is not using lists, instead i am writting each user individually in their individual action performed events. here is the code tha includes the collection/ list.
import java.util.*;
public class FinalProject extends javax.swing.JFrame {
public FinalProject() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jScrollPane1 = new javax.swing.JScrollPane();
DisplayText = new javax.swing.JTextArea();
Exit = new javax.swing.JButton();
ClearText = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
Maria = new javax.swing.JRadioButton();
Jose = new javax.swing.JRadioButton();
Josh = new javax.swing.JRadioButton();
Dahn = new javax.swing.JRadioButton();
Terry = new javax.swing.JRadioButton();
Clark = new javax.swing.JRadioButton();
Sasha = new javax.swing.JRadioButton();
Paul = new javax.swing.JRadioButton();
Irlen = new javax.swing.JRadioButton();
Mary = new javax.swing.JRadioButton();
Puttin = new javax.swing.JRadioButton();
James = new javax.swing.JRadioButton();
Jack = new javax.swing.JRadioButton();
Mark = new javax.swing.JRadioButton();
Jane = new javax.swing.JRadioButton();
Sal = new javax.swing.JRadioButton();
Perry = new javax.swing.JRadioButton();
Charley = new javax.swing.JRadioButton();
Anna = new javax.swing.JRadioButton();
Bruce = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
DisplayText.setEditable(false);
DisplayText.setColumns(20);
DisplayText.setLineWrap(true);
DisplayText.setRows(5);
DisplayText.setWrapStyleWord(true);
jScrollPane1.setViewportView(DisplayText);
Exit.setText("Exit");
Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitActionPerformed(evt);
}
});
ClearText.setText("Clear Text");
ClearText.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearTextActionPerformed(evt);
}
});
jPanel1.setBackground(new java.awt.Color(195, 205, 226));
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(153, 153, 255), new java.awt.Color(51, 51, 51)));
buttonGroup1.add(Maria);
Maria.setText("Maria");
Maria.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MariaActionPerformed(evt);
}
});
buttonGroup1.add(Jose);
Jose.setText("Jose");
Jose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JoseActionPerformed(evt);
}
});
buttonGroup1.add(Josh);
Josh.setText("Josh");
Josh.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JoshActionPerformed(evt);
}
});
buttonGroup1.add(Dahn);
Dahn.setText("Dahn");
Dahn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DahnActionPerformed(evt);
}
});
buttonGroup1.add(Terry);
Terry.setText("Terry");
buttonGroup1.add(Clark);
Clark.setText("Clark");
buttonGroup1.add(Sasha);
Sasha.setText("Sasha");
buttonGroup1.add(Paul);
Paul.setText("Paul");
buttonGroup1.add(Irlen);
Irlen.setText("Irlen");
buttonGroup1.add(Mary);
Mary.setText("Mary");
buttonGroup1.add(Puttin);
Puttin.setText("Puttin");
buttonGroup1.add(James);
James.setText("James");
buttonGroup1.add(Jack);
Jack.setText("Jack");
buttonGroup1.add(Mark);
Mark.setText("Mark");
buttonGroup1.add(Jane);
Jane.setText("Jane");
buttonGroup1.add(Sal);
Sal.setText("Sal");
buttonGroup1.add(Perry);
Perry.setText("Perry");
buttonGroup1.add(Charley);
Charley.setText("Charley");
buttonGroup1.add(Anna);
Anna.setText("Anna");
buttonGroup1.add(Bruce);
Bruce.setText("Bruce");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Perry)
.addComponent(Jack)
.addComponent(Irlen)
.addComponent(Terry)
.addComponent(Maria))
.addGap(28, 28, 28)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Charley)
.addComponent(Mark)
.addComponent(Jose)
.addComponent(Clark)
.addComponent(Mary))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Anna)
.addComponent(Jane)
.addComponent(Puttin)
.addComponent(Sasha)
.addComponent(Josh))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Sal)
.addComponent(Paul)
.addComponent(James)
.addComponent(Dahn)
.addComponent(Bruce))
.addGap(18, 18, 18))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Josh)
.addComponent(Dahn)
.addComponent(Jose)
.addComponent(Maria))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Paul)
.addComponent(Sasha)
.addComponent(Clark)
.addComponent(Terry))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(James)
.addComponent(Puttin)
.addComponent(Mary)
.addComponent(Irlen))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Sal)
.addComponent(Jane)
.addComponent(Jack)
.addComponent(Mark))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Bruce)
.addComponent(Anna)
.addComponent(Charley)
.addComponent(Perry))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(ClearText)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Exit))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(7, 7, 7)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Exit)
.addComponent(ClearText))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void ClearTextActionPerformed(java.awt.event.ActionEvent evt) {
DisplayText.setText("");
}
private void MariaActionPerformed(java.awt.event.ActionEvent evt) {
}
private void JoseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void JoshActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void DahnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FinalProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FinalProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FinalProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FinalProject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FinalProject().setVisible(true);
}
});
// list where all user reside
List<Person> ml = new ArrayList<>();
ml.add(new Person("Maria ", "Short\n", 2001));
ml.add(new Person("Jose ", "White\n", 1980));
ml.add(new Person("Josh ", "Brown\n", 1970));
ml.add(new Person("Dahn ", "Lee\n", 1977));
ml.add(new Person("Terry ", "Black\n", 1983));
ml.add(new Person("Clark ", "Pale\n", 1999));
ml.add(new Person("Sasha ", "Cole\n", 1989));
ml.add(new Person("Paul ", "Chan\n", 1966));
ml.add(new Person("Irlen ", "Bry\n", 1981));
ml.add(new Person("Mary ", "Williams\n", 1975));
ml.add(new Person("Puttin ", "Moreno\n", 1955));
ml.add(new Person("James ", "Blanco\n", 1960));
ml.add(new Person("Jack ", "Negro\n", 1963));
ml.add(new Person("Mark ", "Torres\n", 1957));
ml.add(new Person("Jane ", "Perez\n", 1995));
ml.add(new Person("Sal ", "Salas\n", 1992));
ml.add(new Person("Perry ", "Blah\n", 1973));
ml.add(new Person("Charley ", "Nunca\n", 1996));
ml.add(new Person("Anna ", "Cali\n", 2012));
ml.add(new Person("Bruce ", "Cola\n", 2009));
}
// Variables declaration - do not modify
private javax.swing.JRadioButton Anna;
private javax.swing.JRadioButton Bruce;
private javax.swing.JRadioButton Charley;
private javax.swing.JRadioButton Clark;
private javax.swing.JButton ClearText;
private javax.swing.JRadioButton Dahn;
private javax.swing.JTextArea DisplayText;
private javax.swing.JButton Exit;
private javax.swing.JRadioButton Irlen;
private javax.swing.JRadioButton Jack;
private javax.swing.JRadioButton James;
private javax.swing.JRadioButton Jane;
private javax.swing.JRadioButton Jose;
private javax.swing.JRadioButton Josh;
private javax.swing.JRadioButton Maria;
private javax.swing.JRadioButton Mark;
private javax.swing.JRadioButton Mary;
private javax.swing.JRadioButton Paul;
private javax.swing.JRadioButton Perry;
private javax.swing.JRadioButton Puttin;
private javax.swing.JRadioButton Sal;
private javax.swing.JRadioButton Sasha;
private javax.swing.JRadioButton Terry;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
Essentially what i want is for the user to be able to click on the Jradio button and the predefined data to appear in the jtextarea. Here is an alternative method that i am working on.
class Storage{
Person Juan = new Person("Juan ", "Short\n", 2001);
Person Jose = new Person("Jose ", "White\n", 1980);
Person Josh = new Person("Josh ", "Brown\n", 1970);
Person Dahn = new Person("Dahn ", "Lee\n", 1977);
Person Terry = new Person("Terry ", "Black\n", 1983);
Person Clark = new Person("Clark ", "Pale\n", 1999);
Person Sasha = new Person("Sasha ", "Cole\n", 1989);
Person Paul = new Person("Paul ", "Chan\n", 1966);
Person Irlen = new Person("Irlen ", "Bry\n", 1981);
Person Mary = new Person("Mary ", "Williams\n", 1975);
Person Puttin = new Person("Puttin ", "Moreno\n", 1955);
Person James = new Person("James ", "Blanco\n", 1960);
Person Jack = new Person("Jack ", "Negro\n", 1963);
Person Mark = new Person("Mark ", "Torres\n", 1957);
Person Jane = new Person("Jane ", "Perez\n", 1995);
Person Sal = new Person("Sal ", "Salas\n", 1992);
Person Perry = new Person("Perry ", "Blah\n", 1973);
Person Charley = new Person("Charley ", "Nunca\n", 1996);
Person Anna = new Person("Anna ", "Cali\n", 2012);
Person Bruce = new Person("Bruce ", "Cola\n", 2009);
List<Person> ml = new ArrayList<>();
protected Storage (){
ml.add(Juan);
ml.add(Jose); // and so on..
But again I can't figure out how to connect my Jradio button the predefined information, so that when the user click on the radio button the information displays in the JtextArea.
回答1:
First of all variable names should NOT start with an upper case character. I doubt your O'Rielly text book uses upper case characters.
Instead of storing your Person object in an ArrayList you can store then in a HashMap:
HashMap<String, Person> people = new HashMap<String, Person>();
people.put("Juan", new Person("Juan ", "Short\n", 2001));
people.put("Jose", new Person("Jose ", "White\n", 1980));
Now you can write a common ActionListener to be shared by all radio buttons:
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
Person person = people.get(command);
}
};
Once you have the Person object you can display the data from the Person in you text area.
Also, you shouldn't create the buttons one at a time. Instead create a Array (or ArrayList) of the names you want to use and then iterate through the Array. Something like:
String[] names =
{
"Juan",
"Jose"
};
for (String name: names)
{
JRadioButton button = new JRadioButton(name);
button.addActionListener( al );
panel.add( button );
}
来源:https://stackoverflow.com/questions/28892805/displaying-an-item-from-a-collection-using-jradio-button