JCheckBox não funciona. [Resolvido]

3 respostas
gp7junior
package corretor;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;

public class Janela extends JPanel implements ActionListener{

	JLabel label1,label2;
	JButton openButton, cancelButton, generateButton;
	JFileChooser fc;
	JTextArea log;
	JCheckBox checkBox;
	Leitor arquivoLido;
	int arquivosGerados;
	
	public Janela(){
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		//Create a file chooser
        fc = new JFileChooser();        
        	
        //create a JCheck Box
        checkBox = new JCheckBox("Gerar relatório",null,false);

        
        //Create the open button.
        openButton = new JButton("Abrir o Arquivo");
        openButton.addActionListener(this);
        
        //Create the generate button
        generateButton = new JButton("Gerar o arquivo normalizado");
        generateButton.setEnabled(false);
        generateButton.addActionListener(this);
        
        //Create tool tips, for the heck of it.
		//label1.setToolTipText("Selecione o arquivo a ser usado");
		
		//Create the text areas
		log = new JTextArea(7,30);
		log.setMargin(new Insets(5,5,5,5));
		
		JScrollPane logScrollPane = new JScrollPane();
		logScrollPane.add(log);
        logScrollPane.setViewportView(log);
        logScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
 		logScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
 
		
		// set the text area1 and the log as not editable.
		log.setEditable(false);
		//ta1.setEditable(false);
		
		//Add the labels,text areas and buttons
		//For layout purposes, put the buttons in a separate panel
	
		JPanel buttonPanel1 = new JPanel();
		JPanel buttonPanel2 = new JPanel();	
		JPanel buttonPanel3 = new JPanel();	
		
		buttonPanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
		buttonPanel2.setLayout(new FlowLayout(FlowLayout.CENTER));
		buttonPanel3.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		buttonPanel1.add(openButton);
		buttonPanel1.add(generateButton);
		buttonPanel2.add(checkBox);
		buttonPanel3.add(logScrollPane);
		//buttonPanel3.setLayout(new BoxLayout(buttonPanel3,BoxLayout.X_AXIS));

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

        //Add the button panels    
        add(buttonPanel1,BorderLayout.PAGE_START);
        add(buttonPanel2,BorderLayout.CENTER);
        add(buttonPanel3,BorderLayout.CENTER);
	}
	
	public void actionPerformed(ActionEvent e) {

        //Handle open button action.
        //boolean checked = this.checkBox.isSelected();
        
        
        if (e.getSource() == openButton) {
            int returnVal = fc.showOpenDialog(Janela.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {                
            	File file = fc.getSelectedFile();
            	if (file.getName().endsWith(".txt")){
                    log.append(fc.getSelectedFile().getName()+" aberto com sucesso\n");
                    this.arquivoLido = new Leitor(file);
                    this.generateButton.setEnabled(true);
                    this.arquivosGerados++;
            	}
            	else{
            		log.append("IMPOSSIVEL ABRIR. Por favor selecione um arquivo txt\n");//This is where a real application would open the file.             
            	}
            }
        }else if(e.getSource() == generateButton){
        		if (this.arquivoLido != null){
        			Manipuladores manipulador = new Manipuladores();
        			Escritor escritor = new Escritor("midaret"+arquivosGerados+".txt");
        			if (this.checkBox.isSelected()){        				
        				Escritor report = new Escritor ("midaReport"+arquivosGerados+".txt");
        				manipulador.normalizarComRelatorio(arquivoLido, escritor, report);
        				log.append(escritor.fileName+" Gerado com sucesso\n");        		
        			}else{    				        				
        				manipulador.normalizar(arquivoLido, escritor);
        				log.append(escritor.fileName+" Gerado com sucesso\n");
        			}
        		}
        	  }
    }
        
	/**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
//    	Create and set up the window.
        JFrame frame = new JFrame("Normalizador");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
//      Create and set up the content pane.
        Janela newContentPane = new Janela();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);      

        //Display the window.
        frame.pack();
        frame.setVisible(true);
        Splash.getInstance().finish();//faz com que a Splash Screen suma ao iniciar o programa
        //frame.setBounds(300,300,700,700); 

    }
    
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
                
        //Faz o splash aparecer na tela
		Splash.getInstance().openSplash();
		
		for (int i = 0; i &lt 5000; i++);				
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        }
        );
    }
}

No código acima nao aparece nenhum erro, e no entanto quando selecionamos o checkBox o arquivo de relatorio nao é gerado.

o que pode estar acontecendo?

3 Respostas

emmanuel.silva

vc não está adicionando o listener ao check

checkbox.addActionListener(this);
emmanuel.silva

Comi bola… eu vi que vc está dando listener no botão… tente debugar o metodo actionPerformad para saber o que está acontecendo… qual das condições que está entrando…

gp7junior

Ok , vou tentar.

Criado 5 de abril de 2007
Ultima resposta 5 de abr. de 2007
Respostas 3
Participantes 2