4

Basically it's a task to manage properties, and I am doing a much more complex solution than it is required. It is just the last bit where I struggle.

To give you an idea of what we have:

  • Properties (superclass)
  • PropertiesToLet(extends Properties)
  • PropertiesToSell(extends Properties)
  • EstateAgent (GUI to manage the properties)

So the part I am struggling with is basically the EstateAgent class.

What I wrote so far: http://pastebin.com/0qieM67j

That a about 500 lines - But I need help with the theoretical part not the programming part - because I dont want you to do my coursework - I just need the solution how to approach that.

The lines I am struggling with are from: 55 to 113

Its about a table that I create and insert rows into it. Each row represents a property. It could be a propertyToLet or a propertyToSell object. Those properties come from my ArrayList<Property> properties.

The inserting row and showing the table is fine and it is working the way it should. So there is problem with the code. I apologize for the code structure - but we are limited in the submission - so we cannot submit more than 4 files those file are obviously the named classes - so I cannot extend any more classes or files to the project.

So what I want to do now is: Editing a property.

I have the row that represents a property. It is showing me the position in the arrayList and all the values that I can get.

So now there are a few more possibilities.:

  • Add and Remove a tenant from a PropertyToLet
  • base on a tenant you can collect the rent and pay the rent and see how much rent is left to pay.
  • Add and Remove a purchaser from a PropertyToSell (the sold will then change according if there is a purchaser or not)

So basically there are several ways how to continue from here. For instance it could be a behaviour like this:

  • RightClick on the property opens a context menu where the mouse is and I can choose other options such as: Remove Tenant, Add Tenant, Collect Rent, Show Rent Owed, Add a purchaser, Remove a purchaser ---- ofcourse depending on what kind of property it is.

  • Double click on a row => edit the property ( have a look at this screenshot)

enter image description here

  • it shows a window that I use to add a property - I could add all the values in the field and that button changes to "Update Property"

That would be one solution another one would be: - Edit the cell of the row => changes the value of the property (they will inspect the object and see if that really changed and not just the row value)

That are my ideas of how to inject the last steps of bringing the functionality into the application.

So here are my questions:

And than I need an outside opinion what is easier and faster to implement in this very limited task. The way of opening the existing add window and change it to an edit window - I don't want to have redundant code! Or changing the value by editing the cell so that the values in the arraylist change.

I need some help with the approach what is easier and what is better.

I really appreciate any help here!

I am looking forward to see some answers.

EDIT I finished the popup Menu thank you for your help. I edited the question as wel.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Richard
  • 1,543
  • 3
  • 25
  • 49
  • Are you having problems with getting a menu to open on the right click or what to do once you have the menu? – Preston May 02 '11 at 20:02
  • Well basically - which solution is better and what are the steps to accomplish that solution. I would also appreciate some keywords what I need to look for. - I mean: - table selector on row event listener -> check if right mouse was click -> open menu -> give id as parameter -> call the method that you want to call with the parameter -> go into the array list and do this and that. I am looking for answers like that. But I cannot evaluate which solution is the best for a 1 class problem and no redundant code - as I said my brain is totally blocked at the moment. – Richard May 02 '11 at 20:03
  • 2
    Here's a link for getting the table to listen to a right click. It shows how to find the row selected row as well. From there you can do what you need. http://www.stupidjavatricks.com/?p=12 I searched for "jtable rightclick menu" – Preston May 02 '11 at 20:05
  • ok that helps me with the context menu as I can call a popup menu - thanks for that. – Richard May 02 '11 at 20:07
  • `Property`, `PropertyToSell` and `PropertyToLet` are undefined. – trashgod May 02 '11 at 20:09
  • @trashgod please dont get me wrong here, but I am not looking for somebody to finish my task - I want to do it on my own, therefore I am not going to post the other classes online - despite the fact that it might get me into trouble if a professor sees that. So I need help on the theoretical part - I just thought a showing the code helps understanding the problem. The code of those classes is not really interesting as it is just somekind of -addTenant(), removeTenant(), getNumberOfBedrooms() and so on - so there is no real functionality behind it basically just setter and getter – Richard May 02 '11 at 20:13
  • I was unclear: the goal is not to post more code, but to reduce the problem to a short, self-contained example [sscce](http://sscce.org/). – trashgod May 02 '11 at 20:15
  • 1
    See also [Table Button Column](http://tips4java.wordpress.com/2009/07/12/table-button-column/); there's an example [here](http://stackoverflow.com/questions/5555938/how-to-make-a-jbutton-in-a-jtable-cell-click-able/5559747#5559747). – trashgod May 02 '11 at 20:21
  • @ trashgod please add to your example http://stackoverflow.com/questions/5555938/how-to-make-a-jbutton-in-a-jtable-cell-click-able/5559747#5559747 into block inCrease and deCrease model#fireTableCellUpdated (sure better would be to the model) – mKorbel May 02 '11 at 21:32
  • @mKorbel: Thanks! `TableModel` updated. – trashgod May 03 '11 at 02:01

1 Answers1

2

Since jdk5, the recommended way to attach a JPopupMenu to a component is

   component.setComponentPopupMenu(menu)

This popup is automatically shown when a user gesture (mouse or keyboard) is interpreted as popup trigger - which may vary across OSs.

As to selecting a row on right (popup-trigger) mouseEvent: it's not done by default in Swing, but seems to be the norm nowadays for (near-)native applications (on vista) - arguably a but in Swing. Volunteers to report, anybody ;-)

kleopatra
  • 51,061
  • 28
  • 99
  • 211