JComboBox

Padawan

Erfahrenes Mitglied
Hallo Leute,

ich habe ein problem. Ich habe eine Oberflächem auf dem ein JComboBox und JList ist.
Mit dem JCombobox soll z.B. Projekte oder Mitarbeiter ausgewählt werden. Bei auswahl von Projekten sollen, die Projekte in der JListe ausgegeben werden, wenn in der JList ein Projekt ausgewählt wird, sollen alle mitarbeiter die dem Projekt zugeordnet sind auf einem JtextArea ausgegebenn werden. Bei auswahl der mitarbeiter, sollen alle Mitarbeiter in der JList ausgegeben werden.

Mein Problem: Ich wähle z.B. Projekte, die Projekte werden abber nicht angezeigt, obwohl diese vorhanden sein müssen.

Ich hoffe Ihr könnt mir helfen.
 
Hallo,

das ist ein typisches Beispiel wo es ohne Code tatsächlich nur ein Rätselraten ist. Ins Blaue geraten:
- Das Event "Auswahl der JComboBox hat sich geändert" ist nicht (richtig) registriert. Bist du dir sicher, dass das Event ausgeführt wird?
- Du verwendest für die JList kein ListModel. Siehe Ausschnitt der Docs:
void setListData(E[] listData)
Constructs a read-only ListModel from an array of items, and calls setModel with this model.
void setListData(Vector<? extends E> listData)
Constructs a read-only ListModel from a Vector and calls setModel with this model.
void setModel(ListModel<E> model)
Sets the model that represents the contents or "value" of the list, notifies property change listeners, and then clears the list's selection.
 
Hi,

hier ist der Code:
Die Oberfläche
Java:
public class MainWindow extends JFrame {
	/** Object for Event listner registration */
	private Facade facade = new Facade();
	
	private static final long serialVersionUID = 1L;
	private static JPanel contentPane;
	private JSeparator separator;
	private JButton btnSearch;
	private static JTextArea txtAreaTerm;
	private JList<String> list = new JList<String>();
	private JComboBox<String> cmbChoice;
	private static JTextField txbSearch;

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		super("Hauptfenster");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 750, 500);
		setVisible(true);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		/** Search Button */
		btnSearch = new JButton("Suchen");
		btnSearch.setBounds(645, 21, 89, 23);
		btnSearch.addActionListener(facade);
		btnSearch.setActionCommand(""Suchen);
		contentPane.add(btnSearch);
		
		separator = new JSeparator();
		separator.setForeground(Color.BLACK);
		separator.setBounds(120, 42, 610, 2);
		contentPane.add(separator);
		
		cmbChoice = new JComboBox<String>();
		cmbChoice.addItem("Einträge");
		cmbChoice.addItem("Projekte");
		cmbChoice.addItem("Mitarbeiter");
		cmbChoice.addItem("Tags");
		cmbChoice.setBounds(0, 20, 120, 20);
		cmbChoice.setActionCommand("Change_List");
		cmbChoice.addActionListener(facade);
		contentPane.add(cmbChoice);
		
		txtAreaTerm = new JTextArea();
		txtAreaTerm.setFont(new Font("Arial", Font.PLAIN, 12));
		txtAreaTerm.setEditable(false);
		txtAreaTerm.setLineWrap(true);
		txtAreaTerm.setVisible(true);
		txtAreaTerm.setBounds(122, 47, 612, 415);
		contentPane.add(txtAreaTerm);
		
		list = new JList<String>();
		list.setBounds(0, 41, 120, 421);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setVisible(true);
		contentPane.add(list);
		
		txbSearch = new JTextField();
		txbSearch.setColumns(10);
		txbSearch.setBounds(120, 20, 520, 20);
		contentPane.add(txbSearch);
	}
   /**
	 * Gives the selected terms category back. e.g. Projects, Tags etc.
	 * 
	 * @return Terms category
	 */
	public String getChoiceList(){
		return cmbChoice.getSelectedItem().toString();
	}
	/**
	 * Sets the Choosing list to show terms.
	 */
	public void setChoiceList(String item){
		cmbChoice.addItem(item);
		
		cmbChoice.repaint();
	}
	public void setList(List<String> items) {
		DefaultListModel<String> listModel = new DefaultListModel<String>();
		for(String item: items){
			listModel.addElement(item);
		}
		list = new JList<String>(listModel);
	}
   /**
	 * The Selected Item will be returned back.
	 * 
	 * @return Item
	 */
	public String getSelectedListItem() {
		return list.getSelectedValue();
	}
}

Der Ereignis empfänger.
Java:
public class Facade implements ActionListener, ItemListener {
private MainWindow mainWin;
	/** data model */
	private Work work;
	
	public Facade(Work works){
		this.work = work;
               mainWin = new MainWindow();
	}
	public Facade(){
		
	}
	@Override
	public void actionPerformed(ActionEvent ea) {
		/** Main Window */
		if(ea.getActionCommand().equals("Change_List")){
			if(mainWin.getChoiceList().equals("Projekte")){
				this.showProjects();
			}
			if(mainWin.getChoiceList().equals("Mitarbeiter")){
				this.showEmployee();
			}
			if(mainWin.getChoiceList().equals("Tags")){
				this.showTag();
			}
			if(mainWin.getChoiceList().equals("Einträge")){
				this.showTerms();
			}
		}
	}
	/** 
	 * Shows all Term names in the list in MainWindow
	 */
	public void showTerms(){
               List<String> listItems = new ArrayList<String>();
		for(Term term: glossary.getTerms()){
			listItems.add(term.getName());	
		}
             mainWin.setList(listItems);	
	}
       /** 
	 * Shows all Term names in the list in MainWindow
	 */
	public void showTag(){
               List<String> listItems = new ArrayList<String>();
		for(Tag tag: glossary.getTags()){
			listItems.add(tag.getName());	
		}
          mainWin.setList(listItems);	
	}
/** 
	 * Shows all Term names in the list in MainWindow
	 */
	public void showEmployee() {
               List<String> listItems = new ArrayList<String>();
		for(Employee employee: glossary.getEmployees()){
			listItems.add(employee.getName());	
		}
           mainWin.setList(listItems);	
	}
/** 
	 * Shows all Term names in the list in MainWindow
	 */
	public void showProjects() {
               List<String> listItems = new ArrayList<String>();
		for(Project project: glossary.getProjects()){
			mainWin.setList(project.getName());	
		}
          mainWin.setList(listItems);	
	}
	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub

	}
}
 
Zuletzt bearbeitet:
Frage: Wo kommt Command.CHANGE_TERM_SHOWLIST her und warum verwendest du diesen nicht auch in der Facade Klasse anstatt den String zu verwenden? Vielleicht ist ist der assoziierte Wert von Command.CHANGE_TERM_SHOWLIST eben nicht "Change_List".

Edit: Dein Konstrukt ist irgendwie merkwürdig. Dein MainWindow kennt ein Facade und dein Facade kennt ein MainWindow. Du solltest dir klar werden wer Parent ist. Aktuell würde ich sagen MainWindow ist Parent, aber auf welche Instanz greifst du bei beispielsweise mainWin.getChoiceList().equals("Projekte") zu?

Was ist glossary?
 
Zuletzt bearbeitet:
Die Command Klasse beinhaltet nur String zum werfen von Commandos.
Command.CHANGE_TERM_SHOWLIST entspricht "Change_List".

EDIT: Es wurde falsch übernommen. Ich hbs geändert.
 
Zuletzt bearbeitet:
Hi,
Hier die Lösung zum Problem:

JCheckbox Model
Java:
/**
 * Represents items in a JCheckbox-list that can be selected.
 * Used as a Model in MVC pattern. For Creating a JCheckbox list.
 * 
 * @author Padawan
 * @since 1.0.0
 * @version 1.0.0
 */
public class CheckListItemModel {
	
	private String  label;
	private boolean isSelected = false;

	/**
	 * Sets the text for CheckBox, which is shown on the GUI.
	 * 
	 * @param label of CheckBox
	 */
	public CheckListItem(String label){
		this.label = label;
	}
	/**
	 * Returns the selection mode
	 * @return true if the CheckBox is Selected
	 * 		   false if the CheckBox is not selected
	 */
	public boolean isSelected(){
		return isSelected;
	}
	/**
	 * Sets status of selection of a CheckBox.
	 * 
	 * @param isSelected
	 */
	public void setSelected(boolean isSelected){
		this.isSelected = isSelected;
	}
	/**
	 * Return label name or CheckBox.
	 */
	public String toString(){
		return label;
	}
}


Der Render:
Java:
import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 * Handles rendering cells in the list using a check box.
 * 
 * @author Padawan
 * @since 1.0.0
 * @version 1.0.0
 */
public class CheckListRenderer extends JCheckBox  implements ListCellRenderer{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus){
		setEnabled(list.isEnabled());
		setSelected(((CheckListItemModel)value).isSelected());
		setFont(list.getFont());
		setBackground(list.getBackground());
		setForeground(list.getForeground());
		setText(value.toString());
		
		return this;
	}
}

Die Oberfläche, auf der die JCheckBox List verwendet wird.
Java:
import javax.swing.JPanel;
import CheckListItemModel;
import CheckListRenderer;

/**
 * JFrame to use it in.
 * 
 * @author Padawan
 * @since 1.0.0
 * @version 1.0.0
 */
public class UseCheckBoxList extends JFrame {
	private JPanel contentPane;
	
	public CreateTerm(Glossary glossary) {
		scrollPane = new JScrollPane();
		scrollPane.setVisible(true);
		contentPane.add(scrollPane);
	}
	/**
	* Zum setzen der JCheckBoxen in der Jlist muss diese Funktion, mit den zu 
	* setzenden Daten Aufgerufen werden.
	*/
	public void setCheckboxList(List<Project> Items){
		 
//		JList<CheckListItemModel> list = new JList<CheckListItemModel>(new CheckListItemModel[]{ new CheckListItemModel("apple"),
//									new CheckListItemModel("orange"),
//									new CheckListItemModel("mango"),
//									new CheckListItemModel("paw paw"),
//									new CheckListItemModel("banana")});
		//Adds Some Items as CheckListItemModel
		Vector<CheckListItemModel> CheckListItemModels = new Vector<CheckListItemModel>();
		for(Project item: Items){
			CheckListItemModels.add(new CheckListItemModel(item.getName()));
		}
		
		JList list = new JList(CheckListItemModels);
		// Use a CheckListRenderer (see below)
		// to renderer list cells
		 
		list.setCellRenderer(new CheckListRenderer());
		//Selection mode: Mit der Aktuellen Mode, ist es möglich mehrere JChekboxen zu Wählen.
		list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		 
		// Add a mouse listener to handle changing selection
		 
		list.addMouseListener(new MouseAdapter(){ 
			public void mouseClicked(MouseEvent event) {
				JList list = (JList) event.getSource();
		
				// Get index of item clicked
				int index = list.locationToIndex(event.getPoint());
				CheckListItemModel item = (CheckListItemModel)
				list.getModel().getElementAt(index);
				 
				// Toggle selected state
				item.setSelected(! item.isSelected());
				 
				// Repaint cell
				list.repaint(list.getCellBounds(index, index));
			}
		});
		
		scrollPane.setViewportView(list);
		scrollPane.setBounds(75, 40, 250, 110);
		scrollPane.setVisible(true);
		scrollPane.repaint();
	}
}
 
Zurück