How to Change UI Depending on Combo Box Selection

how to change UI depending on combo box selection

CardLayout works well for this, as suggested below.

Sample Image

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/questions/6432170 */
public class CardPanel extends JPanel {

private static final Random random = new Random();
private static final JPanel cards = new JPanel(new CardLayout());
private static final JComboBox combo = new JComboBox();
private final String name;

public CardPanel(String name) {
this.name = name;
this.setPreferredSize(new Dimension(320, 240));
this.setBackground(new Color(random.nextInt()));
this.add(new JLabel(name));
}

@Override
public String toString() {
return name;
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
create();
}
});
}

private static void create() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 1; i < 9; i++) {
CardPanel p = new CardPanel("Panel " + String.valueOf(i));
combo.addItem(p);
cards.add(p, p.toString());
}
JPanel control = new JPanel();
combo.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JComboBox jcb = (JComboBox) e.getSource();
CardLayout cl = (CardLayout) cards.getLayout();
cl.show(cards, jcb.getSelectedItem().toString());
}
});
control.add(combo);
f.add(cards, BorderLayout.CENTER);
f.add(control, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

How to go to next JPanel depending on combo box selection?

Use a CardLayout. It allows you to swap panels in the same place on the frame.

Read the section from the Swing tutorial on How to Use a CardLayout for a working example that does exactly what you want.

Change UI-layout based on selected ComboBoxItem

I got it to work due to the help of HighCores comment.
I looked at this link about TabControl and transfered it to ComboBox.

So my XAML looks like this:

    <ComboBox Name="RouteOptions"
ItemsSource="{Binding}"
DisplayMemberPath="DisplayName"/>

<ContentPresenter
Content="{Binding SelectedItem, ElementName=RouteOptions}"/>

And for the rest I just followed the instructions of the answer of the mentioned link.

GUI: Changing panels based on value of combo box

You could use the Factory Pattern to create a UI widget for the element that you are selecting. You could also use it to create a validation rule object depending on the type. This would give you some of the flexibility you desire.

So you can have something like:

IWidget widget = UIFactory.createFor(myObject.getType())

That can be invoked on the selection event to create the right widget to edit the selected element.

The IWidget could have methods such as:

validateData()
refreshData()
setDataElement(IDataElement element)

That would allow you to treat all UI widgets generically, but still have a special UI widget for each element type. I am assuming that the elements that you are selecting from the table all implement some IDataElement interface that contains the getType() method.

I used this solution tied together with the Eclipse Extension mechanism to plug-in new UI elements into my "base" solution, to have an extensible core and a high level of reuse. You could achieve something similar by injecting types and widgets into your factory, either manually or with Spring.

Qt How to update GUI QFrame with combobox selection

You're going about it the right way. But Qt actually makes it even easier than what you're thinking.

You can use two built-in widgets: QComboBox and QStackedWidget. You're familiar with the combo box; the stacked widget is a set of widgets, of which only one will be displayed at a time. It's essentially a tabbed widget minus the tabs.

Set up your stacked widget so that its first widget is what you want to show when your combo box is showing its first option, the second for the second, and so on. Then you can connect a built-in signal to a built-in slot: QComboBox::currentIndexChanged(int) to QStackedWidget::setCurrentIndex(int).

Hope that helps!

How to display controls based on Combobox Selection in Windows Application at edit time?

So you have populated the combobox with items of type CompanyType enum. so the selected item should also the same type. Hope that you are getting a string from company.Element("DataSourceType").Value; so you can modify the code like the following:

cmbbx_companyType.SelectedItem = Enum.Parse(typeof(CompanyType),type);        

Please make a try and let me know whether it solve the issues or not.



Related Topics



Leave a reply



Submit