Remplir un jlabel d'une a partir d'un jcombobox d'une autre classe
cs_croire
Messages postés20Date d'inscriptionlundi 2 août 2010StatutMembreDernière intervention22 mai 2012
-
22 mai 2012 à 15:24
Utilisateur anonyme -
29 mai 2012 à 12:51
bonjour a tous
je vous fournis mon code des deux classes afin de pouvoir m'aider
je veux récupérer l'item selectionné de mon combobox de la class "combo" sur le label recup_item de ma classe "label"
j'ai pu l'afficher sur la console mais pas sur la classe label
voici le code
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* combo.java
*
* Created on 21 mai 2012, 22:58:20
*/
/**
*
* @author Microbox
*/
/*--------------------------------------------------------------------------------------------------------------*/
public class combo extends JFrame implements ActionListener {
/**
*
*/
JLabel recup_item;
/** Creates new form combo */
public combo() {
initComponents();
this.setLocationRelativeTo(null);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jComboBox1 = new javax.swing.JComboBox();
btn_ok = new javax.swing.JButton();
btn_ok.setText("ok");
/*--------------------------------------------------------------------------------------------------------------*/
btn_ok.addActionListener(new label(this));
/*--------------------------------------------------------------------------------------------------------------*/
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "rouge", "vert", "bleu" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
/*--------------------------------------------------------------------------------------------------------------*/
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
/*--------------------------------------------------------------------------------------------------------------*/
btn_ok.setText("ok");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(89, 89, 89)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btn_ok)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(63, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(71, 71, 71)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 107, Short.MAX_VALUE)
.addComponent(btn_ok)
.addGap(51, 51, 51))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
//public void itemStateChanged(ItemEvent ie) {
recup_item.setText(jComboBox1.getSelectedItem().toString());
//}
// TODO add your handling code here:
}
/*--------------------------------------------------------------------------------------------------------------*/
private void btn_okActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//new label().setVisible(true);
}
/*--------------------------------------------------------------------------------------------------------------*/
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new combo().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JButton btn_ok;
public javax.swing.JComboBox jComboBox1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
/*--------------------------------------------------------------------------------------------------------------*/
public JComboBox getjComboBox1(){
//JLabel recup_item;
return jComboBox1;
}
/*--------------------------------------------------------------------------------------------------------------*/
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
---------------------------LE CODE DE LA CLASS LABEL-----------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import javax.swing.JComboBox;
import javax.swing.JLabel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* label.java
*
* Created on 21 mai 2012, 23:02:10
*/
/**
*
* @author Microbox
*/
/*--------------------------------------------------------------------------------------------------------------*/
public class label extends javax.swing.JFrame implements ActionListener {
private combo combo1;
public label(combo combo1) {
this.combo1=combo1;
}
/** Creates new form label */
public label() {
initComponents();
this.setLocationRelativeTo(null);
//pour ne pas fermer la fenetre précedente aprés close de label//
setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
//public void actionPerformed1(ActionEvent arg0) {
jPanel1 = new javax.swing.JPanel();
couleur = new javax.swing.JLabel();
recup_item = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
couleur.setText("couleur choisi");
recup_item.setForeground(new java.awt.Color(255, 51, 0));
recup_item.setOpaque(true);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(couleur, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(recup_item, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(45, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(64, 64, 64)
.addComponent(recup_item, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(77, 77, 77)
.addComponent(couleur)))
.addContainerGap(151, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new label().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JLabel couleur;
private javax.swing.JPanel jPanel1;
public javax.swing.JLabel recup_item;
// End of variables declaration
@Override
/*--------------------------------------------------------------------------------------------------------------*/
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//recup_item.setText(combo1.jComboBox1.getSelectedItem().toString());
String jComboBox1=combo1.getjComboBox1().getSelectedItem().toString();
// recup_item.setText("jComboBox1");
System.out.println(jComboBox1);
new label().setVisible(true);
}
/*--------------------------------------------------------------------------------------------------------------*/
}
j'éspére que vous allez m'aidé
merci d'avance
A voir également:
Remplir un jlabel d'une a partir d'un jcombobox d'une autre classe