Example usage for javax.swing JComboBox JComboBox

List of usage examples for javax.swing JComboBox JComboBox

Introduction

In this page you can find the example usage for javax.swing JComboBox JComboBox.

Prototype

public JComboBox(Vector<E> items) 

Source Link

Document

Creates a JComboBox that contains the elements in the specified Vector.

Usage

From source file:MainClass.java

public MainClass() {
    // Build a mapping from book titles to their entries
    for (int i = 0; i < items.length; i++) {
        itemMap.put(items[i].getTitle(), items[i]);
    }//from w  ww . j  av  a2 s  . c  o m

    setLayout(new BorderLayout());

    JComboBox bookCombo = new JComboBox(items);
    bookCombo.setEditable(true);
    bookCombo.setEditor(new MyComboBoxEditor(itemMap, items[0]));
    bookCombo.setMaximumRowCount(4);
    bookCombo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("You chose " + ((JComboBox) e.getSource()).getSelectedItem() + "!");
        }
    });
    bookCombo.setActionCommand("Hello");
    add(bookCombo, BorderLayout.CENTER);
}

From source file:CascadeDemo.java

public CascadeDemo() {
    super("CascadeDemo");
    EARTH = new ImageIcon("earth.jpg");
    m_count = m_tencount = 0;//from   www  .  j  av  a 2  s  . c o m

    m_desktop = new JDesktopPane();
    m_desktop.putClientProperty("JDesktopPane.dragMode", "outline");
    m_newFrame = new JButton("New Frame");
    m_newFrame.addActionListener(this);

    m_infos = UIManager.getInstalledLookAndFeels();
    String[] LAFNames = new String[m_infos.length];
    for (int i = 0; i < m_infos.length; i++) {
        LAFNames[i] = m_infos[i].getName();
    }
    m_UIBox = new JComboBox(LAFNames);
    m_UIBox.addActionListener(this);

    JPanel topPanel = new JPanel(true);
    topPanel.add(m_newFrame);
    topPanel.add(new JLabel("Look & Feel:", SwingConstants.RIGHT));
    topPanel.add(m_UIBox);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add("North", topPanel);
    getContentPane().add("Center", m_desktop);

    setSize(570, 400);
    Dimension dim = getToolkit().getScreenSize();
    setLocation(dim.width / 2 - getWidth() / 2, dim.height / 2 - getHeight() / 2);
    setVisible(true);
    WindowListener l = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(l);
}

From source file:SystemFontDisplayer.java

public SystemFontDisplayer() {
    Container container = getContentPane();
    displayPanel = new DisplayPanel();
    container.add(displayPanel);/* w  ww. ja va  2  s . c o m*/
    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new GridLayout(1, 3));

    fontsBox = new JComboBox(displayPanel.fontFamilyNames);
    fontsBox.setSelectedItem("Arial");
    fontsBox.addActionListener(new ComboBoxListener());

    fontStylesBox.addActionListener(new ComboBoxListener());

    fontSizesBox.setSelectedItem("36");
    fontSizesBox.addActionListener(new ComboBoxListener());
    controlPanel.add(fontsBox);
    controlPanel.add(fontStylesBox);
    controlPanel.add(fontSizesBox);
    container.add(BorderLayout.SOUTH, controlPanel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setSize(400, 250);
    setVisible(true);
}

From source file:Main.java

public JPanel getContent() {
    String[] ids = { "north", "west", "south", "east" };
    Boolean[] values = { Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE };
    CheckComboStore[] stores = new CheckComboStore[ids.length];
    for (int j = 0; j < ids.length; j++)
        stores[j] = new CheckComboStore(ids[j], values[j]);
    JComboBox combo = new JComboBox(stores);
    combo.setRenderer(new CheckComboRenderer());
    combo.addActionListener(this);
    JPanel panel = new JPanel();
    panel.add(combo);/*w ww.ja  va 2  s.  c o  m*/
    return panel;
}

From source file:AffineTransformApp.java

public AffineTransformApp() {
    super();/*from  w  w  w.j  a v a2s.  c  o  m*/
    Container container = getContentPane();

    displayPanel = new DisplayPanel();
    container.add(displayPanel);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(2, 4, 5, 5));
    scaleXval = new JComboBox(scaleValues);
    scaleXval.setSelectedItem("1.00");
    scaleXval.addActionListener(new ComboBoxListener());
    scaleYval = new JComboBox(scaleValues);
    scaleYval.setSelectedItem("1.00");
    scaleYval.addActionListener(new ComboBoxListener());

    shearXval = new JComboBox(shearValues);
    shearXval.setSelectedItem("0.00");
    shearXval.addActionListener(new ComboBoxListener());
    shearYval = new JComboBox(shearValues);
    shearYval.setSelectedItem("0.00");
    shearYval.addActionListener(new ComboBoxListener());

    panel.add(new JLabel("Scale X value:"));
    panel.add(scaleXval);
    panel.add(new JLabel("Scale Y value:"));
    panel.add(scaleYval);
    panel.add(new JLabel("Shear X value:"));
    panel.add(shearXval);
    panel.add(new JLabel("Shear Y value:"));
    panel.add(shearYval);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    setSize(displayPanel.getWidth(), displayPanel.getHeight() + 10);
    setVisible(true);
}

From source file:Main.java

public final JToolBar createToolBar() {

    toolbar = new JToolBar();

    shapeLabel = new JLabel("Shapes: ");
    addToToolbar(shapeLabel, 0, 1);/* ww w  . j av  a  2  s.c  om*/

    shapeChooser = new JComboBox(shapeNames);
    shapeChooser.setSelectedIndex(0);
    addToToolbar(shapeChooser, 0, 2);
    colorLabel = new JLabel("Colors: ");

    addToToolbar(colorLabel, 0, 3);

    colorChooser = new JComboBox(colorNames);
    addToToolbar(colorChooser, 0, 4);

    return toolbar;
}

From source file:AlphaCompositeDemo.java

public AlphaCompositeDemo() {
    super();/* w  w  w .  j  a  v  a 2  s  .  c  om*/
    Container container = getContentPane();

    canvas = new MyCanvas();
    container.add(canvas);

    rulesBox = new JComboBox(rulesLabels);
    rulesBox.setSelectedIndex(0);
    rulesBox.setAlignmentX(Component.LEFT_ALIGNMENT);
    rulesBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
            canvas.compositeRule = rules[cb.getSelectedIndex()];
            canvas.repaint();
        }
    });

    slider.setPaintTicks(true);
    slider.setMajorTickSpacing(25);
    slider.setMinorTickSpacing(25);
    slider.setPaintLabels(true);
    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            JSlider slider = (JSlider) e.getSource();
            canvas.alphaValue = (float) slider.getValue() / 100;
            canvas.repaint();
        }
    });

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1, 3));
    panel.add(rulesBox);
    panel.add(new JLabel("Alpha Adjustment x E-2: ", JLabel.RIGHT));
    panel.add(slider);
    container.add(panel, BorderLayout.SOUTH);

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    setSize(500, 300);
    setVisible(true);
}

From source file:Main.java

public MyComboBoxEditor(String[] items) {
    super(new JComboBox(items));
}

From source file:org.jfree.chart.demo.RightPanel.java

public RightPanel(int x, int y, int width, int height, String baude[], String name[]) {

    setBackground(new Color(176, 199, 246));

    this.width = width;
    this.height = height;

    Data[0] = "8";
    Data[1] = "9";

    Name = name;/*from   w  ww.  ja va2  s  . co m*/
    Baude = baude;

    setLayout(new GridLayout(8, 1, 0, 0));

    lblName = new JLabel("Name");
    lblName.setHorizontalAlignment(SwingConstants.CENTER);
    lblName.setFont(new Font("Arial", 15, 16));
    add(lblName);

    comboBox = new JComboBox(Name);
    comboBox.setFont(new Font("Arial", 15, 16));
    add(comboBox);

    JLabel lblBaude = new JLabel("Baude");
    lblBaude.setFont(new Font("Arial", 15, 16));
    lblBaude.setHorizontalAlignment(SwingConstants.CENTER);
    add(lblBaude);

    final JComboBox comboBox_1 = new JComboBox(Baude);
    comboBox_1.setFont(new Font("Arial", 15, 16));
    add(comboBox_1);

    JLabel lblDataSize = new JLabel("Data size");
    lblDataSize.setHorizontalAlignment(SwingConstants.CENTER);
    lblDataSize.setFont(new Font("Arial", 15, 16));
    add(lblDataSize);

    final JComboBox comboBox_2 = new JComboBox(Data);
    comboBox_2.setFont(new Font("Arial", 15, 16));
    add(comboBox_2);

    JSeparator separator = new JSeparator();
    add(separator);

    btnOpen = new JButton("Open");
    btnOpen.setFont(new Font("Arial", 15, 16));
    btnOpen.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (btnOpen.getText() == "Open") {
                btnOpen.setText("Close");
                open = true;
                int baude = 9600, data = 8;
                try {
                    baude = Integer.parseInt((String) comboBox_1.getSelectedItem());
                    data = Integer.parseInt((String) comboBox_2.getSelectedItem());
                } catch (Exception e) {
                    errors += "Enter correct Bauderate\n";
                    error_flag = true;

                }
                comport = new Comport((String) comboBox.getSelectedItem(), baude, data);
                comport.start();

            } else {
                comport.flag = false;
                btnOpen.setText("Open");
                open = false;

                comport.close();
                //   comport.stop();

                System.out.println("Comport = " + comport.isAlive());
                System.out.println("Comport life  = " + comport.get_life());

            }
        }
    });
    add(btnOpen);
    setBounds(x + 50, y, width - 40, height - 20);
    err.setBounds(x + 50, y, 50, 50);
    //add(err);

}

From source file:ExpenseReport.java

public ExpenseReport() {
    super("Expense Report");
    setSize(570, 200);//from   w  w  w.  j av  a 2 s .c o  m

    m_data = new ExpenseReportData(this);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);
    m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    for (int k = 0; k < ExpenseReportData.m_columns.length; k++) {
        TableCellRenderer renderer;
        if (k == ExpenseReportData.COL_APPROVED)
            renderer = new CheckCellRenderer();
        else {
            DefaultTableCellRenderer textRenderer = new DefaultTableCellRenderer();
            textRenderer.setHorizontalAlignment(ExpenseReportData.m_columns[k].m_alignment);
            renderer = textRenderer;
        }

        TableCellEditor editor;

        if (k == ExpenseReportData.COL_CATEGORY)
            editor = new DefaultCellEditor(new JComboBox(ExpenseReportData.CATEGORIES));
        else if (k == ExpenseReportData.COL_APPROVED)
            editor = new DefaultCellEditor(new JCheckBox());
        else
            editor = new DefaultCellEditor(new JTextField());

        TableColumn column = new TableColumn(k, ExpenseReportData.m_columns[k].m_width, renderer, editor);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.setSize(550, 150);
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

    ImageIcon penny = new ImageIcon("penny.gif");
    m_title = new JLabel("Total: $", penny, JButton.LEFT);
    m_title.setForeground(Color.black);
    m_title.setAlignmentY(0.5f);
    p.add(m_title);
    p.add(Box.createHorizontalGlue());

    JButton bt = new JButton("Insert before");
    bt.setMnemonic('b');
    bt.setAlignmentY(0.5f);
    ActionListener lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row);
            m_table.tableChanged(
                    new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Insert after");
    bt.setMnemonic('a');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row + 1);
            m_table.tableChanged(new TableModelEvent(m_data, row + 1, row + 1, TableModelEvent.ALL_COLUMNS,
                    TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Delete row");
    bt.setMnemonic('d');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            if (m_data.delete(row)) {
                m_table.tableChanged(new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS,
                        TableModelEvent.INSERT));
                m_table.repaint();
                calcTotal();
            }
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    getContentPane().add(p, BorderLayout.SOUTH);

    calcTotal();

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}