-
[SOLVED] Adding ComboBox
Hi,
I am trying to add a combo box to frame which already contains a button. I have done the following code but for some reason it doesn't work. Can you please show me where I have gone wrong?
The code in red is the code for the combo box. The rest of the code is for button which is working fine without the combo box.
Thank you
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
/**
*
* @author Owner
*/
public class NewClass extends JPanel implements ActionListener {
private JLabel picture;
protected JButton b1;
public void Main() {
b1 = new JButton("Disable middle button");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("disable");
//Listen for actions on buttons 1 and 3.
b1.addActionListener(this);
b1.setToolTipText("Go");
//Add Components to this container, using the default FlowLayout.
add(b1);
}
public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
b1.setEnabled(true);
} else {
b1.setEnabled(true);
}
}
/** Returns an ImageIcon, or null if the path was invalid. */
/**
* Combo box
*/
[COLOR="Red"]public void ComboBoxDemo() {
// super(new BorderLayout());
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
//Create the combo box, select the item at index 0.
JComboBox petList = new JComboBox(petStrings);
petList.setSelectedIndex(0);
petList.addActionListener(this);
//Lay out the demo.
picture = new JLabel();
picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
picture.setHorizontalAlignment(JLabel.CENTER);
add(petList, BorderLayout.PAGE_START);
add(picture, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
public void comActionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String petName = (String)cb.getSelectedItem();
updateLabel(petName);
}
protected void updateLabel(String name) {
ImageIcon icon = createImageIcon("images/" + name + ".gif");
picture.setIcon(icon);
picture.setToolTipText("A drawing of a " + name.toLowerCase());
if (icon != null) {
picture.setText(null);
} else {
picture.setText("Image not found");
}
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = NewClass.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
[/COLOR]
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("ButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
Main newContentPane = new Main();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.setPreferredSize(new Dimension(300, 300));
[COLOR="DarkOrange"] There is an error here which states content pane is already defined. [/COLOR]
[COLOR="Red"]//Create and set up the content pane.
JComponent newContentPane = new NewClass();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
[/COLOR]
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
-
Code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
public class NewClassRx extends JPanel implements ActionListener {
private JLabel picture;
protected JButton b1;
// This method was not being called.
// public void Main() {
// You could name it something like "initComponents"
// or you could put the code inside a constructor:
public NewClassRx() {
b1 = new JButton("Disable middle button");
b1.setVerticalTextPosition(AbstractButton.CENTER);
//aka LEFT, for left-to-right locales
b1.setHorizontalTextPosition(AbstractButton.LEADING);
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("disable");
//Listen for actions on buttons 1 and 3.
b1.addActionListener(this);
b1.setToolTipText("Go");
//Add Components to this container, using the default FlowLayout.
add(b1);
addComboBox();
}
public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
b1.setEnabled(true);
} else {
b1.setEnabled(true);
}
}
/**
* Combo box
*/
public void addComboBox() {
// super(new BorderLayout());
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
//Create the combo box, select the item at index 0.
JComboBox petList = new JComboBox(petStrings);
petList.setSelectedIndex(0);
petList.addActionListener(this);
//Lay out the demo.
picture = new JLabel();
picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
picture.setHorizontalAlignment(JLabel.CENTER);
add(petList, BorderLayout.PAGE_START);
add(picture, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
public void comActionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String petName = (String)cb.getSelectedItem();
updateLabel(petName);
}
protected void updateLabel(String name) {
ImageIcon icon = createImageIcon("images/" + name + ".gif");
picture.setIcon(icon);
picture.setToolTipText("A drawing of a " + name.toLowerCase());
if (icon != null) {
picture.setText(null);
} else {
picture.setText("Image not found");
}
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = NewClassRx.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("ButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
// Main is a different class, not defined here.
// Main newContentPane = new Main();
// The enclosing class is NewClassRx:
NewClassRx newContentPane = new NewClassRx();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.setPreferredSize(new Dimension(300, 300));
//There is an error here which states content pane is already defined.
// Yes. You can see the reference "newContentPane" declared
// above and below.
//Create and set up the content pane.
/*
JComponent newContentPane = new NewClass();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
*/
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
-
I am afraid even your code dont show what I am looking for. Your code does open a window and it has only a button. I dont see the combobox in it. Do you know why?
Thanks