PDA

View Full Version : [Java] Bug JComboBox


k_mishima
11-06-2011, 22:08
Salve, ho un bug che proprio non riesco a capire. In questa Gui che ho creato, c'č un menu a tendina che ho chiamato comboBoxYear. Questo menu funziona, cliccando sulla freccettina, si apre e mostra le stringhe che ho definito per esso.
La gui comprende anche dei bottoni che permettono di scegliere dei file dal computer, non capisco perché se scelgo un file, il menu si bugga e non funziona pių, eppure i codici sono indipendenti, per quanto riguarda i listener

Spero voi sappiate che succede, sto impazzendo dietro questo bug


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.sql.SQLException;
import java.util.Observable;
import java.util.Observer;
import javax.swing.SwingWorker;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;

import javax.swing.JTextArea;
import javax.swing.SwingUtilities;




public class CreateGUI extends SwingWorker<Boolean, Void> implements Observer {

private final int contatoreProgressBar;
private final PopolamentoOntologia po = new PopolamentoOntologia();
private final JProgressBar progressBar;
private final JFrame frame = new JFrame("Popolamento Ontologia");

private String pathFashion;
private String pathOntology;
private String pathMondrian;
private JFileChooser fileChooser = null;
public JTextArea jTextArea = null;

private boolean statoOperazione = false;

private String[] chooseYear = {"anno", " 1997"," 2004"," 2005"," 2006"};
private JComboBox comboBoxYear;
private int annoSelezionato = 0;


public CreateGUI(int contatore) {

//Colore di background dalla GUI
Color bgColor = new Color(180,215,255);
Color buttonColor = new Color(238,240,242);

//ProgressBar programma
contatoreProgressBar = contatore;
this.progressBar = new JProgressBar();
this.progressBar.setValue(0); //Valore iniziale JProgressBar
this.progressBar.setStringPainted(true); //Numeri % sulla JProgressBar
this.progressBar.setMinimum(0);
this.progressBar.setMaximum(contatoreProgressBar);
this.progressBar.setBorder(BorderFactory.createMatteBorder(0, 0, 6, 6, bgColor));
this.progressBar.setBackground(buttonColor);


//Create a file chooser
fileChooser = new JFileChooser();

//Bottone popolaOntologia
final JButton buttonPopolaOntologia= new JButton("Popola Ontologia Vendite", new ImageIcon("image/iconBag.jpg"));
buttonPopolaOntologia.setEnabled(false);
buttonPopolaOntologia.setBackground(buttonColor);

//Pannello 1
Panel p1 = new Panel();
JLabel labelFashion = new JLabel("Percorso eFashion:");
p1.add(labelFashion);
final TextField textFieldFashion = new TextField("", 50);
p1.add(textFieldFashion);
JButton sfogliaFashion = new JButton("Sfoglia", new ImageIcon("image/iconAccess.jpg"));
sfogliaFashion.setBackground(buttonColor);
sfogliaFashion.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

int returnVal = fileChooser.showOpenDialog(frame);

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
setPathFashion(file.getPath());

textFieldFashion.setText(file.getPath());
if ((getPathOntology() != null) && (getPathMondrian() != null) && (annoSelezionato != 0)){
buttonPopolaOntologia.setEnabled(true);
}
}
}
});
p1.add(sfogliaFashion);

//Pannello 2
Panel p2 = new Panel();
JLabel labelMondrian = new JLabel("Percorso Mondrian:");
p2.add(labelMondrian);
final TextField textFieldMondrian = new TextField("", 50);
p2.add(textFieldMondrian);
JButton sfogliaMondrian = new JButton("Sfoglia", new ImageIcon("image/iconAccess.jpg"));
sfogliaMondrian.setBackground(buttonColor);
sfogliaMondrian.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg1) {
int returnVal = fileChooser.showOpenDialog(frame);

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
setPathMondrian(file.getPath());
textFieldMondrian.setText(file.getPath());
if ((getPathOntology() != null) && (getPathFashion() != null) && (annoSelezionato != 0)){
buttonPopolaOntologia.setEnabled(true);
}
}
}
});
p2.add(sfogliaMondrian);

//Pannello 3
Panel p3 = new Panel();
JLabel labelOntology = new JLabel("Percorso ontologia: ");
p3.add(labelOntology);
final TextField textFieldOntology = new TextField("", 50);
p3.add(textFieldOntology);
JButton sfogliaOntology = new JButton("Sfoglia", new ImageIcon("image/iconProtege.gif"));
sfogliaOntology.setBackground(buttonColor);
sfogliaOntology.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg2) {
int returnVal = fileChooser.showOpenDialog(frame);

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
setPathOntology(file.getPath());
textFieldOntology.setText(file.getPath());
if ((getPathFashion() != null) && (getPathMondrian() != null) && (annoSelezionato != 0)){
buttonPopolaOntologia.setEnabled(true);
}
}
}
});
p3.add(sfogliaOntology);

//Pannello 4
final Panel p4 = new Panel();
p4.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

//Riga 1
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 0;
c.gridx = 0;
p4.add(labelFashion, c);

c.gridx = 1;
p4.add(textFieldFashion, c);

c.gridx = 4;
p4.add(sfogliaFashion, c);

//Riga 2
c.gridy = 1;
c.gridx = 0;
p4.add(labelMondrian, c);

c.gridx = 1;
p4.add(textFieldMondrian, c);

c.gridx = 4;
p4.add(sfogliaMondrian, c);

//Riga 3
c.gridy = 2;
c.gridx = 0;
p4.add(labelOntology, c);

c.gridx = 1;
p4.add(textFieldOntology, c);

c.gridx = 4;
p4.add(sfogliaOntology, c);


final JLabel labelYear = new JLabel("Seleziona l'anno: ");

comboBoxYear = new JComboBox(chooseYear);
comboBoxYear.setOpaque(true);

comboBoxYear.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
int index = comboBoxYear.getSelectedIndex();
if (index == 0)
buttonPopolaOntologia.setEnabled(false);
else{
annoSelezionato = Integer.parseInt(chooseYear[index].trim());
}
}
});


//Pannello in alto
Panel inputPanel = new Panel();
inputPanel.add(p4);
inputPanel.add(labelYear);
inputPanel.add(comboBoxYear);
inputPanel.add(buttonPopolaOntologia);



final JLabel status = new JLabel("Stato: Pronto per l'esecuzione");
status.setFont(new Font("Georgia", Font.BOLD, 11));
status.setBorder(BorderFactory.createMatteBorder(6, 5, 5, 5, bgColor));
jTextArea = new JTextArea();
jTextArea.setFont(new Font("Serif", Font.ITALIC, 16));
jTextArea.setLineWrap(true);
jTextArea.setWrapStyleWord(true);
jTextArea.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, bgColor));
jTextArea.setEditable(false);

JScrollPane jScrollPane = new JScrollPane(jTextArea);
jScrollPane.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, bgColor));
jScrollPane.setPreferredSize(new Dimension(550, 150));

final JLabel log = new JLabel("Log");
log.setFont(new Font("Georgia", Font.BOLD, 11));
log.setBorder(BorderFactory.createMatteBorder(0, 5, 5, 5, bgColor));
DrawLine line = new DrawLine();
line.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 7, bgColor));

Panel logPanel = new Panel();
logPanel.setLayout(new BorderLayout());
logPanel.add(log, BorderLayout.WEST);
logPanel.add(line, BorderLayout.CENTER);

Panel centerPanel = new Panel();
centerPanel.setLayout(new BorderLayout());
centerPanel.add(logPanel, BorderLayout.NORTH);
centerPanel.add(jScrollPane, BorderLayout.CENTER);

Panel southPanel = new Panel();
southPanel.setLayout(new GridLayout(1,2));
southPanel.add(status);
southPanel.add(this.progressBar);
southPanel.setBackground(bgColor);

Panel p5 = new Panel();
p5.setLayout(new BorderLayout());
p5.add(centerPanel, BorderLayout.CENTER);
p5.add(southPanel, BorderLayout.SOUTH);


Container cp = this.frame.getContentPane();
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

cp.add(BorderLayout.CENTER, inputPanel);
cp.add(BorderLayout.SOUTH, p5);
cp.setBackground(bgColor);


buttonPopolaOntologia.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
status.setText("Stato: Esecuzione...");
buttonPopolaOntologia.setEnabled(false);
p4.setEnabled(false);
Thread t = new Thread() {
public void run() {
try {
statoOperazione = doInBackground();

} catch (Exception e) {
e.printStackTrace();
}

SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (statoOperazione)
status.setText("Stato: Caricamento completato");
else
status.setText("Stato: Operazione fallita");
// b.setEnabled(true);
}
});
}
};
t.start();
}
});
this.frame.setSize(700, 380);

//Centra la finestra nello schermo
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dim = toolkit.getScreenSize();
Rectangle abounds = frame.getBounds();
frame.setLocation((dim.width - abounds.width) / 2,
(dim.height - abounds.height) / 2);

this.frame.setVisible(true);
//this.frame.setResizable(false);
}

public String getPathFashion(){
return pathFashion;
}

public String getPathMondrian(){
return pathMondrian;
}

public String getPathOntology(){
return pathOntology;
}

public void setPathFashion(String path){
pathFashion = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + path + ";}";
}

public void setPathMondrian(String path){
pathMondrian = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + path + ";}";
}

public void setPathOntology(String path){
pathOntology = path;
}


@Override public Boolean doInBackground() {

this.po.addObserver(this);

try {
return this.po.ontologyPopolation(0, pathFashion, pathMondrian, pathOntology, annoSelezionato);
} catch (SQLException e) {
e.printStackTrace();
}
return true;

}

public void update(Observable o, Object arg) {
if (arg.toString().contains("++"))
this.jTextArea.append(arg.toString().substring(2));
else
this.progressBar.setValue(Integer.parseInt(arg.toString()));
}

}



import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;

import javax.swing.JComponent;


public class DrawLine extends JComponent {

private static final long serialVersionUID = 1L;

protected void paintComponent(Graphics g) {
Insets i = getInsets();
int w = getWidth();
int h = getHeight();
int x = i.left;
int y= i.top;
w -= i.left + i.right;
h -= i.top + i.bottom;

//Area x,y,w,h
g.setColor(new Color(238,240,242));
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(2f));
g2.drawLine(x, y + h/2, x + w, y + h/2);

}
}

Scusate per il codice lungo, ma non ho idea di dove derivi l'errore, meglio postarlo per intero.

PGI-Bis
12-06-2011, 17:19
Ad una rapida occhiata non mi sembra che la jcombobox sia "usata male". Io punterei su un problema di threading. Usa un debugger per vedere se il programma si blocca da qualche parte. Bisognerebbe comunque capire cosa succeda dopo aver scelto il file. Scompare il contenuto della jcomboxbox, il sistema di blocca, č rilasciata una eccezione o cos'altro.

k_mishima
13-06-2011, 01:14
Non credo c'entrino i thread, sto facendo varie prove eliminando tutti la parte sui thread e il problema resta, piuttosto se sposto il menu a sinistra non si bugga, credo sia un problema di layout incasinati, ma non riesco a uscirne, il menu si trova sotto le text area e la deve stare, a sinistra sta male.

PGI-Bis
13-06-2011, 01:23
non č che uno dei componenti awt che usi si sovrappone al menų a tendina? Prova a mettere come prima istruzione:

JPopupMenu.setDefaultLightWeightPopupEnabled(false);

k_mishima
13-06-2011, 02:07
non č che uno dei componenti awt che usi si sovrappone al menų a tendina? Prova a mettere come prima istruzione:

JPopupMenu.setDefaultLightWeightPopupEnabled(false);


Ma sei un grande! Funziona, ci sbattevo da 2 giorni, grazie 1000! :D

PGI-Bis
13-06-2011, 10:13
Se puoi non usare componenti awt (Panel, TextField) insieme a componenti Swing: usa solo quelli Swing (JPanel, JTextField).

k_mishima
16-06-2011, 16:41
Se puoi non usare componenti awt (Panel, TextField) insieme a componenti Swing: usa solo quelli Swing (JPanel, JTextField).

scusa il ritardo della risposta, cmq si, era quello il problema, usando solo elementi swing il bug non si presenta pių :D