我正在尝试将JComboBox放在Jpanel上,如果我单击显示组合框的内容,但它没有显示。只有当我将光标移到其上时,才会显示内容。 Jpanel包含一个带有图片的Jlabel,顶部有一个Jcombobox。
PanelRegionlACTEL:
package com.springJPA.vue.appACTEL;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jd.swing.custom.component.button.StandardButton;
import com.jd.swing.custom.component.jcombobox.MyComboBoxEditor;
import com.jd.swing.custom.component.jcombobox.MyComboBoxRenderer;
import com.jd.swing.util.Theme;
import com.springJPA.domain.Numero;
import com.springJPA.domain.Region;
import com.springJPA.domain.Resedence;
import com.springJPA.service.ActelsService;
import com.springJPA.service.FonctionDAO;
import com.springJPA.service.NumeroService;
import com.springJPA.service.RegionService;
import com.springJPA.service.ResedenceService;
import com.springJPA.service.VilleService;
import com.springJPA.vue.App;
public class PanelRegionlACTEL extends JPanel {
/**
*
*/
private static final long serialVersionUID = -3805099295102746248L;
public static JComboBox<String> comboBox_gouver;
public static JComboBox<String> comboBox_ACTELS;
public static JComboBox<String> comboBoxVille;
private JButton okButton;
private JButton cancelButton;
private JProgressBar progressBar;
int value = 0;
int nbp = 0;
public int selectedIndexR;
public int selectedIndexA;
public int selectedIndexV;
public JLabel labelR1;
public boolean tr=false;
FonctionDAO fonctionDAO = new FonctionDAO();
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
RegionService rgService = (RegionService) context.getBean("regionService");
ActelsService atlService = (ActelsService) context.getBean("actelService");
VilleService villeService = (VilleService) context.getBean("villeService");
NumeroService numeroService = (NumeroService) context.getBean("numeroService");
ResedenceService resedenceService = (ResedenceService) context.getBean("resedenceService");
/*public static void main(String[] args)
{
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (Exception e) {
System.out.println("Erreur LookAndFeel"+e);
}
try{
new PanelAccueilACTEL();
}catch (Exception e) {
System.out.println("Erreur App: "+e);
e.printStackTrace();
}
}*/
/**
* Create the panel.
*/
public PanelRegionlACTEL() {
setLayout(null);
setBorder(null);
setOpaque(false);
setBounds(0, 0, 803, 534);
setBackground(new Color(0,0,0,0));
setSize(803, 534);
setVisible(true);
JButton buttonQuitter = new JButton("");
buttonQuitter.setIcon(new ImageIcon(PanelRegionlACTEL.class.getResource("/ap/s/tn/img/btn.PNG")));
buttonQuitter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
buttonQuitter.setIcon(new ImageIcon(PanelLoginACTEL.class.getResource("/ap/s/tn/img/btn.PNG")));
buttonQuitter.setOpaque(true);
buttonQuitter.setSelectedIcon(new ImageIcon(App.class.getResource("/ap/s/tn/img/btn2.PNG")));
buttonQuitter.setFocusPainted(false);
buttonQuitter.setContentAreaFilled(false);
buttonQuitter.setBorderPainted(false);
buttonQuitter.setBounds(618, 420, 144, 41);
add(buttonQuitter);
JMenuReseau();
JMenuItemRegion();
JMenuDemandes();
JRadioButtonMenuItemNinstallation();
JRadioButtonMenuItemTransfaire();
JLabel lblLoginApplicationFta = new JLabel("Region application ACTEL");
lblLoginApplicationFta.setFont(new Font("Segoe Print", Font.BOLD, 16));
lblLoginApplicationFta.setForeground(new Color(255, 0, 0));
lblLoginApplicationFta.setBounds(6, 6, 229, 28);
add(lblLoginApplicationFta);
JLabel lblslectionnerUnGouvernorat = new JLabel("<html><font color='#09024D'>S\u00E9lectionner un gouvernorat</font></html>");
lblslectionnerUnGouvernorat.setFont(new Font("SansSerif", Font.PLAIN, 18));
lblslectionnerUnGouvernorat.setBounds(54, 107, 242, 25);
add(lblslectionnerUnGouvernorat);
comboBox_gouver = new JComboBox<String>();
comboBox_gouver.setModel(new DefaultComboBoxModel<String>(new String[] {"---------------------"}));
afficherLISTE_REGION();
comboBox_gouver.setBounds(323, 106, 118, 26);
comboBox_gouver.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedIndexR = comboBox_gouver.getSelectedIndex();
selectedIndexA = comboBox_ACTELS.getSelectedIndex();
if(selectedIndexR == 0) {
comboBox_ACTELS.setModel(new DefaultComboBoxModel<String> (new String[] {"---------------------"}));
comboBoxVille.setModel(new DefaultComboBoxModel<String>(new String[] {"---------------------"}));
}
else{
afficherLISTE_ACTEL_PAR_REGION();
}
}
});
add(comboBox_gouver);
comboBox_ACTELS = new JComboBox<String>();
//comboBox_ACTELS.setRenderer(new MyComboBoxRenderer());
//comboBox_ACTELS.setEditor(new MyComboBoxEditor());
comboBox_ACTELS.setModel(new DefaultComboBoxModel<String>(new String[] {"---------------------"}));
comboBox_ACTELS.setBounds(323, 134, 118, 26);
comboBox_ACTELS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e2) {
int indexA = comboBox_ACTELS.getSelectedIndex();
if(indexA == 0){
comboBoxVille.setModel(new DefaultComboBoxModel<String>(new String[] {"---------------------"}));
}
else{
afficherLISTE_VILLE_PAR_ACTEL();
}
}
});
JLabel lblchoixActels = new JLabel("<html><font color='#09024D'>Choix ACTELS</font></html>");
lblchoixActels.setFont(new Font("SansSerif", Font.PLAIN, 18));
lblchoixActels.setBounds(54, 139, 133, 16);
add(lblchoixActels);
add(comboBox_ACTELS);
JLabel lblville = new JLabel("<html><font color='#09024D'>Ville</font></html>");
lblville.setFont(new Font("SansSerif", Font.PLAIN, 18));
lblville.setBounds(54, 170, 42, 16);
add(lblville);
comboBoxVille = new JComboBox<String>();
comboBoxVille.setBounds(323, 165, 118, 26);
comboBoxVille.setModel(new DefaultComboBoxModel<String>(new String[] {"---------------------"}));
add(comboBoxVille);
progressBar = new JProgressBar();
progressBar.setBounds(54, 198, 154, 19);
progressBar.setVisible(false);
add(progressBar);
StandardButton button = new StandardButton("OK", Theme.GLOSSY_BLUE_THEME);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
App.cl_conteneurPaneau.show(App.conteneurPaneau, "panelAccueilACTEL");
int indexRegion = comboBox_gouver.getSelectedIndex();
int indexActel = comboBox_ACTELS.getSelectedIndex();
int indexVille = comboBoxVille.getSelectedIndex();
String region = comboBox_gouver.getSelectedItem().toString();
String ville = comboBoxVille.getSelectedItem().toString();
if((indexRegion==0)&&(indexActel==0)&&(indexVille==0)){
labelR1.setText("selectioner tout champs");
}
else if((indexActel==0)&&(indexVille==0)){
labelR1.setText("selectioner tout champs");
}
else{
action();
labelR1.setForeground(new Color(0, 128, 0));
labelR1.setText("region : "+region);
region = comboBox_gouver.getSelectedItem().toString();
ville = comboBoxVille.getSelectedItem().toString();
PanelAccueilACTEL.getLabelR().setText("<html><body><b>Region: "+region+"</b></body></html>");
PanelAccueilACTEL.getLabelV().setText("<html><body><b>*Ville: "+ville+"</b></body></html>");
remplir_code_postal();
remplir_numero();
remplir_type_voie();
}
}
});
button.setActionCommand("OK");
button.setBounds(54, 250, 80, 28);
add(button);
ImageIcon icon = new ImageIcon(getClass().getResource("/tn/pack/image/cadre-rg.png"));
Image zoom = scaleImage(icon.getImage(), 934);
Icon iconScaled = new ImageIcon(zoom);
labelR1 = new JLabel("");
labelR1.setBounds(250, 258, 133, 14);
add(labelR1);
JLabel cadre = new JLabel(iconScaled);
cadre.setIcon(new ImageIcon(PanelLoginACTEL.class.getResource("/tn/pack/image/cadre-rg.png")));
cadre.setBounds(6, 70, 486, 305);
add(cadre);
JLabel arrirePlan = new JLabel("");
arrirePlan.setIcon(new ImageIcon(PanelRegionlACTEL.class.getResource("/ap/s/tn/img/bot.png")));
arrirePlan.setBounds(0, 0, 803, 534);
add(arrirePlan);
}
public void remplir_type_voie() {
List<Resedence> listeTypeVoie = fonctionDAO.afficherLISTE_TYPE_VOIE(resedenceService);
for(Resedence resedence:listeTypeVoie) {
PanelNouvelleInstallation.comboBox_typeVoie.addItem(resedence.getType_voie());
PanelDemandeTransfertACTEL.comboBox_typeVoieA.addItem(resedence.getType_voie());
PanelDemandeTransfertACTEL.comboBox_typeVoieN.addItem(resedence.getType_voie());
}
}
public void remplir_code_postal() {
PanelNouvelleInstallation.textField_codepostal.setText(getcodePostal()+"");
PanelDemandeTransfertACTEL.textField_codepostalA.setText(getcodePostal()+"");
PanelDemandeTransfertACTEL.textField_codepostalN.setText(getcodePostal()+"");
}
protected void afficherLISTE_VILLE_PAR_ACTEL() {
try {
comboBoxVille.removeAllItems();
String nom_actel = comboBox_ACTELS.getSelectedItem().toString();
int id_actel = fonctionDAO.afficherID_ACTEL_PAR_NOM_ACTEL(atlService, nom_actel);
List<String> listeVille= fonctionDAO.afficherLISTE_VILLE_PAR_ACTEL(villeService, id_actel);
for(String lv:listeVille) {
comboBoxVille.addItem(lv);
}
} catch (Exception e1) {
System.out.println("Erreur SELECT ville");
}
}
protected void afficherLISTE_ACTEL_PAR_REGION() {
comboBox_ACTELS.setModel(new DefaultComboBoxModel<String> (new String[] {"---------------------"}));
comboBoxVille.setModel(new DefaultComboBoxModel<String>(new String[] {"---------------------"}));
List<String> listeActel= fonctionDAO.afficherLISTE_ACTEL_PAR_REGION(atlService, comboBox_gouver.getSelectedIndex()+49);
for(String la:listeActel) {
comboBox_ACTELS.addItem(la);
}
}
private void afficherLISTE_REGION() {
List<Region> listeRegion= fonctionDAO.afficherLISTE_REGION(rgService);
for(Region lr:listeRegion) {
comboBox_gouver.addItem(lr.getNom_region());
}
}
private void JRadioButtonMenuItemTransfaire() {
}
private void JRadioButtonMenuItemNinstallation() {
}
private void JMenuDemandes() {
}
private void JMenuItemRegion() {
}
private void JMenuReseau() {
}
void action() {
new Thread(new Runnable() {
public void run() {
try {
progressBar.setValue(value);
value++;
Thread.sleep(15);
}
catch (InterruptedException ex) {ex.printStackTrace();}
if (progressBar.getPercentComplete() == 1.0) {
tr=true;
}
else { tr=false;
action();}
}
}).start();
}
public int remplir_numero() {
String ville = comboBoxVille.getSelectedItem().toString();
Listnumero.listModel.removeAllElements();
PanelAffectationNumero.comboBox_choixN.removeAllItems();
PanelAffectationNumero.comboBox_choixN.setEnabled(false);
List<Numero> listeNum = fonctionDAO.afficherLISTE_NUMERO(numeroService, fonctionDAO.afficherID_VILLE_PAR_NOM_VILLE(villeService, ville));
if(listeNum!=null){
for(Numero ln:listeNum) {
Listnumero.listModel.addElement(ln.getPlage_numero()+"");
}
return 1;
}
else{
return 0;
}
}
/* public int remplir_resedence(){
PanelNouvelleInstallation.comboBoxResedence.setModel(new DefaultComboBoxModel<String>(new String[] {"-----------"}));
String nom_ville = comboBoxVille.getSelectedItem().toString();
List<Ville> listeVille = fonctionDAO.afficherRESEDENCE_PAR_NOM_VILLE(villeService, nom_ville);
if(listeVille!=null){
for(Ville lv:listeVille) {
PanelNouvelleInstallation.comboBoxResedence.addItem(lv.getResedence());
}
return 1;
}
else{
return 0;
}
}*/
/* public void remplir_voie(){
int indextvoie= PanelNouvelleInstallation.comboBoxTypeVoie.getSelectedIndex();
String type_voie = PanelNouvelleInstallation.comboBoxTypeVoie.getSelectedItem().toString();
String nom_ville = PanelRegionlACTEL.comboBoxVille.getSelectedItem().toString();
if(indextvoie == 0){
PanelNouvelleInstallation.comboBoxVoie.setModel(new DefaultComboBoxModel<String>(new String[] {"----------------"}));
}
else{
PanelNouvelleInstallation.comboBoxVoie.setModel(new DefaultComboBoxModel<String>(new String[] {"----------------"}));
List<Ville> listeVille = fonctionDAO.afficherVOIE_PAR_NOM_VILLE(villeService, nom_ville, type_voie);
if(listeVille!=null){
for(Ville lv: listeVille){
PanelNouvelleInstallation.comboBoxVoie.addItem(lv.getVoie());
}
}
}
}*/
public int getcodePostal() {
String nom_actel = comboBox_ACTELS.getSelectedItem().toString();
String nom_ville = comboBoxVille.getSelectedItem().toString();
return fonctionDAO.afficherCODEP_PAR_NOM_VILLE(villeService, fonctionDAO.afficherNUM_ACTEL_PAR_NOM_ACTEL(atlService, nom_actel)+50, nom_ville);
}
public JButton getOkButton() {
return okButton;
}
public void setOkButton(JButton okButton) {
this.okButton = okButton;
}
public JButton getCancelButton() {
return cancelButton;
}
public void setCancelButton(JButton cancelButton) {
this.cancelButton = cancelButton;
}
public static Image scaleImage(Image source, int width, int height) {
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) img.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(source, 0, 0, width, height, null);
g.dispose();
return img;
}
//avec une taille en pixels (=hauteur si portrait, largeur si paysage):
public static Image scaleImage(Image source, int size) {
int width = source.getWidth(null);
int height = source.getHeight(null);
double f = 0;
if (width < height) {//portrait
f = (double) height / (double) width;
width = (int) (size / f);
height = size;
} else {//paysage
f = (double) width / (double) height;
width = size;
height = (int) (size / f);
}
return scaleImage(source, width, height);
}
}