K
K
I made a java program and can't figure out why my uneditable text can still be edited. I have 2 classes in this program here's the secondary class:
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class GUI_two extends JFrame{
private JTextField item2;
private JTextField item3;
private JTextField item4;
private JPasswordField itempass;
ImageIcon image = new ImageIcon("star.png");
public GUI_two(){
super("Kale");
setLayout(new FlowLayout());
item2 = new JTextField(10);
add(item2);
item3 = new JTextField("Enter text here");
item3.setEditable(false);
add(item3);
item4 = new JTextField("Can't edit", 20);
item3.setEditable(false);
add(item4);
itempass = new JPasswordField ("Password");
add(itempass);
reg = new JButton("Button");
add(reg);
Icon b = new ImageIcon(getClass().getResource("Button 2.png"));
Icon a = new ImageIcon(getClass().getResource("Button.png"));
custom = new JButton("Custom", a);
custom.setRolloverIcon(b);
add(custom);
thehandler handler = new thehandler();
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
itempass.addActionListener(handler);
reg.addActionListener(handler);
custom.addActionListener(handler);
}
private JButton reg;
private JButton custom;
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource()==item2){
string=String.format("field 2: %s", event.getActionCommand());}
else if (event.getSource()==item3)
string=String.format("field 3: %s", event.getActionCommand());
else if (event.getSource()==item4)
string=String.format("field 4: %s", event.getActionCommand());
else if (event.getSource()==itempass)
string=String.format("itempass is : %s", event.getActionCommand());
//JOptionPane.showMessageDialog(null, String.format("%s, event.getActionCommand()"));
JOptionPane.showMessageDialog(null, string);
}
}
}
here's the main class:
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class Kale_GUI {
public static void main(String[] args) {
String fn = JOptionPane.showInputDialog("Enter first number");
String sn = JOptionPane.showInputDialog("Enter second number");
int num1 = Integer.parseInt(fn) ;
int num2 = Integer.parseInt(sn) ;
int sum = num1 + num2;
JOptionPane.showMessageDialog(null, "The answer is "+sum, "The title", JOptionPane.PLAIN_MESSAGE);
GUI_two kale = new GUI_two();
kale.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
kale.setSize(500,500);
kale.setVisible(true);
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class GUI_two extends JFrame{
private JTextField item2;
private JTextField item3;
private JTextField item4;
private JPasswordField itempass;
ImageIcon image = new ImageIcon("star.png");
public GUI_two(){
super("Kale");
setLayout(new FlowLayout());
item2 = new JTextField(10);
add(item2);
item3 = new JTextField("Enter text here");
item3.setEditable(false);
add(item3);
item4 = new JTextField("Can't edit", 20);
item3.setEditable(false);
add(item4);
itempass = new JPasswordField ("Password");
add(itempass);
reg = new JButton("Button");
add(reg);
Icon b = new ImageIcon(getClass().getResource("Button 2.png"));
Icon a = new ImageIcon(getClass().getResource("Button.png"));
custom = new JButton("Custom", a);
custom.setRolloverIcon(b);
add(custom);
thehandler handler = new thehandler();
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
itempass.addActionListener(handler);
reg.addActionListener(handler);
custom.addActionListener(handler);
}
private JButton reg;
private JButton custom;
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource()==item2){
string=String.format("field 2: %s", event.getActionCommand());}
else if (event.getSource()==item3)
string=String.format("field 3: %s", event.getActionCommand());
else if (event.getSource()==item4)
string=String.format("field 4: %s", event.getActionCommand());
else if (event.getSource()==itempass)
string=String.format("itempass is : %s", event.getActionCommand());
//JOptionPane.showMessageDialog(null, String.format("%s, event.getActionCommand()"));
JOptionPane.showMessageDialog(null, string);
}
}
}
here's the main class:
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class Kale_GUI {
public static void main(String[] args) {
String fn = JOptionPane.showInputDialog("Enter first number");
String sn = JOptionPane.showInputDialog("Enter second number");
int num1 = Integer.parseInt(fn) ;
int num2 = Integer.parseInt(sn) ;
int sum = num1 + num2;
JOptionPane.showMessageDialog(null, "The answer is "+sum, "The title", JOptionPane.PLAIN_MESSAGE);
GUI_two kale = new GUI_two();
kale.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
kale.setSize(500,500);
kale.setVisible(true);
}
}