From leavens@larch.cs.iastate.edu Fri Oct 1 15:14:27 2004 Date: Fri, 1 Oct 2004 15:14:26 -0500 (CDT) From: Gary T. Leavens To: Drew Templeton Cc: Staff for Com S 362 -- Kun Liang , Matthew Ring Subject: Re: cs362 hw4 question Hi Drew, On Fri, 1 Oct 2004, Drew Templeton wrote: > Hi Prof. Leavens. I was struggling with a part of homework 4 and I was > wondering if you could see what I was doing wrong. My problem that i'm dealing > with is how to add an item to JComboBox for my list of named formulas from the > listener that I have set up with my save button. I tried to pass the named > formulas JComboBox into the save button listener and then add the item in > there, but that didn't seem to work out right, and I also tried having the > named formulas JComboBox set up with a vector, and then pass the vector and > add the name of the new formula onto it. I sorta got it to work by doing these > things, but it new showed up correctly, I could just see by clicking on the > combobox that there was a new space for an item, but no label. Thanks for the > help. What I did was roughly as follows: The "Save" button has an ActionListener whose actionPerformedMethod - extracts the register numbers from the register select combo boxes - extracts the operator selected from the operator select combo box - uses these to create an instance of FormulaType - uses FormulaMap.put to place a mapping from the entered formula name to this newly created FormulaType object - adds the name to the JComboBox under the label "Named Formulas" Then there is a listener for the JComboBox under the named formulas. This listener's actionPerformed methd fetches the text of the selected formula, and uses FormulaMap.get to fetch the FormulaType object. It sounds like what may be missing in your listener is the next step, which is to set as the text in the selected formula box to the output of calling the toString method on this formula. (You need to add a toString method to the formulas to have this last step work.) Does that help? Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 --------------------------------------------------- From leavens@larch.cs.iastate.edu Mon Oct 4 19:51:32 2004 Date: Mon, 4 Oct 2004 19:51:32 -0500 (CDT) From: Gary T. Leavens To: Ted Skjei Cc: cs362s@cs.iastate.edu Subject: Re: hw4 ques#2 Hi Ted, On Mon, 4 Oct 2004, Ted Skjei wrote: > > To construct a BinaryFormula (Add, Mult, or Diff), you pass two integers (which > eventually become reg1 and reg2 through inheritance). However, the Registers > array's elements, which will be passed to a BinaryFormula class, are stored as > doubles. Right. > Are we supposted to change the Add, Mult, and Diff constructors to > meet the values being passed to them (since doubles are passed to them) or are > we suppost to treat the reg1 and reg2 in like integers (even though they will be > passed as doubles when they're constructed). Neither. The ints are the numbers (indexes) of the registers. The Registers class stores doubles. The FormulaType subtypes store ints that refer to registers; these registers hold the doubles. Look, for example at the code for leftValue in BinaryFormula. > In addition, evaluate() (in > Formula) returns a double even though it is performing the evaluation on two > integers (reg1 and reg2). Hopefully you understand what I am saying. No, it fetches the doubles from the registers named by the ints reg1 and reg2. For example if the value in reg1 is the int 1 and if the value in reg2 is the int 2, then leftValue() returns the double that is in Registers.get(1), and rightValue() returns the double that is in Registers.get(2). Does that make sense? Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ----------------------------- From leavens@cs.iastate.edu Wed Oct 6 00:22:45 2004 Date: Wed, 6 Oct 2004 00:22:45 -0500 (CDT) From: Gary T. Leavens To: Greg Beck Cc: cs362s@cs.iastate.edu Subject: Re: Print outs for homework 4 Hi Greg, On Tue, 5 Oct 2004, Greg Beck wrote: > Did you want print outs of FormulaType, Formula, BinaryFormula, Diff, Mult, > and Sum? They were only edited slightly to add the toString method, but > require a bunch of paper to print out. I think you shouldn't have to change FormulaType to have toString in it (all Objects have toString). But the rest of them, you should print if you changed any code in them. Sorry for that. You can print them smaller if that's easy to do. Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 --------------------------- From leavens@cs.iastate.edu Wed Oct 6 00:48:32 2004 Date: Wed, 6 Oct 2004 00:48:32 -0500 (CDT) From: Gary T. Leavens To: Kun Liang Cc: Nic Lehman , "cs362s@cs.iastate.edu" Subject: Re: CS362 ActionListener Question Hi Nic, Kun has looked at the problem with the JComboBox instances. The only other thing I can think of is to be sure you are affecting the same instance of CalculatorFrame. About the organization question. If you are using nested classes, that's okay. My solution uses a bunch of anonymous nested classes, which are handy for writing listeners. As Kun says, it may be easier to follow if you split out these classes, and one way to do that is to pass the CalculatorFrame object to the listener's constructor as an argument. Then provide the necessary access methods in CalculatorFrame so that the information needed is accessible to the listeners. On Tue, 5 Oct 2004, Kun Liang wrote: > Nic Lehman, hai > > The JComboBox.addItem() method work fine with me, I can't tell what's wrong right from your description. > > In the listener for the SaveButton, you need to get info from 3 ComboBox and one TextField, then add item to the NamedFormula ComboBox, I actually pass the CalculatorFrame instance to the listener, then you can access almost everything:P > > Kun Liang > > ======= 2004-10-05 you write : ======= > >> Hello, >> >> I have homework 4 working properly or as far as I can tell it is, but I >> had to put all of the ActionListener events in the CalculatorFrame class. I >> assume I will be penalized for this so I would like some help, I am trying >> to create individual event handler classes for the different events, like >> for the SaveButton. I would like to send the JTextField to a constructor >> for a SaveButton Class, then in the ActionListener implementation I would >> like to getText from that JTextField and then be able to call an update >> function from an instance of CalculatorFrame to put the new name of the >> formula in the Name Formula ComboBox, but when I try to do this the name >> won't show up. I have added print statement and the string does appear in >> CalculatorFrame, but the addItem method won't add the string to the >> combobox. >> >> >> >> >> Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ------------------------------------- From luckbird@cs.iastate.edu Tue Oct 5 21:27:18 2004 Date: Tue, 5 Oct 2004 21:27:16 -0600 From: Kun Liang To: Matt Hall Cc: "cs362s@cs.iastate.edu" Subject: Re: hw4 [ The following text is in the "GB2312" character set. ] [ Your display is set for the "ISO-8859-1" character set. ] [ Some characters may be displayed incorrectly. ] Matt Hall, hai ĦĦĦĦA good way to learn it is to look at class RegisterInputListener which come with the hw package. Basically you could create a listener class for each event you attend to, for example I created a SaveButtonListener class to listen to the saveButton. JButton saveButton = new JButton("Save"); SaveButtonListener sbl = new SaveButtonListener(this); saveButton.addActionListener(sbl); You can also look up the examples at java.sun.com/docs/books/tutorial/uiswing/index.html :) Kun Liang ======= 2004-10-05 you write : ======= >Ive been hung up for awhile now, i have my gui displayed, but how do i use the >listener class to monitor my comboboxes and buttons? thanks, > >Matt Hall >http://www.ourlastchanceonline.com