Création d'un ChekboxGroup avec java

Fermé
minos11 Messages postés 8 Date d'inscription mercredi 16 juillet 2008 Statut Membre Dernière intervention 22 juillet 2008 - 21 juil. 2008 à 12:05
sandul Messages postés 3924 Date d'inscription jeudi 22 mai 2008 Statut Membre Dernière intervention 8 octobre 2010 - 22 juil. 2008 à 09:58
Bonjour,
quel est le code java permettant d'écrire la liste des choix dans un ChekboxGroup et d'afficher ce dernier sur mon interface?Aidez moi s'il vous plait.
Merci
A voir également:

2 réponses

sandul Messages postés 3924 Date d'inscription jeudi 22 mai 2008 Statut Membre Dernière intervention 8 octobre 2010 722
21 juil. 2008 à 15:18
Salut,

Pourquoi pas qqch du genre
		ButtonGroup bg = new ButtonGroup();
		bg.add(getConnectionAdminRB());
		bg.add(getConnectionHistRB());
		bg.add(getConnectionNormRB());


où les méthodes get retournent des JRadioButton? Ceci marche également pour des JCheckBox à la place des boutons radio (mais moins courant de les utiliser dans une interface si des choix à faire sont mutuellement exclusifs).

HTH,
++
0
minos11 Messages postés 8 Date d'inscription mercredi 16 juillet 2008 Statut Membre Dernière intervention 22 juillet 2008
22 juil. 2008 à 07:19
Bonjour,
merci pour votre aide,je vais essayer avec le code que vous m'avez indiqué
0
minos11 Messages postés 8 Date d'inscription mercredi 16 juillet 2008 Statut Membre Dernière intervention 22 juillet 2008
22 juil. 2008 à 07:38
Bonjour,
J'ai essayé le code que vous m'avez indiqué:
ButtonGroup bg = new ButtonGroup();
bg.add(getConnectionAdminRB());
bg.add(getConnectionHistRB());
bg.add(getConnectionNormRB());
mais il me donne cette erreur:can not resolve symbol method getConnectionAdminRB()
can not resolve symbol method getConnectionHistRB()
can not resolve symbol method getConnectionNormRB()
est ce qu'il y a une bibliothèque que je dois ajouter ou quoi?
Une autre question stp,comment je peux ecrire ma liste de choix et l'afficher dans le CheckBox?
Merci.
0
sandul Messages postés 3924 Date d'inscription jeudi 22 mai 2008 Statut Membre Dernière intervention 8 octobre 2010 722
22 juil. 2008 à 09:58
Les 3 méthodes get retournent un JRadioButton (ou une JCheckBox, si tu y tiens) ==> remplacer avec tes propres radio boutons ou checkbox.

Une autre question stp,comment je peux ecrire ma liste de choix et l'afficher dans le CheckBox? ==> là je ne comprends pas trop, est-ce que tu parles d'une ComboBox??

En tout cas, voici un petit exemple montrant l'utilisation des boutons radio, des checkboxes et d'une JComboBox, j'espère que cela sera plus clair:
package minos11.test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class MinosSample extends JFrame {
	private static final long serialVersionUID = 1L;
	private JComboBox comboBox;

	/**
	 * Launch the application
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			MinosSample frame = new MinosSample();
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame
	 */
	public MinosSample() {
		super();
		setBounds(100, 100, 500, 375);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final JPanel centerPane = new JPanel();
		final GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.rowHeights = new int[] { 0, 7, 7 };
		centerPane.setLayout(gridBagLayout);
		getContentPane().add(centerPane);

		final JRadioButton radioButton = new JRadioButton();
		radioButton.setText("New JRadioButton");
		final GridBagConstraints gridBagConstraints_4 = new GridBagConstraints();
		gridBagConstraints_4.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints_4.anchor = GridBagConstraints.WEST;
		centerPane.add(radioButton, gridBagConstraints_4);

		final JRadioButton radioButton_1 = new JRadioButton();
		radioButton_1.setText("New JRadioButton");
		final GridBagConstraints gridBagConstraints_5 = new GridBagConstraints();
		gridBagConstraints_5.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints_5.anchor = GridBagConstraints.WEST;
		centerPane.add(radioButton_1, gridBagConstraints_5);

		final JRadioButton radioButton_2 = new JRadioButton();
		radioButton_2.setText("New JRadioButton");
		final GridBagConstraints gridBagConstraints_6 = new GridBagConstraints();
		gridBagConstraints_6.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints_6.anchor = GridBagConstraints.WEST;
		centerPane.add(radioButton_2, gridBagConstraints_6);

		final JCheckBox checkBox = new JCheckBox();
		checkBox.setText("New JCheckBox");
		final GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.anchor = GridBagConstraints.WEST;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.gridx = 0;
		centerPane.add(checkBox, gridBagConstraints);

		final JCheckBox checkBox_1 = new JCheckBox();
		checkBox_1.setText("New JCheckBox");
		final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
		gridBagConstraints_1.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints_1.anchor = GridBagConstraints.WEST;
		gridBagConstraints_1.gridy = 1;
		gridBagConstraints_1.gridx = 1;
		centerPane.add(checkBox_1, gridBagConstraints_1);

		final JCheckBox checkBox_2 = new JCheckBox();
		checkBox_2.setText("New JCheckBox");
		final GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
		gridBagConstraints_2.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints_2.anchor = GridBagConstraints.WEST;
		gridBagConstraints_2.gridy = 1;
		gridBagConstraints_2.gridx = 2;
		centerPane.add(checkBox_2, gridBagConstraints_2);

		comboBox = new JComboBox();
		final GridBagConstraints gridBagConstraints_3 = new GridBagConstraints();
		gridBagConstraints_3.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints_3.anchor = GridBagConstraints.WEST;
		gridBagConstraints_3.gridy = 2;
		gridBagConstraints_3.gridx = 0;
		centerPane.add(comboBox, gridBagConstraints_3);

		final JPanel southPane = new JPanel();
		final FlowLayout flowLayout = new FlowLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		southPane.setLayout(flowLayout);
		getContentPane().add(southPane, BorderLayout.SOUTH);

		final JButton okButton = new JButton();
		okButton.setText("OK");
		southPane.add(okButton);

		// On rajoute les 3 radio buttons au 1er group, les trois checkboxes ou 2ème group et on remplit le combo
		ButtonGroup bg1 = new ButtonGroup();
		ButtonGroup bg2 = new ButtonGroup();
		bg1.add(radioButton);
		bg1.add(radioButton_1);
		bg1.add(radioButton_2);

		bg2.add(checkBox);
		bg2.add(checkBox_1);
		bg2.add(checkBox_2);

		DefaultComboBoxModel model = new DefaultComboBoxModel();
		model.addElement("1er élément");
		model.addElement("2ème élément");
		model.addElement("3ème élément");
		comboBox.setModel(model);
		//
	}
}


Ciao
0