Results 1 to 5 of 5
Thread: add jlabel to jinternal frame.
- 10-09-2010, 08:35 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 40
- Rep Power
- 0
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();
-
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.
- 10-09-2010, 10:22 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 40
- Rep Power
- 0
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);
}
}
- 10-09-2010, 10:30 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 785
- Rep Power
- 12
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.
- 10-09-2010, 10:37 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-07-2010, 12:02 AM -
Returning focus to a frame after hiding another frame
By fletcher in forum AWT / SwingReplies: 7Last Post: 11-02-2009, 07:31 PM -
Disable Drag mode of Jinternal Frame
By smartsubroto in forum New To JavaReplies: 4Last Post: 06-23-2008, 08:21 AM -
GUI - JLabel
By Azndaddy in forum New To JavaReplies: 8Last Post: 05-02-2008, 08:03 AM -
JLabel
By Freddie in forum AWT / SwingReplies: 2Last Post: 05-29-2007, 03:19 PM
Bookmarks