Using an itemListener with JCheckBox to show/hide JTextField
Tag : java , By : nd27182
Date : March 29 2020, 07:55 AM
will be helpful for those in need I'm trying to create an application that allows a user to choose insurance options in JCheckBoxes. For each option that is selected, the name and price are supposed to appear in a text field. My problem is that even when I select it, it doesn't display the Name & Price. At the moment I'm just trying to make the HMO checkbox work. , Add the following code in your if block and it will work as expected hmoSelection.getParent().revalidate();
|
How to Add JTextField & JCheckBox to ArrayList Object
Tag : java , By : Pradeep Gowda
Date : March 29 2020, 07:55 AM
seems to work fine Use your addPlantBtn ActionListener to gather the information you need when it's called addPlantBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Flower flower = new Flower(nameField.getText(), idField.getText(), colorField.getText(), thornsBox.isSelected(), smellBox.isSelected());
plantList.add(flower);
public class FlowerPane extends JPanel {
JTextField nameField = new JTextField(15);
JTextField idField = new JTextField(10);
JTextField colorField = new JTextField(10);
JCheckBox smellBox = new JCheckBox("Smell Present");
JCheckBox thornsBox = new JCheckBox("Thorns Present");
public FlowerPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(2, 2, 2, 2);
JLabel nameLabel = new JLabel("Name:");
JLabel idLabel = new JLabel("ID:");
JLabel colorLabel = new JLabel("Color:");
add(nameLabel, gbc);
gbc.gridy++;
add(idLabel, gbc);
gbc.gridy++;
add(idLabel, gbc);
gbc.gridx++;
gbc.gridy = 0;
add(nameField, gbc);
gbc.gridy++;
add(idField, gbc);
gbc.gridy++;
add(colorField, gbc);
gbc.gridx = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(thornsBox, gbc);
gbc.gridy++;
add(smellBox, gbc);
}
public Flower create() {
return new Flower(nameField.getText(), idField.getText(), colorField.getText(), thornsBox.isSelected(), smellBox.isSelected());
}
}
FlowerPane flowerPane = new FlowerPane();
switch (JOptionPane.showConfirmDialog(null, flowerPane, "Flower", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
case JOptionPane.OK_OPTION:
Flower flower = flowerPane.create();
plantList.add(flower);
break;
}
|
How can I enable/disable a JTextField with a JCheckBox? or what's wrong with my code?
Tag : java , By : Longchao Dong
Date : March 29 2020, 07:55 AM
Hope that helps I'm newbie in programming java, I have an array of JCheckBox next to an array of JTextfield. , Here is a quick solution with an ItemListener. private void setupEvents() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i=0; i<10; i++) {
final int finalI = i;
cb[i].addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
text[finalI].setEnabled(e.getStateChange() == ItemEvent.SELECTED);
}
});
}
}
private void setupEvents() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i=0; i<10; i++) {
final int finalI = i;
cb[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
text[finalI].setEnabled(!text[finalI].isEnabled() && e.getID() == ActionEvent.ACTION_PERFORMED);
}
});
}
}
|
How can I enable/disable my JTextField depending of the state of a JCheckBox?
Date : March 29 2020, 07:55 AM
|
Search an arraylist by the contents of a JTextField - arraylist.contains(jtextfield)
Date : March 29 2020, 07:55 AM
|