• Post Reply Bookmark Topic Watch Topic
  • New Topic

JButton to controll two panels  RSS feed

 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made a small JFrame with BorderLayout. NORTH i have a menu bar with 3 buttons. CENTER i have a JPanel with cardlayout that changes cards depending on data that is worked with currently. There currently are 2 cards on that panel. Im trying to find a good way to bind the buttons from menubar to both of these panels, but i want it to work in such a way, that the buttons are active only to that card, that i can currently see. I had an idea, that i will make actions for each of the button and then bind them on the panels below. But the problem is, when button is fired the action will activate on both panels at the same time, but it should work on only one at a time. Plus i want to bind one of the keys to "Enter". Can someone help me ? Either tell me i have understood the system wrong or tell me how should i implement it.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain what you want to do, not all the implementation details that you are giving us now. Why have you got a button which changes depending on which panel is visible? Can you add the button to the panel?
Did you know that you can get an array of all Listeners added to a button, that you can remove Listeners from a Button, that you can put Listeners into a Map if you have suitable Keys to index them with, and you can therefore change the Listeners applied to a button if some event happens. That can even be controlled via a Listener on the frame which responds to its being visible or invisible.
 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Picture a JPanel with BorderLayout. BorderLayout.NORTH is another JPanel with 3 buttons and some text. BorderLayout.CENTER is another JPanel with CardLayout. CardLayout has 2 cards. Cards are changed depending of the type of data put into the Parent panel. JPanel in the NORTH has a button(actually 2 but it will work similary) that subits whatever is on the panel in the CENTER. But since the CENTER is different depending on the content, what is a good way to bind buttons

In addition in need to bind 1 button to 3 textfields but this time its bit more difficutl. Like before i have cards . on 2 cards i have total of 3 fields. i can only make selections on the fields. now if i press my button how do i know on whitch field i did the selection ?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you have told us how to implement it, not what you want to do. It sounds as though maybe you need a quick fix for a poor design. I have already supplied a quick fix. Another quick fix idea: consider putting some if statements in your Listener codeThere are other options e.g. if (panel3.hasFocus()).
 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK that isVIsibel system i can use . I might have a poor design, but im just trying out new things. Im still trying to find the way to use actions and actionmaps.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actions are good Have you come across lambdas yet? You can use a λ to write an action listener.
 
Marko Taht
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used lambdas on other projects with Java8. But this one is on Java7 so no lambdas.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!