Michael Salvini

Greenhorn
+ Follow
since May 25, 2010
Merit badge: grant badges
For More
Fair Lawn, NJ
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Michael Salvini

Good Morning All,

I have made it through several Java books and I am reading another one, and started to wonder about something. Some books illustrate the use of an anonymous inner class to do something (in most cases it's an action listener) and some use known inner classes. Two questions come to mind:

1) Which is the better choice and why?

2) Or do they both have uses where one is preferred over the other?

Thanks,
Mike
13 years ago
Hello All,

I am fairly new to Java and have been perusing the boards here picking up little things here and there and I think I have really been learning a lot. Anyway, I have been following this thread https://coderanch.com/t/519939/GUI/java/do-you-switch-JPanels-shown and as a result, I learned about the CardLayout Manager and did some experimenting and playing with it which made me wonder about a couple of things.

If you had some menu, maybe JMenuBar or JToolBar, in a JFrame and some of the menu items' actions were to change the content of the JFrame, maybe from one form to another or one picture to another, something like that, Would CardLayout Manager be something appropriate to use for this or would you replace the contents of the JFrame with a new JPanel each time the individual actions are called? From reading the API for CardLayout, it would be best to use it with mulitple Panels, all uniquely named and then call a change to that name with the individual actions of the JMenuItems.

Now, that said brings me to my next curiousity. I recently read up on Threads and Concurrency in Swing and the use of the invokeLater method to send everything to the event dispatching thread. Should calls to change the card be done in a similar way? I have found most of the Swing components say Thread safe or not, but CardLayout does not, although it says it is Serializable which would indicate to me (not sure about this one) that the cahnges should be called through invokeLater.

Sorry about all the questions, just trying to straighten out in my head what I learned and read over the last couple of days.

Thanks for your time,
Mike
13 years ago
Sorry Maneesh. I had only posted here because the question was directly related to what was being discussed. Sorry about that. I will re-post my quesiton.

Thanks,
Mike
13 years ago
Thank you Tom and Paul for your advice and input. Right now I am just building things for the sake of practice, so I will stick with opening and closing a connection everytime I need it, but I will definitely keep the connection pool idea bouncing around my head. I started to google that a little bit and I think the use of that is a little out of my league right now, I am really just trying to get the basics of the language down and then I will try and move onto some of these seemingly more advanced topics.

My eyes have been opened to a lot as a result of this question and that is what I am looking for.

I really appreciate you guys taking the time to help, thanks!

Mike
Thank you so much. You know, I kept going back over the code multiple times trying to figure out if I had the TreeMap initialized somewhere else and never found it, but I never thought about the method declaration.

Thanks for clearing that up for me.

Thanks,
Mike
13 years ago
Good Evening All,

I wasn't sure if I should post this in Java in General or Beginning Java. I went with Beginning Java because I am just beginning, but I do apologize if this should be in another forum.

I have a class (LoginFrame) that calls a static method from another class (Users). The static method in class Users is here:



As you can see I am returning the TreeMap users which I have initialized as TreeMap<String,String>.

In my LoginFrame class, I store the results of the above method. The line that declares this and calls the method is here:



When I compiled it, I got the following notes.

Note: LoginFrame.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: LoginFrame.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.


I then recompiled with "-Xlint:unchecked" specified at the command line and got the following warnings:

LoginFrame.java:146: warning: [unchecked] unchecked conversion
found : java.util.TreeMap
required: java.util.TreeMap<java.lang.String,java.lang.String>
TreeMap<String,String> users = Users.getUsernamesAndPasswords();
^
1 warning
LoginFrame.java:146: warning: [unchecked] unchecked conversion
found : java.util.TreeMap
required: java.util.TreeMap<java.lang.String,java.lang.String>
TreeMap<String,String> users = Users.getUsernamesAndPasswords();
^
1 warning


While I realize these are not show stoppers, and everything works as expected, I am trying to understand why this is happening. I realize that in the Users class, the TreeMap is declared with a type of <String,String>, so I thought I did the right thing by declaring if the same way in my LoginFrame class. Is this not the proper way? Any ideas as to why this is happening?

Thanks,
Mike

13 years ago
Makes sense, thanks for the reply. What would typically dictate the need for such a change, strictly performance or something else?

Thanks,
Mike
Good Evening All,

I am fairly new with Java and just playing around creating various little tools and applications while studying for my SCJA. I have been reading through the Oracle tutorials regarding the use of JDBC and I am curious about some things.

1) Is it better practice to make a DB Utility class to make a connection to the database each time a query needs to be executed on the dbms or is it better practice to create a db class that allows the connection to stay open and then I can just pull a reference to the connection each time.

2) I have also seen the connection established two ways, the first:



And the Second:



I am curious if the first call to load the driver is a necessary check. I know my error handling needs to be better.


Just curious.

Thanks,
Mike
Sounds good to me, and again I appreciate the time and help.

Mike
13 years ago
Makes sense, thanks for the help. I will leave it how I started to set it up.

I don't know what I would do if I didn't have a place like this to ask these questions.

Thanks again.

Mike
13 years ago
Good Morning All,

I am pretty new to Java and I have been spending lots of time creating small applications for practice and on one of them, I wanted to have the version appear in many different parts of the gui (login window, title bar, help window, etc). I create a class called StandardText with some static variables marked private. I have pasted some sample code below. I then created some getter methods to retrieve the static variables and then I was sitting here thinking that why do I need the getter methods, the variables are already static, if I mark them public, I will be able to access them direct. My question is two parts:

1) Which way is the better method of practice, private static variables with getter methods that are also static or public static variables with no getter method?

2) Is there are better practice of doing what I am doing?



Thanks,
Mike
13 years ago
Maneesh,

I have been reading through the links you sent me and I have a couple of questions. Below is the code I have created for reference.

1) Should all calls to gui/swing functions use the SwingUtilities.invokeLater() method? Things like for instance my status bar update method? (setStatusBarMessage)

2) In regards to the SwingWorker class, is this something you would use to when creating a new JPanel or form so you don't call set Methods on that form until it is complete, or am I missing the point with this?

I appreciate you time and help.

Thanks,
Mike

13 years ago
Paul,

I appreciate the help and pointers. I have rewritten the main class as you suggested. I guess now I am wondering, when do I break up this class? Let's say I start adding many functions to the menu bar and each one displays a different panel, maybe a form, something like that. Would I keep writing all of these helper methods in one class?



Maneesh,

Thanks for the tips and the reading. I am reading through those articles now. Sorry about posting in the wrong forum, I wasn't sure which one to post in. I ultimately decided the Beginner one because I was more focused on understanding OO than the swing piece and now I think I am getting a better understanding of both.

Thanks,
Mike
13 years ago