This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

User Input Parameters

 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am looking at how to support the user input for :-

  1. Server mode - DB location + port number

  2. Client Network mode - IP address for server machine + port number

  3. Client standalone mode - DB location



Been looking , and trying to follow, how Andrews book provides the above. I am also learning swing as I am going along. I wonder if the approach in the book is more than what is required for the assignment. It uses focus listener, observer, grid bag layout, a common dialog panel ?

I decided to re use Andrew’s approach but to simplify it. Below is what I have done.

I have an problem. When the user selects "connect", I would like to validate the parameters. I thought I would be able to do this is the "connect" action listener method. What seems to be happening is that when the user selects "connect", the action listener method is called and control is returned back to MainWindow(). Is there a way I can validate the input parameters and only allow control back to the MainWindow for valid parameters please ... or is there a better ( simpler approach ! ).

Regards

Pete





The ConfigurationDialog class is this



ConfigOptions class is
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the user inputs ultimately you need to store the config in some properties file. I suggest using Properties.store() and Properties.load() to write and read the config. This will make life easier.

As for the GUI as long as it prompt for the appropriate items is fine. I see you are using 1 JPanel for both local and remote setup - that's ok - but I separate them into their own classes.

For the checking, you need to consider these:
* for local = is the input file a valid database file (eg correct magic cookie value, has read/write permission etc)
* for remote = is the server ip address and port valid
* what to do if client can't connect to server or not the correct server port

You get the idea. At the end the config will be saved if "everything" matches else re-prompt user to enter again.
 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi K

Thank you for the quick response.

if "everything" matches else re-prompt user to enter again


It is the above I am having trouble with. In my approach, the ConfigurationDialog class
prompts the user for the appropriate information ( ip address + port ( remote), etc ). however, I would like to validate the parameters and re prompt the user of they are invalid, before proceeding further.
In the code provided, after the user has entered the parameters, the code always gets to line 20 of the MainWindow class. I would like to know how, I can ensure, line 20 is only hit after the user parameters have been validated. please.


Thank you

Pete
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK since you are using JOptionPane, you do need to check if the returned value = OK or CANCEL. Then you can use a while loop:


The way I did mine is to use a JFrame or JDialog for the setup window with a error pop up. As long my pop up runs the logic stops.
 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

I think I have done what was suggested as shown below.
Effectively, for the moment, it is a forever while loop, displaying the JOptionPanel for the user inputs.




When I read back the options.getValue() at line 51, I would have expected the value returned would be (zero) as per line 11 of the actionPerformed(). It is not always the case. It is either zero or uninitializedValue.

first time on selecting connect button
ConfigurationDialog PRE DISPLAY value is 2
ConfigurationDialog POST DISPLAY value is uninitializedValue
actionPerformed PRE option value is uninitializedValue ActionCommand is Connect
actionPerformed POST option value is 0 ActionCommand is Connect

second time on selecting connect button
ConfigurationDialog POST DISPLAY value is uninitializedValue
actionPerformed PRE option value is uninitializedValue ActionCommand is Connect
actionPerformed POST option value is 0 ActionCommand is Connect
actionPerformed PRE option value is 0 ActionCommand is Connect
actionPerformed POST option value is 0 ActionCommand is Connect

third time on selecting connect button
actionPerformed PRE option value is uninitializedValue ActionCommand is Connect
actionPerformed POST option value is 0 ActionCommand is Connect
actionPerformed PRE option value is 0 ActionCommand is Connect
actionPerformed POST option value is 0 ActionCommand is Connect
actionPerformed PRE option value is 0 ActionCommand is Connect
actionPerformed POST option value is 0 ActionCommand is Connect
ConfigurationDialog POST DISPLAY value is 0

Two things of interest here.
1) The value read at line 51 is not always as expected. The output seems to suggest that line 51 is executed actionPerformed () has finished. Can this happen ?

2) For every iteration of the loop, it seems that the previous user connect button selection are re applied, because, of the extra "actionPerformed PRE & POST .." outputs. Why would this happen ?

Any explanation/workaround to above be appreciated please.

Thank you

Pete

 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Palmer wrote:
When I read back the options.getValue() at line 51, I would have expected the value returned would be (zero) as per line 11 of the actionPerformed(). It is not always the case. It is either zero or uninitializedValue.

Yes this is correct because your options variable is local variable!! So whatever it does in the actionPerformed() is independent from the other method.

Given this I think you can figure out why your 2 points of interests occur.

Oh just a thought you may want to use switch case statements instead of if-else statements since you are checking against some constants. Also I highly recommend you use the Properties class. The ConfigOptions class you have basic can be used as setting up the screen/window but of course you do need to get the value in order to proceed.
 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because your options variable is local variable


The options variable is declared as a class level variable :-


So when I perform operations of

I presumed this would be on the class variable.
Therefore, when I do
in the actionPerformed(),
I should get that the changed value in
of the ConfigurationDialog().

Or have I missed your point.

Also, my understanding is that the actionPerformed() should process to completion before, line 51 is hit. Is that correct ?

Thank you.

Pete
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to bang your head like that. The problem you are getting may be the fact that you want to layout all components dynamically in one JPanel. Why not break it down a bit. Work with say local client config first. Since you are using JOptionPane, this approach is different from mine (using JFame and JTextFields).

Once you get the local working (prompt -> error -> save) then move on.
 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You don't need to bang your head like that

Just a little frustrated. I had planned to do alot over the easter break and this issue had taken up all my time and as a result progress was minimal.

I will take up your suggestions.


Could you confirm/correct me please.

Thank you

Pete
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pete, have you done the Data class and server side (socket/RMI)? Because if these aren't done "properly" like locking/searching then it doesn't matter great your GUI is you can't pass. Cos GUI is only 40 points where locking/Data class/server is combined like 160 points.

I have done some testing with JOptionPane and I didn't use actionPerformed().



Now I use Properties.store to save the config and the isValid() method is where you check your parms.
 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi K,

have you done the Data class and server side (socket/RMI)

I have implemented the Data class and used RMI. I need to javadoc the RMI. Will I get 160 points for this. I wish. Considering I am learning as I am going along, I need every point.

I like your example using JOPtionPane. However, if I was to use this, it would mean that the user will provided with one dialog for every parameter required, one after another. OK I appreciate there will be a max of 2. However, I feel the user, and the examiner, would much prefer to enter the information all in one window. If you know, how your example could be extened to allow the user to input more than one parameter in one window, I would like to know and would very much appreciate it.

Otherwise, it would be back to trying to get my original approach to work or try using JFame and JTextFields. As the holiday is over, it is a case of working on this on late nights or weekends.


Thank you.

Pete
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK then use the dialog/frame with text fields approach. In my approach, I have 2 frames - one for local setup and the other for remote setup. Each has its own save action.

However in your current approach, it looks to me that you are trying to put both local and remote setup items into one single method dynamically laying out the stuff. This idea may be good but honestly keep it simple. Remember one aspect of SCJD is to allow junior programmers to understand. This would make evaluators easier task of grading.

This is my code for setting up which setup window load.
 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi K,

it looks to me that you are trying to put both local and remote setup items into one single method dynamically laying out the stuff

Absolutely correct.

Will review & re visit this area of my design again taking on board your advice & examples and see if I can crack this problem !

Many thanks.

Pete
 
Sebastien Chenevotot
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I know this topic is quite old, but I encountered the exact same issue and I finally found a solution after banging my head against my keyboard for a long time. I want to publish it there as I did not find the solution on the web.
The issue is due to the setVisible(false) method which is misplaced. As soon as it is executed, the main thread wakes up. As a consequence, the value is not set yet, and you get the famous uninitializedValue.
So the solution is simply to put the setVisible(false) after the if/else statement:


I hope it may avoid headaches to others...

Cheers,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic