aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem refreshing a panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem refreshing a panel" Watch "Problem refreshing a panel" New topic
Author

Problem refreshing a panel

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Ok, I have a problem with classes working together. Here's how they are to work:

The main application (AutoHistory.java) builds a JPanel that includes a combobox which is built from the contents of a file. One button on the panel (Add Vehicle) is to add a new entry to the combobox, when this button is clicked it calls a second class (AddVehicle.java).

The second class builds a JPanel that includes several fields needed to create a new entry in the file. After adding the new entries a button is clicked (Cancel) to return to the main application.

Now, here is where I am experiencing the problem, how do I redraw the first JPanel so that the file is re-read and the combobox is updated? The main application is rather large, so I am going to try and provide a URL to a temporary file on my server, here's hoping it works.

(Please do not comment on the sloppyness of the code, I am just learning and this is still a work in process. Once it is working I intend to go back and clean things up.)

main application

second class
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> how do I redraw the first JPanel so that the file is re-read and the combobox is updated?

you don't need to redraw the first panel.

create a method to read the file into a DefaultComboBoxModel (dcbm), and after file is closed, set the comboBox's model to that dcbm.
you call this method when the first panel is created/shown.
you can call this method again after the file changes (add/modify/delete)
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Suggestions:

1) I would not have the add vehicle window interact with your database. Instead have the main class deal with it and just have the add vehicle class create a vehicle object and deliver it to the main class. The database code also should be kept distinct from the GUI code if at all possible. I am no database expert, so take my recommendations here with a bit of salt.
2) Don't make the add vehicle window another JFrame, but instead make it a modal JDialog. This way your main class that calls it will know precisely when the user closes the window and will thus know when it can extract the vehicle information from it. Or you could simply place a JPanel into a JOptionPane and show it as JOptionPanes will give return constants corresponding to which button was pressed and are pretty easy to use.
3) Give the add vehicle class public getter methods so that the main class can extract information from it if the user presses the save button. You can also give it a save boolean variable so that you'll know whether or not the save button was pressed.
4) Get rid of all your static modifiers, except for the main method that is.
5) Go through the Swing layout manager tutorial so you can move away from using absolute layouts.
6) Use a DefaultComboBoxModel as your combo box's model. Then when you want to update the combobox, simply add or remove from the model, and the combobox will show these changes.

Here's a simple example of use of a JDialog to get information from the user, and then places it into a combobox. The dialog has only one JTextField for simplicity here:

AutoHistory2.java


AddVehicle2.java


Best of luck, and hope this helps

edit: comments added to first class
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Pete, thanks for all the work. It's taking me a while to go through and try to understand what all of it does. So much that is not covered in basic Java courses. Can you explain to me what this section is doing:



I read the AbstractAction on the Sun site, but the super("Save") just isn't making sense.

I can't find anything that explains the "@" statement, I know I use it in my existing code, but to be frank I lifted that from someone else's code on JavaRanch, with no understanding as to what "@" does. At first I thought is was a comment, but that doesn't seem to be the case.

Also, I understand your comments on using a layout manager, but I prefer to place the objects myself so that they blend in with the background images. Also, there is additional design (fields, etc.) that will be added, and the existing flow managers just aren't flexible enough to handle what I want to do.

This is a learning application, as well as my first attempt to create anything in Java. It was sparked by my current version of FileMaker not compatible with Apples next OS, and my lack of desire to spend a couple hundred to upgrade to the next version for three databases that I use locally. When I first purchased FM my needs were far more complex than they are now.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mike Lipay wrote:I read the AbstractAction on the Sun site, but the super("Save") just isn't making sense.

Have a look here: Swing Components Supporting Action. The super(String) calls the AbstractAction's constructor that uses a String parameter. It changes the text property of the button, setting the button's displayed text.

I can't find anything that explains the "@" statement, I know I use it in my existing code, but to be frank I lifted that from someone else's code on JavaRanch, with no understanding as to what "@" does. At first I thought is was a comment, but that doesn't seem to be the case.

@Xxxx declares an annotation. This one tells the compiler not to show a warning even though I did not give my AbstractAction a static long serialVersionUID field.

Also, I understand your comments on using a layout manager, but I prefer to place the objects myself so that they blend in with the background images. Also, there is additional design (fields, etc.) that will be added, and the existing flow managers just aren't flexible enough to handle what I want to do.

Check out all the layout managers and learn to use them nested and in concert with each other, because as your app gets more complex, trust me, your absolute positioning will become more and more of a chore and will hinder you.

This is a learning application, as well as my first attempt to create anything in Java. It was sparked by my current version of FileMaker not compatible with Apples next OS, and my lack of desire to spend a couple hundred to upgrade to the next version for three databases that I use locally. When I first purchased FM my needs were far more complex than they are now.

Best of luck with this!
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Thanks for the descriptions, some are making sense. It's taking a while trying to figure out what in your version I need, versus what is just there because you didn't care for my coding.

I wish there was a "beginning" area for things like SWING. The beginning forum tells me to come here, here everyone assumes I am more advanced than I am. I find Sun's API a bit difficult to read, like diving into a chemical properties book before you're taken Chemistry 201.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mike Lipay wrote:I wish there was a "beginning" area for things like SWING. The beginning forum tells me to come here, here everyone assumes I am more advanced than I am. I find Sun's API a bit difficult to read, like diving into a chemical properties book before you're taken Chemistry 201.

But the more you "dive" the more familiar and comfortable you become with it. My suggestion: keep diving as much as you can.
 
 
subject: Problem refreshing a panel