Hi, I am working on code to have a drop down menu from which you can pick items and add a button with that course. Unfortunetally, I can't get the button to display. And help is greatly appreciated.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxFrame extends JFrame {
	/***/ private static final long serialVersionUID = 1L;
	
	private JComboBox b;
	private String s;
	private int x = 0;
	private JButton g[] = new JButton[10];

	private String courses[] = 
		{ "Advanced Functions", "Calculus",  "Chemistry", "Physics" };

	public ComboBoxFrame() {
		super("Pick your courses!");
		setLayout(new GridLayout(3, 3));

		b = new JComboBox(courses);

		b.addItemListener( new ItemListener() {
			public void itemStateChanged(ItemEvent event) {
				if(event.getStateChange() == ItemEvent.SELECTED) {
					s = (String) b.getSelectedItem();
					g[x] = new JButton(s);
					add(g[x]);
					//g[x].addActionListener(this);
					x++; 
				} } } );
				
		add(b);
	}
	
/**	public void actionPerformed(ActionEvent e) {
		String s;
		for(int x = 0; x < 10; x++) {
			s = courses[x];
			if(e.getSource() == s) remove(g[x]);
		}
	}*/
	
	public static void main(String args[]) { 
		ComboBoxFrame comboBoxFrame = new ComboBoxFrame();
		comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	    comboBoxFrame.setSize( 350, 150 );
	    comboBoxFrame.setVisible( true );
	}
}

Recommended Answers

All 3 Replies

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxFrame extends JFrame {
	/***/ private static final long serialVersionUID = 1L;
	
	private JComboBox b;
	private String s;
	private int x = 0;
	private JButton g[] = new JButton[10];
	private ComboBoxFrame me;
	private String courses[] = 
		{ "Advanced Functions", "Calculus",  "Chemistry", "Physics" };

	public ComboBoxFrame() {
		super("Pick your courses!");
		setLayout(new GridLayout(3, 3));
		me = this;
		b = new JComboBox(courses);

		b.addItemListener( new ItemListener() {
			public void itemStateChanged(ItemEvent event) {
				if(event.getStateChange() == ItemEvent.SELECTED) {
					s = (String) b.getSelectedItem();
					System.out.println(s);
					g[x] = new JButton(s);
					add(g[x]);
					me.validate();
					//g[x].addActionListener(this);
					x++; 
				} } } );
				
		add(b);
	}
	
/**	public void actionPerformed(ActionEvent e) {
		String s;
		for(int x = 0; x < 10; x++) {
			s = courses[x];
			if(e.getSource() == s) remove(g[x]);
		}
	}*/
	
	public static void main(String args[]) { 
		ComboBoxFrame comboBoxFrame = new ComboBoxFrame();
		comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	    comboBoxFrame.setSize( 350, 150 );
	    comboBoxFrame.setVisible( true );
	}
}

Now try.

Thanks! One more question; I am trying to get the button to remove itself when clicked, but I can't get it working. Here i tried to call the .remove() method and .repaint(), but I can't get it to remove itself. Some insight to the problem would be great, thanks in advance! :)

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class ComboBoxFrame extends JFrame {
	/***/ private static final long serialVersionUID = 1L;
	
	private JComboBox b;
	private String s;
	private TextFieldHandler handler = new TextFieldHandler();
	private int x = 0;
	private JButton g[] = new JButton[10];
	private ComboBoxFrame me;
	private String courses[] = 
		{ "Advanced Functions", "Calculus",  "Chemistry", "Physics", "English", 
			"History", "Law",  "Politics", "Phys. Ed.", "Accounting" };

	public ComboBoxFrame() {
		super("Pick your courses!");
		setLayout(new GridLayout(3, 3));
		me = this;
		b = new JComboBox(courses);

		b.addItemListener( new ItemListener() {
			public void itemStateChanged(ItemEvent event) {
				if(event.getStateChange() == ItemEvent.SELECTED) {
					s = (String) b.getSelectedItem();
					System.out.println(s);
					g[x] = new JButton(s);
					g[x].setBorder(null);
					add(g[x]);
					me.validate();
					g[x].addActionListener(handler);
					x++;
				} } } );
				
		add(b);
	}
	
	private class TextFieldHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String s = "";
			for(int x = 0; x < 10; x++) {
				s = courses[x];
				if(e.getSource() == s) remove(g[x]);
			}
			me.repaint();
		}
	}
	
	public static void main(String args[]) { 
		ComboBoxFrame comboBoxFrame = new ComboBoxFrame();
		comboBoxFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	    comboBoxFrame.setSize( 350, 150 );
	    comboBoxFrame.setVisible( true );
	}
}

You need to register an ActionListener with the buttons as they are created, similarly to how you declared the ItemListener (with b.addItemListener). You can find examples of ActionListeners being used online, here is one similar to the ItemListener you used. http://www.javaprogrammingforums.com/java-tips-tutorials/278-how-add-actionlistener-jbutton-java-swing.html

In your actionPerformed method, you'd just use JFrame's remove() method on the button. So you'd use me.remove((JButton)event.getSource()) where event is the argument to the actionPerformed method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.