Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Jay Brass

Ranch Hand
+ Follow
since Oct 24, 2000
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jay Brass

I realized that my rowListener and my tableHeaderRender were in both trying to update the selected row. I created a RowClickedListener that extended MouseAdapter to check for when a user clicked on a row instead of when they clicked on a header. Long story short, this allowed me to set a boolean value and only change (or reset) the selected row to what the user clicked on and not what the header sort had changed it to. Seems to work pretty well.
8 years ago
Hey There,

I have a JTable in a JScrollPane. The table model is created from a resultset from a database query. I've set tempTable.setRowSorter(sorter) where sorter is a new TableRowSorter<TableModel>(model); I've also set sorter.setMaxSortKeys(4); because I only want to sort on 4 specific columns. Currently I call a header render to sort the columns but after it sorts them the selected row is incorrect. I had to add the columns to the SortKeys and adjust for the column selected to sort on so that it would be similar to an OrderBy clause in a database query. Does anyone have any idea how to keep the selected column after the sort? Converting to model or view don't seem to help.
From the class where the table is:

the table header render class
8 years ago
First thanks to you all for your replies. It made me think a little more about what I was trying to do. The solution I have come up with is this:
This works:
calls the class to get the resultset from the database

in the database class:
public DefaultComboBoxModel getSource() {
I still have to do the trick to get the number of rows returned with the cursor
in order to create my arrays

builder is an object of the container class that will hold the array of the numeric ranking, in this case an array of results from the database. That's all I need because the other array is used ot create the DefaultComboBoxModel which is the return type of this method.
The Builder class uses the builder design pattern to hold dynamic data throughout the application. I get an instance to the builder class and in my class that sets the model to the JComboBox:

It does what I need it to do and I don't need to.
8 years ago
I'm probably over thinking the problem. I figured creating a whole class to hold these was overkill. That's why I thought HashMap until I realized I needed them in a certain order. From there I moved to multi dimensional arrays or as you put it an array of arrays. I think I could still use a TreeMap for this though instead of a new class.
8 years ago
I need to add data from a resultSet to a JComboBox. It always has to be in the order that the database returns it to me. A minus for HashMap and a Plus for Array. I can get the number of columns from the ResultSetMetaData and I can get the row count by setting the resultset to rs.last rs.getRow() and resetting it back to rs.beforeFirst to iterate over it. This gives me the 2 values for my 2 dimensional array.
new Object[rowcount][columnCount] So if the database people add more items I don't have to change my code. I know an ArrayList is variable length but it's slow.

An array will help me create the model for the JComboBox. In the database the items are stored with a column that specifies the numeric ranking of the item and a column for the text description to be displayed. The numeric ranking starts with -1 and a JComboBox index starts at 0. The problem currently is when a user chooses an item and I get the selectedIndex of the JComboBox, I'm off by 1 when I do the look up in the database. I could just subtract one from the selectedIndex when I get it but I was hoping for a better and more dynamic solution in case the database people add a -2 item. I don't think there will ever be null values but I know better than to assume this.

As I understand them:
Arrays - First class object. An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed

HashMap - This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

My first thought was to use a HashMap since it would be easier to get the key based on the value selected (JComboBox.getSelectedItem) and pass the key to the database which would always give me the correct results. Should I sort the Hashmap first, before creating the JComboBox model? Or should I iterate over the 2D array and create a new array from the descriptions that it holds?
On the Object oriented front you're going to tell me to create a class to hold my values returned from the database where one field of that class is the numeric ranking and the other is the description. I think.
8 years ago
You guys are awesome. Thank you Bear Bibeault and Paul and everyone. Timestamp (not TimeStamp) is what I will use.
8 years ago

as opposed to
which would show 2013-02-07 15:30:25.123
I think I like using the string when I have formatted it with SimpleDateFormat. That being said, my question isn't how to get it to the database, it's about whether I should pass around a String or Date object. Passing a Long has some good points, but I think using the original object is the best idea. I don't know if I will need to use it in any other situation in the future and if I use a String or Long, I am limiting myself. I didn't realize this until I saw all of your postings. That got me thinking that a String puts me in a corner where I'm limited to what I can do with my Date time object.
This also sparked a new question, if I were to use a TimeStamp as has been suggested, and against the DBA's wishes, can SQLServer implicitly convert it to a DateTime type? There are hundreds of tables that have DateTime fields. I'd have to make someone go back and change them all to TimeStamps.
8 years ago
Yeah, I thought about TimeStamp, but the DBA warned me that he had a bad experience using TimeStamp in the past and got burned really bad. A Long would be OK but it doesn't make testing very easy. I can log or system.out Strings and they are readily readable. Just a little more to do if I put Longs out there. The other advantage to Strings is that SQLServer converted it for me.
8 years ago
I've been working in Java for awhile but never thought about this. I have a class that captures the current time. This has to be sent to a database. There seems to be a problem going from Java.util.Date to java.sql.Date. All of the hours, minutes and seconds get set to zero. The stored procedure doesn't return the right data since the data is time (and Date) sensitive. My question is, is there any taboo in creating a string from the Date object and passing it around? I will never change this since it represents a moment in time that I never have to add or delete from. It makes it a lot easier since SQLServer does the conversion from a string to DateTime for me. I also don't have to monkey around with Calendar or GregorianCalendar classes or deprecated date methods. I'm using Java 1.6
8 years ago
Looking at some of the other topics in the forum, it looks like one way to read all the images is to create a Table of Contents (TOC) list. It won't be hard to implement but is an extra maintenance step I had hoped to avoid. If I write the code correctly, checking for null, I won't have to worry about the directory of images and the TOC being in sync, as long as the images I want to show are correctly referenced in the TOC.
9 years ago
I know this topic is a little old but I have a similar problem. I can access an image from the executable jar while the application is running using this:

That works fine for 1 image. What I would like to do is read the entire directory of images and load them without having to hardcode the name of each and every image.

The below code works fine from eclipse but when I make an executable jar file, it doesn't work anymore.

In the above code f is a JFrame I created earlier in the code.
I have a piece of code that tries to get the resources

but that points to the workspace where the project is: ......./workspace/Images/bin and that won't work from the runnable jar when I execute the jar from my desktop or from another machine.
how can I access a list of all the .jpg files in my runnable jar without hard coding the names of each of them?
9 years ago
Thank you I'll look into ORMs. The current architecture uses all stored procs and I built the front end to retrieve the meta data to get the number of columns in the result. I build the model for the JTable using this information by putting the cloumn names in one container and the data in another. This way my JTable stays dynamic based on the results returned. Any table joins or view loo kups are done in the stored proc. Much of this code is inherited and there is already a class for reading XML so I just reused it as well as editing the existing XML file that had it's own xsd schema file. A lot of this code was written in 2003 so I'm trying to bring it up to date. (it used flat files) A lot has changed since then. There aren't any frameworks used either and I'm not sure I want to retrofit something like Spring into this. Originally I didn't think there would be any problem holding the initial database IP with things like title or backgroundImage. They aren't supposed to change over the life of the app. But I think separating them does make sense.
Thanks for your advice.
Hi all,

I'm working with a swing application that does not connect to the internet. It does connect to a database. Before it can do that, it needs to read the IP address of the database from an XML config file that is resident on the local machine. If the app is ported to another computer, the IP could change which is why it's in the config. (that's out of my control). I need a class to hold the IP address so the app can connect to the database. So when the app starts, it reads the config.xml, connects to the database and performs queries. I'd also like to add some more values to the properties of this configuration class after some more queries have been run and results returned. For instance, if I were building a volkswagon, the number of seats, and engine size would be different than if I were building a Ferrari. These results would be returned from the queries. If I create a static instance variable and get an instance of the class, all the information I need would be there or I could add new information as more queries and results are returned. I'd like to avoid using setters but since the class holds a lot of information, the constructor would be ugly with all of the parameters necessary. Does this sound like a candidate for a builder pattern? My understanding is that a factory pattern would create the same type of object every time and I might not need all of the properties. I need the properties available though in case I want to build a Ferrari instead of a Volkswagon. As the user makes choices, I want to add to this class and use its properties to drive other area's of the app without having to return to the database all the time.
You're awesome. I don't know why I couldn't think of it.

Thank you.
10 years ago
Thanks Paul,
I had kinda gotten you hint in your first post, I'm not sure how to implement it though. Creating a list isn't the problem. What I'm having trouble with is how to get to the lower lever buttons from the top list. I have to name the buttons based on a value in the database, to keep things generic. If the top list has a bunch of names in it, how do I store or retrieve the list of buttons for the lower level?
I had thought of creating a class like a bean and storing some attributes in it.(buttonName, buttonText, buttonColor, etc) Then I could add that object to the list. One of the attributes in the class could be a list so it would have the lower level buttons available by iterating over the list contained in the class.
I'm still not sure this is the best solution though. I will be trying it, and will let you know how I make out.


10 years ago