0

I have a registration panel where if student, instructor, and course administrator can register so if student is selected it should show something like this when Student is selected:

https://i.stack.imgur.com/cKrdf.png

and should show like this if other two are selected like this when any of other two are selected:

https://i.stack.imgur.com/Tpi1D.png

I tried using if condition on the selected item in where I have added those text fields but it seems it only works at the beginning of the program when I run it on the basis of what is pre-selected and does not change when I select other items in JComboBox. Is there a solution to this?

Nutan Panta
  • 65
  • 2
  • 7
  • Use a `CardLayout`. Read the section from the Swing tutorial on [How to Use CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). The example shows how to "swap" panels when an item is selected from a combo box. – camickr Jan 18 '21 at 16:25
  • @camickr thank you so much I been trying to find the solution for some time now. Thank You. – Nutan Panta Jan 18 '21 at 16:28

1 Answers1

1

You can achieve this in different ways. One of such ways is to use Action Listeners. A JComboBox object generates an action event when a selection is made (see Handling Events on a Combo Box).

In your case, you need to trigger an event based on the selection made in a combo box. This action should change the visibility of components in your panel, which are simply changing the visible attribute from true to false (or vice versa) depending on the selection made.

hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • yes thanks it worked but I have use gridbagconstaints to add those fields so when those fields are set visible to false below the register button there is gap so how can i remove that gap? – Nutan Panta Jan 18 '21 at 18:22
  • @NutanPanta *there is gap so how can i remove that gap?* and that is why you should not play with the visibility of individual components. Each layout manager will determine whether to respect visibility or not. The CardLayout is designed for this requirement. – camickr Jan 18 '21 at 20:17
  • @camickr Your statement is not accurate. As the name infers, a Layout Manager is responsible mainly for the positioning of components in a container. As you suggested, a `CardLayout` could achieve something similar to this by creating different views (panels) with different configuration and component states, but "visibility of components" per se is not a explicit responsibility of any layout manager. Layout and state of components are two distinct things. – hfontanez Jan 18 '21 at 22:04
  • @NutanPanta You can do as camickr suggested or you could also remove the component from the container and rebuild it and add as needed. The main drawback of using a card layout for something like this is that the number of panels could increase with the number of selections you make. If your problem will stay the same, it might be easier FOR THIS SPECIFIC CASE to create two panels and switch between them depending on the selection. But you could also remove unwanted components altogether. Only you know what is needed. Regardless, the trigger for the event is the same. – hfontanez Jan 18 '21 at 22:10
  • @NutanPanta assume that you have something like 20 user types with different requirements. Worst case, you would need 20 panels using Card Layout. At any given point, 19 of these panels will be in memory not doing anything. You could instead create a solution that discard unnecessary components and add them only when needed. You, as the designer, must determine which of the "solution" fits your specific needs best. That said, hidden" components are almost as bad as hidden panels. But, I think it might be slightly better in memory because it should be less objects in memory. – hfontanez Jan 18 '21 at 22:14
  • @hfontanez *a Layout Manager is responsible mainly for the positioning of components* - correct. And some layout managers will "save" a position for an invisible component in the layout, which is why you can have a gap between components which the OP does not want. *assume that you have something like 20* - well the OP stated there are 3 , but in any case that is exactly why you want to use a CardLayout. You can easily manage and group the components onto individual panels instead of using a large nested if statement. This make the program much easier to maintain. – camickr Jan 19 '21 at 02:11
  • *discard unnecessary components and add them only when needed.* - that is a mess to maintain. Inserting components on a panel can become messy, especially when you need to remember the constraints needed to effectively position the components on the panel. Build each child panel once and swap panels and you have less maintenance issues and complex layout logic – camickr Jan 19 '21 at 02:11
  • 1
    I think @camickr is right in this case not only it causes problems with my layout it is causing extra problems while adding data. – Nutan Panta Jan 19 '21 at 03:35
  • @NutanPanta if you remember I stated that "it might be easier FOR THIS SPECIFIC CASE" to do what camickr suggested. I am trying to give you possibilities so that, if the problem statement changes slightly, you don't get stuck solving it this specific way. I even stated that hidden components might be just as bad as creating many panels in a CardLayout. Ultimately, you must decide what is right for this issue and also learn that it is not the only way to solve these sort of problems so that you can apply a different solution if it is merited. – hfontanez Jan 19 '21 at 05:48
  • @camickr you stated that "discard unnecessary components and add them only when needed. - that is a mess to maintain." This is not necessarily true. It can be, but not always. This is why you have factories and other creational patterns. But this is not the place to discuss this. You may think that I am knocking your suggestion and this is simply not so. On my second post I stated that the OP could do what you suggested or what I suggested, offered some pros and cons, and told the OP that it is up to him or her to decide what is best for this issue. – hfontanez Jan 19 '21 at 05:52