renato.marquez 23 de jul. de 2011
Olá!
Veja se o código abaixo ajuda em alguma coisa.
import java.awt.Container ;
import java.awt.Dimension ;
import java.awt.EventQueue ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import java.util.Enumeration ;
import javax.swing.AbstractButton ;
import javax.swing.ButtonGroup ;
import javax.swing.JCheckBox ;
import javax.swing.JFileChooser ;
import javax.swing.JFrame ;
import javax.swing.JRadioButton ;
import javax.swing.JTextField ;
/**
*
* @author Renato
*/
public class MyForm extends JFrame {
public MyForm () {
setLayout ( null );
Container container = getContentPane ();
final JCheckBox jCheckBoxOpenFileChooser = new JCheckBox ( "Abrir JFileChooser" );
jCheckBoxOpenFileChooser . setBounds ( 15 , 20 , 180 , 18 );
jCheckBoxOpenFileChooser . addActionListener ( new ActionListener () {
@Override
public void actionPerformed ( ActionEvent e ) {
if ( jCheckBoxOpenFileChooser . isSelected ()) {
JFileChooser jFileChooser = new JFileChooser ();
if ( jFileChooser . showOpenDialog ( null ) == JFileChooser . APPROVE_OPTION ) {
//Alguma lógica aqui
}
}
}
});
final JTextField jTextFieldOther = new JTextField ();
jTextFieldOther . setBounds ( 105 , 112 , 120 , 25 );
jTextFieldOther . setVisible ( false );
final JRadioButton jRadioButtonOption1 = new JRadioButton ( "Opção 1" );
jRadioButtonOption1 . setBounds ( 15 , 53 , 80 , 18 );
final JRadioButton jRadioButtonOption2 = new JRadioButton ( "Opção 2" );
jRadioButtonOption2 . setBounds ( 15 , 83 , 80 , 18 );
final JRadioButton jRadioButtonOptionOther = new JRadioButton ( "Outro" );
jRadioButtonOptionOther . setBounds ( 15 , 113 , 60 , 18 );
final ButtonGroup buttonGroup = new ButtonGroup ();
buttonGroup . add ( jRadioButtonOption1 );
buttonGroup . add ( jRadioButtonOption2 );
buttonGroup . add ( jRadioButtonOptionOther );
Enumeration & lt ; AbstractButton & gt ; enumeration = buttonGroup . getElements ();
while ( enumeration . hasMoreElements ()) {
AbstractButton button = enumeration . nextElement ();
button . addActionListener ( new ActionListener () {
@Override
public void actionPerformed ( ActionEvent e ) {
boolean showTextField = jRadioButtonOptionOther . isSelected ();
jTextFieldOther . setVisible ( showTextField );
}
});
}
container . add ( jCheckBoxOpenFileChooser );
container . add ( jRadioButtonOption1 );
container . add ( jRadioButtonOption2 );
container . add ( jRadioButtonOptionOther );
container . add ( jTextFieldOther );
setSize ( new Dimension ( 300 , 200 ));
setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
}
public static void main ( String [] args ) {
EventQueue . invokeLater ( new Runnable () {
@Override
public void run () {
MyForm form = new MyForm ();
form . setVisible ( true );
}
});
}
}
Abraços!
ViniGodoy 24 de jul. de 2011
Movido para o fórum de interface gráfica. Por favor, leia com atenção a descrição dos fóruns antes de postar.