Skip to content

Instantly share code, notes, and snippets.

@hdvianna
Last active March 5, 2021 23:05
Show Gist options
  • Save hdvianna/a5b28dad2c0fd6d550bb9614bbbd72e7 to your computer and use it in GitHub Desktop.
Save hdvianna/a5b28dad2c0fd6d550bb9614bbbd72e7 to your computer and use it in GitHub Desktop.
JRadioButton
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class JRadioButtonExample extends JFrame {
private JRadioButton botao1RadioButton;
private JRadioButton botao2RadioButton;
private JPanel rootContainer;
private JTextField exampleTextField;
public JRadioButtonExample() {
super("JRadioButtonExample");
setContentPane(rootContainer);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exampleTextField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent documentEvent) {
System.out.println("Inseriu texto");
}
@Override
public void removeUpdate(DocumentEvent documentEvent) {
System.out.println("Removeu texto");
}
@Override
public void changedUpdate(DocumentEvent documentEvent) {
System.out.println("Modificou o texto");
}
});
ItemListener itemListener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent itemEvent) {
if (itemEvent.getStateChange() == ItemEvent.SELECTED) {
if (botao1RadioButton.isSelected()) {
System.out.println("botao1RadioButton foi selecionado");
}
if (botao2RadioButton.isSelected()) {
System.out.println("botao2RadioButton foi selecionado");
}
}
}
};
botao1RadioButton.addItemListener(itemListener);
botao2RadioButton.addItemListener(itemListener);
pack();
}
public void showMe() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
setVisible(true);
}
});
}
public static void main(String[] args) {
(new JRadioButtonExample()).showMe();
}
}
private double celsiusToFahrenheit(long temperature) {
return (temperature * 9/5) + 32;
}
private double fahrenheitToCelsius(long temperature) {
return (temperature - 32) * 5/9;
}
Integer.parseInt(text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment