JComboBox and CardLayout

91 views
Skip to first unread message

abendigo

unread,
Dec 4, 2010, 11:13:56 PM12/4/10
to JavaBuilders
Howdy!

I've just started playing with Swing Javabuilders for a little proof-
of-concept project at work. I am looking for an example of using JList
and/or JComboBox, preferably with CardLayout. Ideally I would like to
re implement this example:
http://download.oracle.com/javase/tutorial/uiswing/examples/layout/CardLayoutDemoProject/src/layout/CardLayoutDemo.java

abendigo

unread,
Dec 4, 2010, 11:35:21 PM12/4/10
to JavaBuilders
Wow. Don't I feel the fool. I found the sample I needed in the full
package download.

In my defense, I was using Maven to download the jars for me, and that
does not fetch the samples.

On Dec 4, 11:13 pm, abendigo <m...@oosterveld.org> wrote:
> Howdy!
>
>   I've just started playing with Swing Javabuilders for a little proof-
> of-concept project at work. I am looking for an example of using JList
> and/or JComboBox, preferably with CardLayout. Ideally I would like to
> re implement this example:http://download.oracle.com/javase/tutorial/uiswing/examples/layout/Ca...

abendigo

unread,
Dec 6, 2010, 9:19:05 PM12/6/10
to JavaBuilders
In case any one else is interested, here is what I ended up with:

CardLayoutDemo.yaml:

JFrame:
- JComboBox(name=combo, onAction=onAction):
- SOmeTHing
- JPanel(name=cardpanel):
- JPanel(name=panel1):
- JButton(name=button1,text=Button 1)
- JButton(name=button2,text=Button 2)
- JButton(name=button3,text=Button 3)
- JPanel(name=panel2):
- JTextField(name=textField, text=Some Text)
- CardLayout(name=cards): [panel1,panel2]
- MigLayout: |
[grow]
combo
cardpanel

bind:
- combo.model: this.choices


CardLayoutDemo.java:

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.javabuilders.swing.SwingJavaBuilder;

@SuppressWarnings("serial")
public class CardLayoutDemo extends JFrame {

private List<String> choices = new LinkedList<String>();
private JPanel cardpanel;
private CardLayout cards;

public CardLayoutDemo() throws Exception {

choices.add("Card with JButtons");
choices.add("Card with JTextField");

SwingJavaBuilder.build(this);
}

public List<String> getChoices() {
return choices;
}

public void onAction(JComboBox box, ActionEvent event) {
if (cards != null)
switch(box.getSelectedIndex()) {
case 0:
cards.show(cardpanel, "panel1");
break;
case 1:
cards.show(cardpanel, "panel2");
break;
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new CardLayoutDemo().setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
Reply all
Reply to author
Forward
0 new messages