I've got a JDialog with a textField and a button. If I press the button and the textField is empty, it prints an error message. But now I want to open a new Window (JDialog I guess) with the information of the textField.
{
JButton aceptarButton = new JButton("Aceptar");
aceptarButton.setBounds(332, 387, 86, 23);
contentPanel.add(aceptarButton);
aceptarButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
aceptarButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ArticuloDAO dao = new ArticuloDAO();
Articulo a = new Articulo();
if (nombreField.getText().equals("")) {
System.out.println("Nombre");
}else{
String nombre = nombreField.getText();
a.setNombre(nombre);
dao.insert(a);
Success s = new Success();
s.setVisible(true);
setVisible(false);
}
}
});
aceptarButton.setActionCommand("OK");
getRootPane().setDefaultButton(aceptarButton);
}
A new window saying "Name is missing" should open whenever I click OK and the textField nombreField is empty. How is it?
You can simply use JOptionPane for this. Just call it like so:
JOptionPane.showMessageDialog(this, "Name is missing");
It will display the message in a dialog box and wait for OK.
To show a popup messages from any kind you can use the class JOptionPane and their static methods. in your case JOptionPane.showMessageDialog(null, "Name is missing"); should do the thing.
Related
what do I have to add to the code below so that the user has to enter a specific word i.e. "London" to open the JOptionPane input dialog box.
JFrame frame = new JFrame("JTextField");
JTextField textfield = new JTextField(30);
frame.add(textfield);
At the moment I can type in anything in the text field and the dialog box will appear. I only want it to open if the user enters a specific word.
I'm using the action event with action listener and action performed to open the JOptionPane Dialog box.
public class Test9 {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextField");
JTextField textfield = new JTextField(30);
frame.add(textfield);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,200);
JPanel panel = new JPanel();
frame.add(panel);
panel.add(textfield);
textfield.addActionListener(new Action4());
}
}
You can do something like this.
if(museum_name.equals("London")){
JOptionPane.showMessageDialog(null, " You are attending the " + museum_name);
} else{
// show the error message
}
Its encouraged to use equals() method for String comparison. Please note, equals() is used to compare two strings for equality, while operator == compares the reference of an object in java.
Update
To show an error message if the input is not "London", you can do something like this.
static class Action4 implements ActionListener {
#Override
public void actionPerformed(java.awt.event.ActionEvent e) {
String museum_name = ((JTextField) e.getSource()).getText();
if (museum_name.equals("London")) {
JOptionPane.showMessageDialog(null, "You are attending the " + museum_name);
} else {
JOptionPane.showMessageDialog(null, "Wrong input!");
}
}
}
I have created a JComboBox in order to add players to a list as I am creating a game. I am trying to show an error message using JOptionPane if the text field is left empty I did this using the method below.
btnAddPlayer = new JButton("Add Player");
btnAddPlayer.addActionListener(new ActionListener() { //This is the layout for the list of points that are possible to achieve
public void actionPerformed(ActionEvent arg0) {
if (txtAddPlayer.equals("")){
JOptionPane.showMessageDialog(btnAddPlayer, this, "Please Enter Full Details", NumofAnswers);//THIS IS THE METHOD I TRIED
} else {
comboBox.addItem(txtAddPlayer.getText());
}
}
});
btnAddPlayer.setBounds(469, 243, 89, 23);
panel.add(btnAddPlayer);
txtAddPlayer = new JTextField();
txtAddPlayer.setBounds(373, 244, 86, 20);
panel.add(txtAddPlayer);
txtAddPlayer.setColumns(10);
I am not sure why this is not working. Please provide an answer using my code.
Regards,
Should be
If (textField.gettext().equals(""))
I have an application that uses JDialog popups. I'm using 1.6 (compatibility requirement for the customer.) The ENTER key dismisses the dialog, taking previous results. The problem is that the user can type-ahead on hitting the ENTER key (or any other text for that matter).
Here's a snippet:
private void promptForCredentials(final GenericSwitch sw) {
final JDialog jd = new JDialog();
jd.setModal(true);
jd.getContentPane().setLayout(new FlowLayout());
final JLabel l_user = new JLabel("Username: ");
final JLabel l_pass = new JLabel("Password: ");
final JTextField tf_user = new JTextField(25);
final JTextField tf_pass = new JPasswordField(25);
JButton b_ok = new JButton("OK");
JButton b_same = new JButton("Same as previous");
JButton b_cancel = new JButton("Cancel");
final JLabel l_authfail = new JLabel("Authentication failed, try again.");
ActionListener okSameListener = new ActionListener() {
public void actionPerformed(ActionEvent ev) {
// if (((JButton) ev.getSource()).isVisible()) {
// return; // always returns for 'OK' button!
// }
String name = ((JButton) ev.getSource()).getText();
if (name.equals("OK")) {
// If user hits OK, save user/pw, unless they're both empty
// (treat that as "use last")
if (! tf_user.getText().isEmpty()
&& ! tf_pass.getText().isEmpty()) {
mLastUsername = tf_user.getText();
mLastPassword = tf_pass.getText();
}
}
jd.dispose();
sw.setUsername(mLastUsername, true);
sw.setPassword(mLastPassword, true);
if (!sw.checkLogin()) {
// auth failed so don't save for next time
mLastUsername = "";
mLastPassword = "";
}
display(sw.mIndex);
poll();
}
};
b_ok.addActionListener(okSameListener);
b_same.addActionListener(okSameListener);
b_cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
System.out.print("Poll cancelled\n");
abort();
jd.dispose();
}
});
Box box0 = Box.createHorizontalBox();
Box box1 = Box.createHorizontalBox();
Box box2 = Box.createHorizontalBox();
Box box3 = Box.createHorizontalBox();
box0.add(l_authfail);
box1.add(l_user);
box1.add(tf_user);
box2.add(l_pass);
box2.add(tf_pass);
box3.add(b_ok);
box3.add(Box.createHorizontalStrut(10));
if (!mLastUsername.isEmpty() && !mLastPassword.isEmpty()) {
box3.add(b_same);
box3.add(Box.createHorizontalStrut(10));
}
box3.add(b_cancel);
if (sw.authFailed()) {
jd.add(box0);
}
jd.add(box1);
jd.add(box2);
jd.add(box3);
SwingUtilities.getRootPane(b_ok).setDefaultButton(b_ok);
jd.setTitle("Login for " + sw.mShortname);
jd.setPreferredSize(new Dimension(400, 150));
jd.setSize(new Dimension(400, 150));
jd.setLocationRelativeTo(getContentPane());
jd.setVisible(true);
}
Since this app polls a number of external resources, most of which usually have the same username/pw, it's nice to just hit "enter" to cycle through the lot, the first time. All good. (And yeah, I know I should have "use same for subsequent" checkbox. Maybe later.)
However, I noticed to my surprise that I can type ahead. Frankly, I like this, but I don't think it's really appropriate for general users. How can I disable it?
Would it work to consume all actions prior to setting the JDialog visible? I tried checking if the button is visible, but while the Cancel button is, the OK button isn't! (commented out above)
Thanks
I thought it was something like this(but turns out it doesn't do anything!):
public void actionPerformed(ActionEvent e) {
String labelID = e.getActionCommand();
JTextField EnterName = new JTextField();
JLabel NameINLabel = new JLabel();
String random = new String();
random = EnterName.getText();
if (labelID == "Enter Name"){
NameINLabel.setText(random);
}
}
My button works perfectly, nothing is wrong with that. But when i click the button "Enter Name", nothing happens to the jlabel. HELP PLZ
I agree with vaisakh, you need to declare your components outside the event handler. You also need to add them to the content pane.
For compare content of string you should use the .equals("") or compareTo("") methods.
After the password is entered i want the window to disappear and pop a new window.
JButton btnEnter = new JButton("Enter");
btnEnter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
if(passwordField.equals("test"))
{
frame.setVisible(false);
}
else if(!passwordField.equals("test"))
{
JOptionPane.showMessageDialog(null,"Access Denied!!");
}
}
});
btnEnter.setBounds(149, 184, 117, 29);
frame.getContentPane().add(btnEnter);
I'm assuming passwordField is a JTextField, if so, you need to get the text from it, just .getText() I think and store that in a string. Then test the string. At the moment you are testing if your JTextField equals the string.
Create 2 JFrames and make a reference for each one:
JFrame oldFrame = new JFrame();
// ...
JFrame newFrame = new JFrame();
// ...
// ...
if(passwordField.equals("test"))
{
oldFrame.setVisible(false);
newFrame.setVisible(true);
}