I am designing a basic telephone directory for a project. It has three classes, Main (for the GUI), TelephoneDirectory (an object to store TelephoneRecords objects) and a TelephoneRecords class (where information for each record object is stored).
The requirements state: Extend your application by adding a list displaying the complete current contents of the telephone directory, ordered alphabetically by name. You will need to implement a ListModel. You may want to study class AbstractListModel before starting on your own implementation.
Only problem is, I have absolutely no idea how to extend my application to achieve this. I have searched online all night and haven't found a way to do this. I have tried storing the objects in an AbstractListModel rather than a HashMap but get errors. I don't exactly know what or why it is used and how I could use it. The next requirement (by the way) is to have the JList auto-update with new data when it's entered so I guess it has something to do with that?
Either way, if anyone could help it'd be great. My current working code for the previous that needs to be edited version is:
MAIN
public class Main extends JFrame implements ActionListener {
private static TelephoneDirectory directory = new TelephoneDirectory();
private JTextField nameField;
private JTextField numberField;
private JList contactList;
public Main() {
setTitle("Telephone Directory");
setLayout(new GridLayout(0,2));
JLabel nameLabel = new JLabel("Name of Contact:");
nameField = new JTextField(20);
add(nameLabel);
add(nameField);
JLabel numberLabel = new JLabel("Number of Contact:");
numberField = new JTextField(20);
add(numberLabel);
add(numberField);
JButton enterButton = new JButton("Enter");
JButton cancelButton = new JButton("Cancel");
enterButton.addActionListener(this);
cancelButton.addActionListener(this);
add(enterButton);
add(cancelButton);
JLabel contactsLabel = new JLabel("Current Contacts:");
contactList = new JList();
add(contactsLabel);
add(contactList);
setVisible(true);
pack();
}
public static void main(String[] args) {
new Main();
}
#Override
public void actionPerformed(ActionEvent arg0) {
JButton jb = (JButton) arg0.getSource();
if (jb.getText().equals("Cancel")) {
System.exit(0);
} else {
directory.addRecord(nameField.getText(), new TelephoneRecords(nameField.getText(), numberField.getText()));
System.out.println("Added record for " + nameField.getText() + ": number is " + numberField.getText() + ".");
}
}
}
TELEPHONEDIRECTORY
public class TelephoneDirectory implements Iterable<TelephoneRecords> {
private HashMap records;
public TelephoneDirectory() {
records = new HashMap<String, TelephoneRecords>();
}
public void addRecord(String name, TelephoneRecords newRecord) {
records.put(name, newRecord);
}
public TelephoneRecords getRecord(String name) {
return (TelephoneRecords) records.get(name);
}
public void getDirectory() {
System.out.println("Telephone Directory:");
records.values().iterator();
}
#Override
public Iterator<TelephoneRecords> iterator() {
return records.values().iterator();
}
}
TELEPHONERECORDS
public class TelephoneRecords {
private String name;
private String number;
public TelephoneRecords(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public String getNumber() {
return number;
}
#Override
public String toString() {
return "The phone number of " + name + " is " + number + ".";
}
}
You may be trying to do too much with inheritance. Rather than using an AbstractListModel in place of your HashMap, consider creating a class that extends AbstractListModel and that holds the TelephoneDirectory class with its HashMap as the nucleus of the AbstractListModel's data. This is called extending a class by composition rather than by inheritance.
Edit: Also consider using a TreeMap rather than a HashMap so as to be able to retrieve your names and telephone records in name order. You'll also need to give your TelephoneDirectory class a getElementAt(int index) and a getSize() method to allow it to be used within the AbstractListModel class.
Related
I'm writing a simple Java program.
First, the program asks user to input some info like name and id of the student and uses radio button for asking whether the student is present or absent. After finishing the inputs, then program validate whether predefined student names and inputs are match or not. And check id number again and spit out if input is over 4. Lastly check radio button is true or false. If one of the above two rules get error then program will quit without executing next method.
I have three .java files. One for UI. One for validation and one for storing data.
UI.java
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UI extends JFrame {
JTextField name = new JTextField("Name", 10);
JTextField id = new JTextField("Id", 10);
JRadioButton attendance = new JRadioButton("Present");
JButton JB = new JButton("Check");
public UI() {
super("Test");
JPanel JP = new JPanel();
JP.add(name);
JP.add(id);
JP.add(attendance);
JP.add(JB);
add(JP);
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void buttonAction(){
UI UIbutton = new UI();
UIbutton.JB.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == UIbutton.JB) {
String nameInput = UIbutton.name.getText();
int idInt = Integer.parseInt(UIbutton.id.getText());
boolean attInput = UIbutton.attendance.isSelected();
Validate.nameChk(nameInput);
Validate.idChk(idInt);
Validate.attChk(attInput);
Student studentObj = new Student(UIbutton.name.getText(), idInt, UIbutton.attendance.isSelected());
System.out.println(studentObj.name + "'s ID number is : " + studentObj.id + ".");
System.out.println(studentObj.name + " is present: " + studentObj.attendance);
System.exit(0);
}}});
}
public static void main(String[] args) {
buttonAction();
}
}
Validate.java
public class Validate {
public static void nameChk (String nameInput) {
String n1 = "Matthew";
String n2 = "John";
String n3 = "Mark";
String n4 = "Luke";
if ((nameInput.equalsIgnoreCase(n1))||
(nameInput.equalsIgnoreCase(n2))||
(nameInput.equalsIgnoreCase(n3))||
(nameInput.equalsIgnoreCase(n4))){
System.out.println("Your data is okay.");
}
else {
System.out.println("Error, wrong student name.");
System.exit(0);
}
}
public static void idChk (int idInt) {
if (idInt > 4) {
System.out.println("Your id is not correct.");
System.exit(0);
}
else {
System.out.println("Your id is correct.");
}
}
public static void attChk (boolean attInput) {
if (attInput) {
System.out.println("The student is present.");
} else {
System.out.println("The student is absent.");
}
}
}
Student.java
public class Student {
String name;
int id;
boolean attendance;
Student(String name, int id, boolean attendance) {
this.name = name;
this.id = id;
this.attendance = attendance;
}
}
What I want to know is how can I reuse output of that actionlister method somewhere else. Let say I would create foo.java class and use that studentObj variable to give grades like
System.out.println(studentObj.name+"has B+.");
Sort of.
How can I do that? How to turn that variable into global?
This can be achieved in different ways.
Quite simple, but not a good practice would be to create a Singleton. It would contain Students objects and you'll be able to access them from anywhere. Here is example with eager singleton, but you can implement much better versions (check about singleton implementations i.e. here https://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-examples)
public class StudentsSingleton {
private Map<Integer, Student> students = new HashMap<>();
public Student getStudent(int id) { return students.get(id);}
public void addStudent(Student s) { students.put(s.id, s);}
private static final StudentsSingleton instance = new StudentsSingleton();
//private constructor to avoid client applications to use constructor
private StudentsSingleton(){}
public static StudentsSingleton getInstance(){
return instance;
}
}
In that case, you can access it from anywhere by getting the instance :
StudentsSingleton.getInstance().getStudent(id);
A much better solution and a good practice would be to use some Dependency Injection framework i.e. Spring. In that case, you would create a Bean and inject it whenever it is needed to use.
I'm trying to create a student registration system. In this system, students can see course name, course credit, and the instructor of the course by clicking the "Courses" button.For this purpose i have a Courses class, a database, a frame and a JList courslist.
ArrayList<Courses> aq = Database.allCourses();
//allCourses() is a static method in my Database class that returns fields from my Database as an ArrayList<Courses>
courselist.setListData(Driver.converToCoursesArray(aq));
//Driver.converttoCoursesArray() is a static method in my Driver class that takes a ArrayList<Courses> as a paramater and returns a Courses[] array.
Now, my problem is that in my frame, JList always seen like p1.Courses#4532
I've seen a similar problem when i was accidently trying to print an object with System.out.println(). But in this situation i convert the arraylist to an array and my JList holds objects(JList). So i'll be happy if you help me.
You need to override toString() in the Course class, such that it returns the name of the course you want to display.
Take a look at this example:
import javax.swing.*;
import java.awt.*;
public final class Example extends JFrame {
public Example() {
Course[] courses = {
new Course("Course 1"),
new Course("Course 2"),
new Course("Course 3")
};
JList<Course> courseJList = new JList<>(courses);
getContentPane().add(courseJList);
pack();
setMinimumSize(new Dimension(200, 200));
setVisible(true);
}
public static void main(String[] args) {
new Example();
}
}
final class Course {
private final String courseName;
public Course(final String courseName) {
this.courseName = courseName;
}
#Override
public String toString() {
return courseName;
}
}
This displays the following:
I am able to expose a private JTextField by doing this:
public void setTextField(String value) {
someTF.setText(value);
}
It would be a lot of work if I have a lot of JTextFields. I tried doing this but failed. No error it's just not setting the right value on specified JTextField.
public class SomeView {
private JTextField someTF = new JTextField(10);
...
public void initComponents() {
...
}
public void setTextField(JTextField jTF, String value) {
jTF.setText(value);
}
}
public class SomeViewTable implements ...{
public void mousePressed(MouseEvent e) {
if (e.getSource() == someButton) {
JTextField someTF = new JTextField(10);
String value = "Some Value";
SomeView sv = new SomeView();
sv.initComponents();
sv.setTextField(someTF, value);
}
}
}
Im expecting this to happen in SomeView class when I called method sameTextField
someTF.setText("Some Value");
Is this possible, what rules in java am I breaking here?
In your listener, you are creating a local variable:
JTextField someTF = new JTextField(10);
...
sv.setTextField(someTF, value);
But what you want is to set the text field of SomeView. So remove the first line, and replace the second with:
sv.setTextField(sv.someTF, value);
Now, to answer the more global question of how to expose many private JTextFields through one method, one possibility could be to assign a string ID to each of them, and store them all in a HashMap:
Map<String,JTextField> map = new HashMap<String,JTextField>();
map.put("field 1", textField1);
...
map.put("field n", textFieldn);
public void setTextField(String id, String value) {
map.get(id).setText(value);
}
Or you could simply generate getters automatically for all your fields (most IDE do that painlessly)...
I'm having trouble putting multiple classes into a single file. For example, when my file looks like:
public class FirstClass() {}
public class SecondClass() {}
public class ThirdClass() {}
I get an error during compilation. I'm not quite sure what's causing this. Any ideas?
One Java file can consist of multiple classes with the restriction that only one of them can be public. As soon as you remove public keyword from your classes, you can combine them into a single Java file.
At the risk of spoon-feeding
Please read http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TheaterWindow extends JFrame
{
private JPanel pnlAdultTicketPrice, pnlAdultTicketsSold, pnlChildTicketPrice, pnlChildTicketsSold,
pnlCalculate, pnlMain;
private JLabel lblAdultTicketPrice, lblAdultTicketsSold, lblChildTicketPrice, lblChildTicketsSold;
private JTextField txtAdultTicketPrice, txtAdultTicketsSold, txtChildTicketPrice, txtChildTicketsSold;
private JButton btnCalculate;
public TheaterWindow()
{
// Sets window title
setTitle("Theater");
// Sets layout to BorderLayout
setLayout(new GridLayout(5,1));
// Specifies what happens when close button is clicked
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Builds the panels
buildPanels();
// Add the panels to the frame's content pane
add(pnlAdultTicketPrice);
add(pnlChildTicketPrice);
add(pnlAdultTicketsSold);
add(pnlChildTicketsSold);
add(pnlCalculate);
// Size the frame to fit all of the panels
pack();
// Display the window
setVisible(true);
}
private void buildPanels()
{
// Creates labels to display instructions
lblAdultTicketPrice = new JLabel("Adult ticket price");
lblChildTicketPrice = new JLabel("Child ticket price");
lblAdultTicketsSold = new JLabel("Adult tickets sold");
lblChildTicketsSold = new JLabel("Child tickets sold");
// Creates text fields that are 10 characters wide
txtAdultTicketPrice = new JTextField(10);
txtChildTicketPrice = new JTextField(10);
txtAdultTicketsSold = new JTextField(10);
txtChildTicketsSold = new JTextField(10);
// Creates button with caption
btnCalculate = new JButton("Calculate");
// Adds action listener to button
btnCalculate.addActionListener(new CalcButtonListener());
// Creates panels
pnlAdultTicketPrice = new JPanel();
pnlChildTicketPrice = new JPanel();
pnlAdultTicketsSold = new JPanel();
pnlChildTicketsSold = new JPanel();
pnlCalculate = new JPanel();
pnlMain = new JPanel();
// Adds elements to their proper panels
pnlAdultTicketPrice.add(lblAdultTicketPrice);
pnlAdultTicketPrice.add(txtAdultTicketPrice);
pnlChildTicketPrice.add(lblChildTicketPrice);
pnlChildTicketPrice.add(txtChildTicketPrice);
pnlAdultTicketsSold.add(lblAdultTicketsSold);
pnlAdultTicketsSold.add(txtAdultTicketsSold);
pnlChildTicketsSold.add(lblChildTicketsSold);
pnlChildTicketsSold.add(txtChildTicketsSold);
pnlCalculate.add(btnCalculate);
// Adds all of the above panels to a main panel
pnlMain.add(pnlAdultTicketPrice);
pnlMain.add(pnlChildTicketPrice);
pnlMain.add(pnlAdultTicketsSold);
pnlMain.add(pnlChildTicketsSold);
pnlMain.add(pnlCalculate);
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Creates object of Theater
Theater theater = new Theater();
// Sets the member variables of Theater to the user's input
theater.setAdultTicketPrice(Double.parseDouble(txtAdultTicketPrice.getText()));
theater.setChildTicketPrice(Double.parseDouble(txtChildTicketPrice.getText()));
theater.setAdultTicketsSold(Integer.parseInt(txtAdultTicketsSold.getText()));
theater.setChildTicketsSold(Integer.parseInt(txtChildTicketsSold.getText()));
// Creates DecimalFormat object for rounding
DecimalFormat dollar = new DecimalFormat("#.##");
// Display the charges.
JOptionPane.showMessageDialog(null, "Adult ticket gross: $" +
Double.valueOf(dollar.format(theater.getAdultGross())) + "\n" +
"Child ticket gross: $" + Double.valueOf(dollar.format(theater.getChildGross())) + "\n" +
"Adult ticket net: $" + Double.valueOf(dollar.format(theater.getAdultNet())) + "\n" +
"Child ticket net: $" + Double.valueOf(dollar.format(theater.getChildNet())) + "\n" +
"Total gross: $" + Double.valueOf(dollar.format(theater.getChildGross())) + "\n" +
"Total net: $" + Double.valueOf(dollar.format(theater.getTotalNet())));
}
}
public class Theater
{
private double PERCENTAGE_KEPT = 0.20;
private double adultTicketPrice, childTicketPrice;
private int adultTicketsSold, childTicketsSold;
public double getAdultGross()
{
return getAdultTicketPrice() * getAdultTicketsSold();
}
public double getAdultNet()
{
return PERCENTAGE_KEPT * getAdultGross();
}
public double getAdultTicketPrice()
{
return adultTicketPrice;
}
public int getAdultTicketsSold()
{
return adultTicketsSold;
}
public double getChildGross()
{
return getChildTicketPrice() * getChildTicketsSold();
}
public double getChildNet()
{
return PERCENTAGE_KEPT * getChildGross();
}
public double getChildTicketPrice()
{
return childTicketPrice;
}
public int getChildTicketsSold()
{
return childTicketsSold;
}
public double getTotalGross()
{
return getChildGross() + getAdultGross();
}
public double getTotalNet()
{
return getChildGross() + getChildNet();
}
public void setAdultTicketPrice(double adultTicketPrice)
{
this.adultTicketPrice = adultTicketPrice;
}
public void setAdultTicketsSold(int adultTicketsSold)
{
this.adultTicketsSold = adultTicketsSold;
}
public void setChildTicketPrice(double childTicketPrice)
{
this.childTicketPrice = childTicketPrice;
}
public void setChildTicketsSold(int childTicketsSold)
{
this.childTicketsSold = childTicketsSold;
}
}
}
Yes You can write your all classes in a single .java file, But you must have only one class public(if file name and class name same)
Example:
class A
{
}
class B
{
}
class C
{
}
I am assuming you are very beginner! Just copy paste all these contents in a single file TheaterDemo.java. And dont forget to remove all the public keyword in the beginning of class declaration.
One Java file can contain at most one top-level public class. That public top-level class can contain any number of public nested classes.
You can eliminate your compiler errors by any of the following approaches:
Moving the other classes into their own files. For example: FirstClass.java, SecondClass.java, and ThirdClass.java.
Nesting the classes whose name is not the file basename. For example:
public class FirstClass() {
public class SecondClass() {}
public class ThirdClass() {}
}
Removing the public qualifier from all but the one class whose name is the file basename. This approach has become less common after the introduction of nested classes in Java v1.1. For example, in file FirstClass.java, you could have:
public class FirstClass() {}
class SecondClass() {}
class ThirdClass() {}
From the Java Language Specification, section 7.6: Top Level Type Declarations:
If and only if packages are stored in a file system (§7.2), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
The type is referred to by code in other compilation units of the package in which the type is declared.
The type is declared public (and therefore is potentially accessible from code in other packages).
Just remove public from all other class definition and paste the code into TheaterDemo.java file
public class TheaterDemo
{
public static void main(String[] args)
{
TheaterWindow theaterWindow = new TheaterWindow();
}
}
//Here class code after removing public
// Here another class code
I see you have already done that kind of implementation. Please refer
private class CalcButtonListener implements ActionListener
in your TheaterWindow class.
By doing this, you are creating inner classes i.e. CalcButtonListener is an inner class of TheaterWindow class. Some concept you can extend to other classes.
There is no restriction on the number of class files in a java file.
But we can’t have more than one public class per source code file. Also the name of the file must match the name of the public class. Like, a class declared as public class Dog { } must be in a source code file named Dog.java.
And files with no public classes can have a name that does not match any of the classes in the file.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Test1{
JComboBox combo;
JTextField txt;
public static void main(String[] args) {
Test1 b = new Test1();
}
public Test1(){
String degrees[] = {"AAS1","AAS2","AAS1","AAS3"};
JFrame frame = new JFrame("Creating a JComboBox Component");
JPanel panel = new JPanel();
combo = new JComboBox(degrees);
combo.setEditable(true);
combo.setBackground(Color.gray);
combo.setForeground(Color.red);
txt = new JTextField(10);
txt.setText("1");
panel.add(combo);
panel.add(txt);
frame.add(panel);
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
txt.setText(String.valueOf(combo.getSelectedIndex()+1));
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
} }
As you see from the code above. I have JComboBox with 4 items. If there are no items that are same everything is OK.
But in my example ("AAS1","AAS2","AAS1","AAS3") first and third items are same, and I have problems in this case.
When I select any item I want to get it's index in JTextField, but when I select third item I get index of first item.
Has any idea?
That's because JComboBox is using equals to check the item equality. In your case, those two String are equal so it returns the first index that match. If you really need to do that, you might need to define your own item class like this:
private static class MyItem {
private String value;
public MyItem(String value) {
this.value = value;
}
public String getValue() {
return value;
}
#Override
public String toString() {
return value; //this is what display in the JComboBox
}
}
And then add the item like this:
MyItem degrees[] = {new MyItem("AAS1"),new MyItem("AAS2"),new MyItem("AAS1"),new MyItem("AAS3")};
JComboBox combo = new JComboBox(degrees);
Create a class like that:
class ComboItem{
private String name;
public ComboItem(String name){
this.name = name;
}
public String toString() {
return name;
}
}
and create your combobox:
comboBox = new JComboBox(new ComboItem[]{
new ComboItem("AAS1"),
new ComboItem("AAS2"),
new ComboItem("AAS1"),
new ComboItem("AAS3")
});
You have to separate how the equals is calculated on the Strings items and the effective representation. I think that this can be done just by creating a specific class for your purpose and use it instead that String.
Since this could be homework I'm not going to give exact result, just think about how the JComboBox internally chooses the index specified.
try using combo.getSelectedItem() instead. Since its two different strings in the String Array, you should be able to do a reference comparison and tell a difference between the two.