Results 1 to 5 of 5
  1. #1
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default add jlabel to jinternal frame.

    Guys, I am working on this java project. I have a user login window where users can type username. if username doesnt matches the value in DB, it should display an error "Invalid User" at the bottom of the window. I was trying to add a new jlabel with the error message and add it to the frame from within an else clause.ie,

    this is the logic
    -------------

    if(username matches){

    ---proceed }

    else(display error message){

    container.add(errorLbl);-----------------here i get error, cant add to the window
    }

    is it legal to add to the container from within an else clause?

    forgot to mention this..

    final Container container = getContentPane();

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Yes, it is legal if you have variables that are in scope (my guess is that you don't -- but I can't tell for sure given the information at hand).

    If you are still having problems, you'll probably want to give us more information about your code, the actual text of the error message, and any other information necessary for us to be able to answer your question.

  3. #3
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default

    sorry this is my code...


    public class LoginUI1 extends JInternalFrame {

    private static final long serialVersionUID = 1L;
    public static JTextArea nameFld0 = new JTextArea();
    public static JTextArea TaskFld0 = new JTextArea();
    private JLabel usernameLbl = new JLabel("Username");
    private JLabel TaskLbl1 = new JLabel("Task Description");
    private JLabel TaskLbl2 = new JLabel("Task Date");
    private JLabel TaskLbl3 = new JLabel("Task Time");
    public static JTextField usernameFld = new JTextField(16);
    public static JTextField TaskFld1 = new JTextField(16);
    public static JTextField TaskFld2 = new JTextField(16);
    public static JTextField TaskFld3 = new JTextField(16);
    private JButton createTaskBtn = new JButton("Create Task");
    private JButton cancelBtn = new JButton("Cancel");
    private JLabel errorLbl = new JLabel("Unknown User. Try Again!!!");

    // Login Constructor
    public LoginUI1(final String name) {
    super(name);

    final Container container = getContentPane();

    FlowLayout layout = new FlowLayout();
    container.setLayout(layout);
    layout.setAlignment(FlowLayout.LEFT);
    container.setBackground(Color.orange);

    container.add(usernameLbl);
    container.add(usernameFld);
    container.add(TaskLbl1);
    container.add(TaskFld1);
    container.add(TaskLbl2);
    container.add(TaskFld2);
    container.add(TaskLbl3);
    container.add(TaskFld3);

    container.add(createTaskBtn);
    container.add(cancelBtn);



    createTaskBtn.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent event) {
    Customer customer = new Customer();
    Task task = new Task();
    boolean conn;
    customer.setName(usernameFld.getText());
    task.setTaskDescription(TaskFld1.getText());
    task.setDate(TaskFld2.getText());
    task.setTime(TaskFld3.getText());
    customer.setTask(task);

    CustomerManager customerMgr = new CustomerManager();
    try {
    conn = customerMgr.makeConnection();
    if (conn) {
    try {
    customerMgr.create(customer);
    } catch (InvalidCustomerException e) {
    JOptionPane.showMessageDialog(new JFrame(),
    "Invalid Customer Information", "Error",
    JOptionPane.ERROR_MESSAGE);

    } catch (ServiceLoadException e) {
    JOptionPane.showMessageDialog(new JFrame(),
    "System Unavailable", "Error",
    JOptionPane.ERROR_MESSAGE);
    e.printStackTrace();
    }

    try {
    customerMgr.create(task, customer);
    } catch (ServiceLoadException ex) {
    Logger.getLogger(LoginUI1.class.getName()).log(Lev el.SEVERE, null, ex);
    } catch (InvalidTaskException e) {
    JOptionPane.showMessageDialog(new JFrame(),
    "Invalid Customer Information", "Error",
    JOptionPane.ERROR_MESSAGE);

    }


    setVisible(false);

    try {
    customerMgr.readCustomer(customer);


    nameFld0.setText(customer.getName());

    } catch (CustomerNotFoundException e) {


    e.printStackTrace();
    } catch (ServiceLoadException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    customerMgr.readTask(task);

    TaskFld0.setText(task.getTaskDescription() + " "
    + task.getDate() + " " + task.getTime());

    } catch (ServiceLoadException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (TaskNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    new Display();

    } else {
    System.out.println("unknown user:");
    usernameFld.setText("");
    TaskFld1.setText("");
    TaskFld2.setText("");
    TaskFld3.setText("");
    container.add(errorLbl);
    }

    } catch (ServiceLoadException ex) {
    Logger.getLogger(LoginUI1.class.getName()).log(Lev el.SEVERE, null, ex);
    }
    }
    });


    cancelBtn.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent event) {

    usernameFld.setText("");
    TaskFld1.setText("");
    TaskFld2.setText("");
    TaskFld3.setText("");

    setVisible(false);
    }
    });

    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    pack();
    setVisible(true);
    setSize(225, 300);

    }
    }

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    You have two options in my opinion:

    - call revalidate() after container.add(errorLbl);
    or
    - add the label previously... like the other labels, but make it not visible (setVisible(false) ... and in your listener you call setVisible(true) on the label.

  5. #5
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default

    Yes!!! that worked. i added it at the top and setVisible(true) at the bottom.
    thnx man... appreciate it..

Similar Threads

  1. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  2. Replies: 7
    Last Post: 11-02-2009, 06:31 PM
  3. Disable Drag mode of Jinternal Frame
    By smartsubroto in forum New To Java
    Replies: 4
    Last Post: 06-23-2008, 07:21 AM
  4. GUI - JLabel
    By Azndaddy in forum New To Java
    Replies: 8
    Last Post: 05-02-2008, 07:03 AM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 02:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •