wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Data exchange between JPanels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Data exchange between JPanels" Watch "Data exchange between JPanels" New topic
Author

Data exchange between JPanels

Roberto De Giuli
Greenhorn

Joined: Feb 01, 2013
Posts: 17

I've been writing a simple swing application described in the picture below, but now I'm stuck with a problem I can't solve:



I've written a JFrame class and two other classes that extend JPanel. The JFrame contains the two JPanel subclasses: ButtonsPanel e ContentPanel.
The JButton mouse clicks located in the first JPanel subclass (ButtonsPanel) are caught by the ActionListener (always in the same JPanel).
The problem is the following: the ActionListener in the first JPanel should tell to the JFrame container to load the second JPanel.
Am I doing something wrong? I thank you very much for any help or suggestion you could give me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Afraid I am getting suspicious about all sorts of things you said.
It sounds as though you have a panel which appears when you click the button. That would give a very peculiar effect. If you want something to appear when you click, you usually use a dialogue (by the way, is green eggs and ham an allusion to a Dr Seuss book?). That will give the appearance people usually get when they click a button and a box appears.
What have you added the ActionListener to? You should usually add it to the button in question.
Roberto De Giuli
Greenhorn

Joined: Feb 01, 2013
Posts: 17

I'll try to explain myself better, because I don't need a Dialog.

The main window (JFrame) has a JPanel on the left (in BorderLayout.WEST position) with 3 JButtons: buttonBook, buttonAuthor and buttonYear. This class is called ButtonsPanel which extends JPanel.

Some code in the JFrame class:



In the ButtonsPanel class I have this code:



and



What the application should do, when the JButton is clicked, is loading in the JFrame (jFrameObject.add(new BooksPanel(), BorderLayout.CENTER)) the proper JPanel.
So, if I click the Books JButton in the JPanel, the JFrame should load the BooksPanel with a view of my books by title. If I click the Author JButton, the JFrame should load the Authors panel and so on.

I'm starting to think that the Buttons JPanel should not be a subclass but an object of the JFrame.
Once again, any suggestion is more than welcome. Thank you.

Roberto
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2166
    
  47
What the application should do, when the JButton is clicked, is loading in the JFrame (jFrameObject.add(new BooksPanel(), BorderLayout.CENTER)) the proper JPanel.
So, if I click the Books JButton in the JPanel, the JFrame should load the BooksPanel with a view of my books by title. If I click the Author JButton, the JFrame should load the Authors panel and so on.

As Campbell has said this is just not how GUI's normally work, it could cause unexpected resizing behaviour and may well be confusing to the user. You need to add a default panel in the right hand area on start up and display the appropriate values in it as and when buttons are pressed. If you have different layouts for each of the possible displays that go in this area, look at using a CardLayout.

I'm starting to think that the Buttons JPanel should not be a subclass but an object of the JFrame.

No don't do that, it is a retrograde step.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Get rid of the extends JPanel implements ActionListener syntax. Create individual listener classes and add them individually to your three buttons.
You can add a text pane, text box or text field to the EAST part of the frame, and alter its text depending which button has been pressed.
Roberto De Giuli
Greenhorn

Joined: Feb 01, 2013
Posts: 17

Thank you both for your suggestion, I clearly misunderstood how components should work and interact.
Please bear with me and make one concept clearer, because I'm not sure I've got it. I will get rid of all classes like "extends JPanel implements ActionListener" and I try to use one JPanel to display my data and here is my question.
What I'm trying to do is making three different table available to the user:
1) a JTable with all the books in my library and some more buttons to interact with the table;
2) a JTable with all the author and some more buttons to interact even with the table;
3) another table, another view.

Apart from my previous bad design, I thought that using three different JPanels could be a good idea to keep the code in order and they are easier to use.
If I used just one JPanel I would have to remove and add JTables and JButtons every time I choose a different view. Did I get something wrong?
Once again, thank you for your help.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I thought that using three different JPanels could be a good idea to keep the code in order and they are easier to use.

as mentioned earlier, another panel set as a CardLayout to hold those 3 views would be ideal.
A CardLayout is basically a JTabbedPane without the tabs, where you use e.g. menuItems to 'show' the view/tab you want.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
I didn’t mean to get rid of the panels. I meant to get the Listeners out of the panels into other classes. Even though many books show abominations like extends JFrame implements ActionListener.
Roberto De Giuli
Greenhorn

Joined: Feb 01, 2013
Posts: 17

Campbell Ritchie wrote:I didn’t mean to get rid of the panels. I meant to get the Listeners out of the panels into other classes. Even though many books show abominations like extends JFrame implements ActionListener.


Can you please suggest me a good book to learn good java swing design patterns? Anyway, I'm trying to change my code using a CardLayout and custom listeners.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
I am looking at Horstmann and Cornell Core Java II (vol 1) at the moment. I have always found that book good.Do you know about the Bunkhouse?
Roberto De Giuli
Greenhorn

Joined: Feb 01, 2013
Posts: 17

Campbell Ritchie wrote:I am looking at Horstmann and Cornell Core Java II (vol 1) at the moment. I have always found that book good.Do you know about the Bunkhouse?


Thanks for the suggestion. I didn't know about the Bunkhouse, it's very interesting!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
You’re welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Data exchange between JPanels
 
Similar Threads
Simple java layout question
GUI interaction
Can't update GUI from thread - need syntax for invokeLater
Listeners And OOP
Problem with FOR loop..Need Help ASAP...