Help form - RESOLVIDO

5 respostas
fabioebner

Pessoal estou com a seguinte duvida: Tenho um form principal com 5 botoes, ao clicar em um botao ele abre um form correspondente ao botao e desabilita todos os outros botoes. eu queria fazer o seguinte agora. qdo eu fechar esse form. ele me habilitasse todos os botoes do form principal. Como eu faco isso? e como se em javaScript eu utilizase o parent. entenderam??

tks

5 Respostas

Flasoft

Eu acho que é mais facil abrir os formularios como uma dialog, então abrir como “modal” sabe ficaria automatico…

Não seria necessario desbilitar os botões pois todo o formulario ficaria desabilitado enquando a dialog estivesse aberta…

(Nunca testei estender de uma dialog)

fabioebner

mas cara o problema e q para fazer um jdialog ele dica “dentro do meu JFrame” e vai ficar mto confuso… e nao tem como eu fazer um JFrame modal… entendeu.

E

ola! vc pode implementar sua classe com ‘InternalFrameListener’ para habilitar os botões. Segue um exemplo que pode ajudar.

import java.awt.<em>;

import java.awt.event.</em>;

import javax.swing.<em>;

import javax.swing.event.</em>;
public class TFrame extends JFrame implements InternalFrameListener, ActionListener,ItemListener {

JButton btn1, btn2, btn3;

JDesktopPane desktop;

JInternalFrame win1;

JInternalFrame win2;

JInternalFrame win3;

JCheckBox checkbox;

boolean recurso;
public TFrame() {
    super("Teste");
    desktop = new JDesktopPane();
    desktop.putClientProperty("JDesktopPane.dragMode","outline");
    setContentPane(desktop);
desktop.setLayout(null);
JPanel pane = new JPanel();
pane.setLayout(null);
btn1 = new JButton("Abrir Janela 1");
btn2 = new JButton("Abrir Janela 2");
btn3 = new JButton("Abrir Janela 3");
checkbox = new JCheckBox("Desabilitar botões quando clicar",true); 
recurso = true;
checkbox.addItemListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
pane.setBounds(0,0,200,500);
btn1.setBounds(20,20,150,22);
btn2.setBounds(20,47,150,22);
btn3.setBounds(20,74,150,22);
checkbox.setBounds(10,101,180,40);
pane.add(btn1);
pane.add(btn2);
pane.add(btn3);
pane.add(checkbox);
desktop.add(pane);
    }

public void internalFrameClosing(InternalFrameEvent e) {
    
}

public void internalFrameClosed(InternalFrameEvent e) {
    enableBtns(true);
}

public void internalFrameOpened(InternalFrameEvent e) {
   
}

public void internalFrameIconified(InternalFrameEvent e) {
    
}

public void internalFrameDeiconified(InternalFrameEvent e) {
    
}

public void internalFrameActivated(InternalFrameEvent e) {
    
}

public void internalFrameDeactivated(InternalFrameEvent e) {
    
}
private void enableBtns(Boolean v){
	btn1.setEnabled(v);
	btn2.setEnabled(v);
	btn3.setEnabled(v);
}

public void itemStateChanged(ItemEvent e) {
	
	if (e.getStateChange() == ItemEvent.DESELECTED)
		recurso = false;
	else	
		recurso = true;
        }


public void actionPerformed(ActionEvent e) {
if (recurso)
	enableBtns(false);

    if (e.getSource().equals(btn1)){
	btn1.setEnabled(false);
	win1 = new JInternalFrame("Janela 1", true, true, true, true); 
    	win1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	win1.addInternalFrameListener(this);
        win1.setSize(300, 100);
	win1.setLocation(200,0);
	desktop.add(win1);
	win1.show();
} else if(e.getSource().equals(btn2)){
	btn2.setEnabled(false);
	win2 = new JInternalFrame("Janela 2", true, true, true, true); 
    	win2.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	win2.addInternalFrameListener(this);
        win2.setSize(400, 150);
	win2.setLocation(200,100);
	desktop.add(win2);
	win2.show();
} else {
	btn3.setEnabled(false);		
	win3 = new JInternalFrame("Janela 3", true, true, true, true); 
    	win3.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	win3.addInternalFrameListener(this);
        win3.setSize(500, 200);
	win3.setLocation(200,300);
	desktop.add(win3);
	win3.show();
}

}

public static void main(String[] args) {
    JFrame frame = new TFrame();

    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    frame.setSize(800, 600);
    frame.setVisible(true);
}

}

fabioebner

cara gostei do internal nunca tinha usado… mas tem um problema, a minha aplicacao deve ter umas 20 telas, e se eu fizer tudo em internaframe vai ficar inviavel, ate mesmo pq quando vc utiliza um internarframe todas as acoes dos botoes estarao dentro do arquivo principal. e eu queria pode fazer cada tela em um arquivo separado

fabioebner

RESOLVI PESSOAL

fiz os seguinte:
no meu form Principal modifiquei os botoes para serem todos Statics criei os seguintes metodos statics no meu form principal

public static void setDesabilitaBotoes(){
        jButton1.setEnabled(false);
        jButton2.setEnabled(false);
        jButton3.setEnabled(false);
        jButton4.setEnabled(false);
        jButton5.setEnabled(false);
        jButton6.setEnabled(false);
    }
    
    public static void setHabilitaBotoes(){
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
        jButton3.setEnabled(true);
        jButton4.setEnabled(true);
        jButton5.setEnabled(true);
        jButton6.setEnabled(true);
    }
dai qdo eu fecho outro form eu chamei assim
PrincipalJF().setHabilitaBotoes();
PrincipalJF() e o nome da minha classe q tem o frame principal :D

obrigado

Criado 31 de maio de 2006
Ultima resposta 2 de jun. de 2006
Respostas 5
Participantes 3