aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes HashMap problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "HashMap problem" Watch "HashMap problem" New topic
Author

HashMap problem

claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
The problem is that I am trying to put some new messages on top in a hashmap using an adding window where a person can write a new message(i have a textField for title and a textArea for content).

The data from my hashmap is listed in a JList. The JList has implemented an ListSelectionListener in order to display the corect content associated with the title of the message in a textarea.



I've managed to put them at the end of the map...but when i add a new element to the map and implicitly the JList it appears in the JList when I open the ViewMessages window but if I try to add a second item, it is displayed in the jlist only when I click on other items who were previously added directly in the list through code (like this )


This is the code I ve used for adding the messages:



How can I add new messages on top of the list and also after I close the ViewMessages window and open the AddMessages window a second time to be able to add more messages and not get any displaying errors (ex: the messages appear in the jlist just after I've selected a preexisting item or I can't select the messages who were newly added to see the content in the textarea) errors in the ViewMessages window ?
Thank you.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

There are several issues with your post:

1) First, your subject line is not good - UseAMeaningfulSubjectLine

2) in one place, you create an instance of MessageList. In another place, you access the list directly like MessageList.list - is list a static member? if so, why are you creating an instance?

3) You are talking about HashMap and the latest message appearing at the top. The HashMap implementation does not guarantee how it will be ordered. So, you should not use HashMap. Instead you should consider TreeMap - which does natural ordering - say, in your case ascending order of the keys. If you want the reverse, you should supply your own Comparator when creating the TreeMap.

4) You have used DefaultListModel. First of all, in Swing components, model holds the data. The DefaultListModel implementation holds a Vector to hold this data. You are using your own list to hold the data. I don't understand how these 2 are related/synchronized with each other.

What you should do:
Write your own ListModel by extending the AbstractListModel. Within that class have a TreeMap variable to hold the data. Within the add or put method, make a call to super.fireContentsChanged() or fireIntervalAdded() to instruct the JList to adjust its display based on the new additions/deletions.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
Thank you for your answer. I'm trying to use a MVC interface. I was wondering how can I use list as nonstatic and instantiate the MessagesList class in the main frame class (as it was instantiated earlier too, as you've noticed), create a method in Controller which will enter the data in JList from the Client view class and then call the method in the main class after creating a MessagesList object. I've tried doing this but i keep getting null pointer reference.


In the main class I would write this:


Also cand you give me an example of how to use fireIntervalAdded()?
Thank you, I hope your answer will help me understand better how this works, I've tried so far to get it but I think I need a concrete example.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

I get the impression that you have not read my post fully. Let me repeat my point here:

Ranganathan Kaliyur Mannar wrote:What you should do:
Write your own ListModel by extending the AbstractListModel. Within that class have a TreeMap variable to hold the data. Within the add or put method, make a call to super.fireContentsChanged() or fireIntervalAdded() to instruct the JList to adjust its display based on the new additions/deletions.


So, did you try to write your own ListModel?
I suggest you read the tutorial first.
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
Yes, you're right...it does work with a listmodel, thank you very much.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

You're welcome.
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
Now I'm trying to put the data from a treemap in a jTable but I'm having some troubles because it keeps returning just the last product added in the treemap.
I'm having a Product class, a ProductList class that holds the data and I'm trying to add all the data in the JTable.

This is the model for the JTable



The function called elementAt is defined in the Products class:





I get a 2 rows table with "Tea", "sdff", 1,true in both rows.

This is the class that holds the data:


Can you tell me what's wrong? Thank you!
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

Your getValueAt implementation is wrong. Why are you using a for loop inside this? And, you return from within the loop. So, everytime, only the first row will be returned.
You already have the row number in the 'row' parameter - so, use it directly. Same way, you already have the column value in the 'col' parameter. So, I don't see the need to use switch at all. You could just call this directly:

return ctl.CgetProductbyId(row).elementAt(col);
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
Yes, I know now that loop didn 't have any point...i've repaired it, thanks.
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
I have another question, if you don t mind. I ve created an interface where i'm selecting some products from an window with a list of products (every product from the jtable has a checkbox cell) and the suite of selected products ( representing an order)are added to another jtable in another window, the orderlist window. Each order has a button cell that validates the order (that means the order is ready). What I want to do is to fire a message to the first window when I click that validate button to let it know that the order is ready. I was thinking to put an listener to the button that will trigger the appearance of a dialogbox window in the first window saying "order with number... is ready". I was thinking of putting this mesages in some kind of toolbar or menu bar just like a tab so they won't cover the whole window if they are too many (and to open when thay are clicked in the tab).
Do you think this is the best solution, is it implementable (also considering the fact that I'm using a mvc interface)? Or should I use something else?
Is it a more professional or adequate way of doing the triggering alert thing? Thank you.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

A dialog is a dialog whether it is shown from the first window or the second window. I think you would like to bring the first window to focus when an order is validated - this leads me to a question - by window, do you mean 2 different JFrame or just another tab?
in any case, if you bring the first window to focus everytime the user clicks a button to validate - I think that would turn annoying soon - for example, lets say I have 5 orders to be validated. I click on the first one, the first window is brought to focus and a message appears. Now, I have to click on the dialog box ok, then click again to re-activate the second window and then click on the button for the second order and so on - user has to click 3 times now to validate the second order and so on...

Since you already have a JTable in the first window, I would add another column named 'Status'. When the order is submitted, I would show the status as 'Submitted' or 'Processing' and when the validation is done, I would change the status to 'Validated' or 'Ordered'. I would also look to use a Renderer to show the status text in different colors. Say green color after the order is successfully validated and red if there is an error.

This way, whenever the user comes back to the first window, (s)he would see the status of all orders - without being intrusive. You can also thinking of having a status column in the second window too (and re-use the Renderer class).
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
Hi, thank you for your answer.
I have 3 different JFrames (together with the main frame from where you can acces either one of the other 2 frames). The first Jframe is a tabbedpane containig a tab with the order menu list and the other tab I was thinking of using it to put ready orders in there (in a jtable eventually).

The other JFrame contains the JTable with all the orders that were given from the first JFrame and should be accesed by another person by clicking another button from the main jframe.
Indeed, I was thinking of focusing the tab containing the jtable of ready orders.

I have two options I guess: either to reuse the jtable with all the orders by adding them simoultaneously in the second tab from the first JFrame and in the second JFrame and put a state (Processing) on both jtables from both jFrames.
Both users need to validate an order. The first to validate is the person who directly acceses the order jtable. When he validates the order from the second JFrame that should cause a change of state in the both tables. And then the second user can validate too the orders validated by the first user, his validation conducting to their removal from the jtables.

Or I could add only the orders that have been validated from the second JFrame into the second tab from the first JFrame where the second user to validate can view only the ready ones and validate them, at his turn. Because the menu is on another tab I would like to focus the tab so that the person using the first JFrame would know that a order is ready.

Though I don't like the idea of moving orders from one table to another. I'm looking for the most efficient and practical method, considering the fact that there are 2 users accesing the application (and have to validate orders from the jtable with orders succesively).
Should I use multithreading so both users could acces the same window but have different views that will allow them to perform validations at different turns? ( to be allowed only to performe some actions or click some buttons, after the other user clicked another button, have different views of the same window? the multithreading thing is pretty new for me but is it best applicable here than the other methods?)

The other problem would be that I have to save the whole application periodically (serialization to xml or smthg else). I practically save what? The 2 treemaps I used for holding data(one for products and one for orders)?

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

You requierment is confusing to me. How would 2 users take turns in the same desktop to validate orders? If they are working from 2 different machines, then you should use sockets or RMI for the 2 instances to talk to each other.
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
Ok, so I've made a selection of all the ready orders from the first jtable (that displays all the data, including the orders that are not ready). I displayed the selection in another Jtable, using the same table model, but by applying a filter on it. Now I want to delete a row from the table model by clicking on a button in the jframe containing the jtable with the filter.
This is the code I've used for deleting:



This is the corresponding code from the controller:


For the fireTableRowsDeleted() method I've used i have this code


And this is the filter I used:


this is the error I get


I would be very grateful if you could tell me what I did wrong.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

claire perp wrote:


This code is completely wrong. You first do a removeRow in which you call fireTableDataUpdated. Then, in the else part, again, you make a call to fireTableRowDeleted (that too with wrong row numbers). why do you need this call?

fireTableDataChanged is used to tell that every cell of the table has changed - which is not the case here. So, you could just use fireTableRowsDeleted once.
And in the fireTableRowsDeleted, you pass the row numbers - for example, 'I have removed row 2 to row 9, so, please notify everyone'. The row numbers in the table begin with zero. So, if you have 10 rows, your getRowCount() is 10, but you have rows 0 to 9. In your code, what you are essentially telling is to delete row 10 to row 10 - which is non-existent. You should be getting this error when the else part fires...
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
Yes, you're right, thank you very much. I've modified the code.




This code works fine if I click on a button to delete the selected row but it doesn t work at all if I use it in the renderer for the button type cell.



It gives me this error



How do I make it detele rows on the button cell click?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

claire perp wrote:



Again wrong. You are actually deleting only one row - but you are calling fireTableRowsDeleted(0, row) - this tells the view that you have actually deleted rows 0 till 'row'.
You should actually be calling: fireTableRowsDeleted(row, row);
Also, the call to fireTableRowsDeleted can be done within the RemoveRow method. There isn't a need to have a separate method and add another line call for that.
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
Thank you for the correction but it's still a problem with the button cell renderer. I don't know how to make the code work for the jbutton cell, it just gives me the error.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

2 things:

1) In your default cell editor, have you overridden getTableCellEditorComponent? You should do it and return the button instance for the button to become the editor.

2) In the actionPerformed method, the first call should be 'ButtonEditor.this.stopCellEditing()' - then you can continue with the deletion of the row.
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
Yes, I ve tried, this is my code.



And now it gives me this error (if i try to delete the fifth row of the table, for example):


It displays an error with this line: ordersready.remove(row);
I can t understand what the problem is. Again, the code works fine with an outside button.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

ordersReady is your JTable, not a table model. You can't remove components from a JTable. You must instead use the table model's methods to remove rows.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
I've tried using this method instead of the orderlist.remove(row)


but now instead I get nullpointerexception.
I just can t seem to sort this out, clearly I'm doing something wrong or missing something here.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1535
claire perp wrote:
...but now instead I get nullpointerexception.


You get a null pointer exception when you invoke, null.someMethod().

For example,


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
All that the code uses are these:

In the controller class I acces the method from the class with the treemap:


And then in the buttoncelleditor I have this:

And that should work, right?
If I use this code on a random outside button it works without any problems:


Where is the problem?
The error is on this line c.RemoveRow(i);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Either ordersready.getValueAt(row, 0) returns null and the auto unboxing causes the exception, or c is null, or sorter is null.
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
If I keep reading the error log it says this:



Should I interpret it how?
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1535
claire perp wrote:If I keep reading the error log it says this:



Should I interpret it how?


Read the first line in the log, and also the one concerning to your (written by you) java file.
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
Well this is it:


And this is the whole program concerning the JButton:




Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

claire perp wrote:Well this is it:

That's where you can stop. Check line 99 of ReadyOrders.java.
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
Yes, it's on the c.RemoveRow(i); line but I don;t understand why.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Where did you initialize Controller c?
claire perp
Greenhorn

Joined: Nov 03, 2011
Posts: 19
It's in the code from the previous post at the beginning:

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

He is asking about 'initializing' and not declaring. and are you setting the c back to null elsewhere - before the code in editor is called? note that, you are passing an object to the editor - it is just a reference - so, if the parent is set to null so will the reference within the editor too...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap problem