• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Data exchange between JPanels

 
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic