The Artima Developer Community
Sponsored Link

Java Answers Forum
Question in Java Programming(GUI)

1 reply on 1 page. Most recent reply: May 4, 2009 11:46 PM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
bnafsaj alsaadi

Posts: 3
Nickname: bnafsaj
Registered: May, 2009

Question in Java Programming(GUI) Posted: May 1, 2009 7:02 AM
Reply to this message Reply
Advertisement
hi all
i need help with my code
i'm designing a frame that will ask the user to enter name, age , and id
then using add button all details will be entered and displayed in text area at the same frame,
using sort button, the details should be order according to the age
and display the orderd details again in the text area

i do not know how to sort, and i wrote the code but it does not work

i but the code in 2 packeges
this one in GUI package
package GUI;



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import java.util.*;

import Domain.StudentsDetails;

public class StudentsDetailFrame extends JFrame {

private JPanel pnlTxtArea, pnlStudentDetails, pnlButtons;
private JLabel lblAge,lblName,lblID;
private JTextField txtName,txtID, txtAge;
private JButton btnAdd,btnSort,btnExit;
private JTextArea jtaStudentsList;

public StudentsDetailFrame(){

pnlStudentDetails = new JPanel();
pnlStudentDetails.setLayout(new GridLayout (3,2,2,8));
lblName = new JLabel ("Student Name:");
lblID = new JLabel ("Student ID:");
txtName = new JTextField(5);
txtID = new JTextField(5);
lblAge = new JLabel("Student Age:");
txtAge = new JTextField(5);

//panel with LineBorder
pnlStudentDetails.setBorder(new LineBorder(Color.BLACK,2));

// setting the text color to red
lblName.setForeground(Color.RED);
lblID.setForeground(Color.RED);
lblAge.se tForeground(Color.RED);
pnlStudentDetails.add(lblName);
pnlStudentDetails.add(tx tName);
pnlStudentDetails.add(lblID);
pnlStudentDetails.add(txtID);
pnlStudentDe tails.add(lblAge);
pnlStudentDetails.add(txtAge);

pnlTxtArea =new JPanel();
pnlTxtArea.setLayout(new GridLayout(1,1,2,2));
pnlTxtArea.setBorder(new LineBorder(Color.BLACK,2));
pnlTxtArea.add(jtaStudentsList = new JTextArea("",1,2));
JScrollPane scrpn = new JScrollPane (jtaStudentsList);
jtaStudentsList.setLineWrap(true);
//jtaStudentsList.setFont(new Font ("Courier", Font.BOLD,14));
jtaStudentsList.setEditable(false);
pnlTxtArea.add(scrpn);

// panel for the buttons
pnlButtons = new JPanel();
pnlButtons.setLayout(new FlowLayout());
pnlButtons.add(btnAdd = new JButton("Add"));
pnlButtons.add(btnSort = new JButton("Sort"));
pnlButtons.add(btnExit = new JButton("Exit"));

// adding tool tip text to all the buttons
btnAdd.setToolTipText("click to add student details to the list");
btnSort.setToolTipText("sort the students in the list according to their age");
btnExit.setToolTipText("Exit from the frame");

// action listener for the exit button
btnExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);

}
});

// action listener for the reset button
btnSort.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
btnSortClicked();
}
}

);


// action listener for the add button
btnAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
btnAddClicked();
}
}

);

//create the frame

setLayout(new BorderLayout());
add(pnlStudentDetails, BorderLayout.NORTH);
add(pnlTxtArea, BorderLayout.CENTER);
add(pnlButtons, BorderLayout.SOUTH);



}
protected void btnAddClicked(){
String Name=txtName.getText();
String ID= txtID.getText();
int Age = Integer.parseInt(txtAge.getText());
Domain.StudentsDetails s= new Domain.StudentsDetails( Name,ID, Age);
//System.out.println(s);
StudentsDetails.students.add(s);
jtaStudentsList .append(s.toString());
jtaStudentsList.append("..n");

txtName.setText(null);
txtID.setText(null);
txtAge.setText(null);


}
protected void btnSortClicked(){
jtaStudentsList.setText(null);
for (Object element:StudentsDetails.treeSet){
jtaStudentsList.append(element.toString());
}


}



public static void main(String[] args) {
StudentsDetailFrame frame = new StudentsDetailFrame ();
frame.setTitle("Student Detail Frame");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setSi ze(370,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

}

the two other classes are in another package called Domain
this class StudentsDetails

import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;


public class StudentsDetails {
private String name;
private String id;
private int age;

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
// setter for ID
public void setId(String id) {
this.id = id;
}
// getter for the studen name
public String getName() {
return name;
}
//setter for the student name
public void setName(String name) {
this.name = name;
}

public static ArrayList students = new ArrayList ();
public static Set <StudentsDetails> treeSet = new TreeSet<StudentsDetails>(new SortByAge());

public StudentsDetails(String name,String id, int age){
this.name=name;
this.id=id;
this.age= age;
}



public String toString(){
return "Student ID : "+getId()+" Student Name : "+getName()+ " Age : "+getAge();


}


}

the second called SortByAge

import java.util.Comparator;


public class SortByAge implements Comparator <StudentsDetails>, java.io.Serializable{

public int Compare(StudentsDetails s1 , StudentsDetails s2){
int age1=s1.getAge();
int age2=s2.getAge();

if (age1 > age2)

return 1;

else
if (age1==age2)
return 0;

else
return -1;
}

public int compare(StudentsDetails a, StudentsDetails b) {

return 0;
}


i need help, what wrong with the code????


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Question in Java Programming(GUI) Posted: May 4, 2009 11:46 PM
Reply to this message Reply
Cannot follow this code, please use Java tags - see the "Formatting Your Post" to the right of the TextArea in which you type your Question.

Flat View: This topic has 1 reply on 1 page
Topic: Constructor, setters and getters Previous Topic   Next Topic Topic: reverse array in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use