7 Replies - 502 Views - Last Post: 01 December 2010 - 04:40 AM Rate Topic: -----

#1 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Help with protected textfields

Posted 30 November 2010 - 09:52 PM

I have two classes, one is my main GUI and the other is a popup window used to add a new student to an arraylist. I have the information from the arraylist displayed in JTextFields which I have set as protected. I'm trying to modify the text in the the main GUI JTextfields through the new window class but it doesn't seem to want to work. Here is my code:

mainGUI:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class GUImain extends JFrame implements ActionListener{
	private final int WIDTH = 550;
	private final int HEIGHT = 300;
	private Student student;
	private JMenuBar menuBar;
	private JMenu fileMenu, editMenu, helpMenu;
	private JMenuItem newItem, openItem, saveItem, exitItem;
	private JPanel panel, panel2;
	private JLabel fName1, lName1, id1, major1;
	protected JTextField fName2, lName2, id2, major2;
	private JButton previous, next, add, delete, exit;
	private AddNewWindow addWindow;
	private int position = 0;
	private Student startup;
	private static ArrayList<Student> students;
	private static ReadFile rFile;
	
	//GUI constructor
	public GUImain() {
		//sets parameters for the window
		setTitle("Final Project");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(WIDTH, HEIGHT);
		setLayout(new BorderLayout());
		setResizable(false);
		
		//adds an exit button to a panel
		exit = new JButton("Exit");
		panel = new JPanel();
		panel.add(exit);
		
		//adds the exit button panel and sets it to the center
		add(panel, BorderLayout.CENTER);
		
		//calls the buildPanel method
		buildPanel();
		
		//sets the text fields text to the first student
		fName2.setText(startup.getFirstName());
		lName2.setText(startup.getLastName());
		id2.setText(startup.getID());
		major2.setText(startup.getMajor());
		
		//sets the window to visible
		setVisible(true);
	}
	
	//buildPanel method
	public void buildPanel() {
		//instantiates startup with the first student
		startup = new Student(students.get(0));
		
		//instantiates panel2 and sets the layout to a gridlayout
		panel2 = new JPanel();
		panel2.setLayout(new GridLayout(7, 2));
		
		//instantiates all the buttons, labels, and textfields in the window
		fName1 = new JLabel("First Name");
		fName2 = new JTextField();
		lName1 = new JLabel("Last Name");
		lName2 = new JTextField();
		id1 = new JLabel("Student ID");
		id2 = new JTextField();
		major1 = new JLabel("Major");
		major2 = new JTextField();
		previous = new JButton("Previous");
		next = new JButton("Next");
		add = new JButton("Add New");
		delete = new JButton("Delete");
		
		//instantiates a menubar
		menuBar = new JMenuBar();
		
		//instantiates all menus and menuitems
		fileMenu = new JMenu("File");
		editMenu = new JMenu("Edit");
		helpMenu = new JMenu("Help");
		newItem = new JMenuItem("New");
		openItem = new JMenuItem("Open");
		saveItem = new JMenuItem("Save");
		exitItem = new JMenuItem("Close");
		
		//adds the newitem, openitem, separator and exititem to the filemenu
		fileMenu.add(newItem);
		fileMenu.add(openItem);
		fileMenu.addSeparator();
		fileMenu.add(exitItem);
		
		//adds menus to the filebar 
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(helpMenu);
		
		//adds the menubar to the window
		setJMenuBar(menuBar);
		
		//makes the display text areas not editable
		fName2.setEditable(false);
		lName2.setEditable(false);
		id2.setEditable(false);
		major2.setEditable(false);
		previous.setEnabled(false);
		
		//adds all the components to panel2
		panel2.add(fName1);
		panel2.add(fName2);
		panel2.add(lName1);
		panel2.add(lName2);
		panel2.add(id1);
		panel2.add(id2);
		panel2.add(major1);
		panel2.add(major2);
		panel2.add(previous);
		panel2.add(next);
		panel2.add(add);
		panel2.add(delete);
		
		//adds action listeners to the buttons
		add.addActionListener(this);
		exit.addActionListener(this);
		next.addActionListener(this);
		previous.addActionListener(this);
		delete.addActionListener(this);
		
		//adds the panel to the content pane and sets it to north
		add(panel2, BorderLayout.NORTH);
	}
	
	//actionlistener
	public void actionPerformed(ActionEvent e) {
		
		//action listener for the add button
		if(e.getSource() == add) {
			Student temp;
			
			//creates a new add window object
			addWindow = new AddNewWindow(students);
			System.out.println(students);
			System.out.println(students.size());
			
			if(students.size() == 0) {
				System.out.println(students);
			}
		}
		
		
		
		
		if(e.getSource() == previous) {
			Student temp;
			
			if(position > 0) {
				
				try {
					if(position == 1) {
						previous.setEnabled(false);
					}
					
					next.setEnabled(true);
					position--;
					temp = students.get(position);
					
					fName2.setText(temp.getFirstName());
					lName2.setText(temp.getLastName());
					id2.setText(temp.getID());
					major2.setText(temp.getMajor());
				}
				catch(IndexOutOfBoundsException ex) {
				}
			}
			else {
				previous.setEnabled(false);
			}
		}
		
		if(e.getSource() == next) {
			Student temp;
			if(position < students.size()-1) {
				
				try {
					if(position == students.size()-2) {
						next.setEnabled(false);
					}
					
					previous.setEnabled(true);
					position++;
					temp = students.get(position);
					
					fName2.setText(temp.getFirstName());
					lName2.setText(temp.getLastName());
					id2.setText(temp.getID());
					major2.setText(temp.getMajor());
				}
				catch(IndexOutOfBoundsException ex) {
				}
			}
			else {
				next.setEnabled(false);
			}
		}
		
		//deletes a file from the arraylist
		if(e.getSource() == delete) {
			Student temp;
			
			try {
				temp = students.get(0);
			}
			catch(IndexOutOfBoundsException ex) {
				JOptionPane.showMessageDialog(null, "No records exist to delete");
			}
			
			try {
				temp = students.get(position);
				
				if(students.size() == 2) {
					previous.setEnabled(false);
					next.setEnabled(false);
				}
				
				if(students.equals(temp)) {
					
				}
				
				students.remove(position);
				
				temp = students.get(position);
				fName2.setText(temp.getFirstName());
				lName2.setText(temp.getLastName());
				id2.setText(temp.getID());
				major2.setText(temp.getMajor());
				
			}
			catch(IndexOutOfBoundsException ex) {
				if(position > 0) {
					position--;
					temp = students.get(position);
					fName2.setText(temp.getFirstName());
					lName2.setText(temp.getLastName());
					id2.setText(temp.getID());
					major2.setText(temp.getMajor());
				}
				else if(position == 0) {
					fName2.setText("");
					lName2.setText("");
					id2.setText("");
					major2.setText("");
				}
				else {
					JOptionPane.showMessageDialog(null, "No more records to delete");
				}
			}
		}
		
		if(e.getSource() == exit) {
			System.exit(0);
		}
	}
	
	public static void main(String[] args) {
		//sets up the arraylist, reads the students_raw file and writes it to the arraylist
		students = new ArrayList<Student>();
		rFile = new ReadFile("students_raw.csv");
		students = rFile.readTextFile();
		
		GUImain window = new GUImain();
		
	}
}




addNewWindow:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class AddNewWindow extends JDialog implements ActionListener{
	private JLabel fName1, lName1, id1, major1;
	private JTextField afName2, alName2, aid2, amajor2;
	private JPanel panel1, panel2, panel3, panel4, buttonPanel;
	private JButton enter, cancel;
	private ArrayList<Student> list;
	private Student temp;
	
	public AddNewWindow(ArrayList<Student> s) {
		//performs a shallow copy
		list = s;
		
		//sets default settings
		setTitle("Add new Student");
		setSize(500, 400);
		setLayout(new GridLayout(5, 2));
		setResizable(false);
		
		temp = new Student();
		panel1 = new JPanel();
		panel2 = new JPanel();
		panel3 = new JPanel();
		panel4 = new JPanel();
		buttonPanel = new JPanel();
		fName1 = new JLabel("First Name");
		afName2 = new JTextField(10);
		lName1 = new JLabel("Last Name");
		alName2 = new JTextField(10);
		id1 = new JLabel("Student ID");
		aid2 = new JTextField(10);
		major1 = new JLabel("Major");
		amajor2 = new JTextField(10);
		enter = new JButton("Enter");
		cancel = new JButton("Cancel");
		
		//places components on panels
		panel1.setLayout(new GridLayout(1, 2));
		panel2.setLayout(new GridLayout(1, 2));
		panel3.setLayout(new GridLayout(1, 2));
		panel4.setLayout(new GridLayout(1, 2));
		buttonPanel.setLayout(new GridLayout(1, 2));
		
		//adds the action listener to buttons
		enter.addActionListener(this);
		cancel.addActionListener(this);
		
		//adds components to panels
		panel1.add(fName1);
		panel1.add(afName2);
		panel2.add(lName1);
		panel2.add(alName2);
		panel3.add(id1);
		panel3.add(aid2);
		panel4.add(major1);
		panel4.add(amajor2);
		buttonPanel.add(enter);
		buttonPanel.add(cancel);
		
		//adds panels to the content pane
		add(panel1);
		add(panel2);
		add(panel3);
		add(panel4);
		add(buttonPanel);
		pack();
		
		//sets visible
		setVisible(true);
	}
	
	public String getFName() {
		return temp.getFirstName();
	}
	
	public String getLName() {
		return temp.getLastName();
	}
	
	public String getID() {
		return temp.getID();
	}
	
	public String getMajor() {
		return temp.getMajor();
	}
		
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == enter) {
			//adds new student to the arraylist
			temp.setFirstName(afName2.getText());
			temp.setLastName(alName2.getText());
			temp.setID(aid2.getText());
			temp.setMajor(amajor2.getText());
			list.add(temp);
			this.setVisible(false);
		
                       //what should happen if the list only has one item in it
			if(list.size() == 1) {
				lName2.setText(temp.getFirstName());
				fName2.setText(temp.getLastName());
				id2.setText(temp.getID());
				major2.setText(temp.getMajor());
			}
		}
		
		if(e.getSource() == cancel) {
			//closes the window and doesn't save
			this.setVisible(false);
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with protected textfields

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Help with protected textfields

Posted 30 November 2010 - 10:14 PM

I have not looked in depth at your code, but what I see is that you have the TextFields showing what is contained in a Student object. Then, you change the Student in the JDialog class. This does NOT mean that the text boxes change. The text they contain is still the old text. If you pass the JDialog class the main GUI class, you can setText() on the textFields too.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with protected textfields

Posted 30 November 2010 - 10:20 PM

fName2.setText(temp.getLastName());

should be

someInstanceOfGuiMain.fName2.setText(temp.getLastName());

if you want your second class AddNewWindow to access a variable in GUIMain (protected only or not)
you will need the instance of GUIMain passed to AddNewWindow
May be you can passed it in the constructor of AddNewWindow

class A {
   protected JLabel x;

   ....
   new B(this);
   ...
}

class B {
    // constructor
    B(A a) {
       a.x = ...   can access the x of A here



Was This Post Helpful? 1
  • +
  • -

#4 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Help with protected textfields

Posted 30 November 2010 - 10:24 PM

View PostDogstopper, on 30 November 2010 - 09:14 PM, said:

I have not looked in depth at your code, but what I see is that you have the TextFields showing what is contained in a Student object. Then, you change the Student in the JDialog class. This does NOT mean that the text boxes change. The text they contain is still the old text. If you pass the JDialog class the main GUI class, you can setText() on the textFields too.


Well I made a shallow copy of the the student object from the gui class in the dialog class so the two are the same. The problem I'm having (which I should have specified in my first post) is the JDdialog class doesn't see these textfields to change.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with protected textfields

Posted 30 November 2010 - 10:27 PM

View Postgiggly kisses, on 30 November 2010 - 11:24 PM, said:

Well I made a shallow copy of the the student object from the gui class in the dialog class so the two are the same. The problem I'm having (which I should have specified in my first post) is the JDdialog class doesn't see these textfields to change.

It sees the Student but not the instance variables of the class that holds that Student
Was This Post Helpful? 0
  • +
  • -

#6 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Help with protected textfields

Posted 30 November 2010 - 10:28 PM

View Postpbl, on 30 November 2010 - 09:20 PM, said:

fName2.setText(temp.getLastName());

should be

someInstanceOfGuiMain.fName2.setText(temp.getLastName());

if you want your second class AddNewWindow to access a variable in GUIMain (protected only or not)
you will need the instance of GUIMain passed to AddNewWindow
May be you can passed it in the constructor of AddNewWindow

class A {
   protected JLabel x;

   ....
   new B(this);
   ...
}

class B {
    // constructor
    B(A a) {
       a.x = ...   can access the x of A here




Ok, I see what you're saying. To call the AddNewWindow method would I do something like this inside the actionlistener: new AddNewWindow(ArrayList, this);
Was This Post Helpful? 0
  • +
  • -

#7 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Help with protected textfields

Posted 30 November 2010 - 10:45 PM

Never mind to the post above, it works perfectly! Thank you, this problem was driving me nuts for 2 hours. I am wondering if you could help me with another problem involving the same program. I would like the user to not be able to use the main gui window while the add new window is open. In other words the add new window keeps focus until it is closed. How can I go about doing this?

EDIT: Sorry for double posting, just realized I did it right as I hit the submit button

This post has been edited by giggly kisses: 30 November 2010 - 10:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Help with protected textfields

Posted 01 December 2010 - 04:40 AM

Glad you solved your problem. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1