This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Getting information into a JTable from a drawing Canvas Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting information into a JTable from a drawing Canvas" Watch "Getting information into a JTable from a drawing Canvas" New topic
Author

Getting information into a JTable from a drawing Canvas

Kieran Murray
Ranch Hand

Joined: May 18, 2008
Posts: 47
Dear Ranchers,

I have a drawing Canvas on which I draw various things.
I can right-click on them and bring up a JPopupMenu.
I have a JTable also that is in its own JFrame.


I am not sure how to implement the following procedures.



The objects on the canvas refer to real electrical elements and I want to pass information to the JTable about the element clicked on.

I need to pass about 6 TextFields for each element.
Do I use a JPanel to do this?

Whichever container I use, I have to be able to move it around on screen.

I enter the data and then click a button and then pass this data to the JTable.

The container closes.

Kind regards,

The Apprentice
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Sounds like a job for Model View Controller - make model objects for the data you need for the electrical elements. You'll probably need to make another model object to consolidate the elements you want to display in the canvas, to collect them and perhaps hold information about connections. The canvas will be the view for this model. Then, you'll want to make another model object by implementing TableModel (or extending DefaultTableModel if you can) to hold all the electrical element model objects you want to appear in the table. A JTable will be the view of this model. Then you write controllers - these will be the listeners and event handlers that pass electrical element model objects between the canvas and the table, handle adding/deleting elements from the canvas or table, etc.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Kieran Murray
Ranch Hand

Joined: May 18, 2008
Posts: 47
Dear Nate,

Thanks. I am trying to implement this MVC philosophy and am quite some way down the path but I am not sure about passing information from one package to another. My JTable is in one package - tables and my drawing Canvas is in another - translation.
I can input information into my JTable from the screen. However, I also want to be able to pass information into the JTable from the Drawing Canvas - this is where it all gets fuzzy.

This is the code for the popup Window to input data for the Generator from the Drawing Canvas



How do I get the information from here into this



I should press the EnterButton in the Generator Input Details class and then somehow get the information from the JTextFields and pass it on. It is this that I do not understand.

Kind regards,

Kieran
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Please edit your code to get rid of the very long lines which make it very difficult to read. It is also impossible to try out because you are using a non-standard class (SpringUtilities) and not telling us where to get it from.

You would need to import the tables.GeneratorData class, and pushing the buttons would create a GeneratorData object.
Kieran Murray
Ranch Hand

Joined: May 18, 2008
Posts: 47
Dear Ranchers,

Thank you for your help. Finally settled on the code below. The important method for passing of data is setGeneratorValues();
I import tables.*;



The code SpringUtilities is from the Java Sun Tutorial on Spring layouts.

Thanks again,

Kieran
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
You're welcome; not sure how much I helped.

And I could never understand SpringLayout anyway.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting information into a JTable from a drawing Canvas
 
Similar Threads
JTable
jtable
JTable(!!!)
Jtable
JTable