Forums Register Login

Data exchange between JPanels

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
> 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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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!
+Pie Number of slices to send: Send
You’re welcome
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1453 times.
Similar Threads
First GUI program
JMenu help
Unable to implement detection of click on a JButton using Swing and AWT
JButtons switching states(icons) too quickly
Can't update GUI from thread - need syntax for invokeLater
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 00:53:43.