overlapping output in java applet...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: overlapping output in java applet...

  1. #1
    Join Date
    Aug 2005
    Posts
    6

    Unhappy overlapping output in java applet...

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    
    public class TicketInterface extends Applet implements ActionListener
    { 
    private JPanel p = new JPanel();
    private String UsernameAdmin;
    private String PasswordAdmin;
    private JComboBox DateOfShowing = new JComboBox();
    private JComboBox MovieTitle = new JComboBox();
    private JComboBox TimeOfShowing = new JComboBox();
    
    
    private JPanel PanelError = new JPanel();
    private JLabel error = new JLabel ("Wrong username or password");
    private JButton back = new JButton ("Back");
    
    private JButton admin = new JButton ("Admin");
    private JButton user = new JButton ("User");
    
    private JPanel TextFieldUserAdmin = new JPanel();
    private JTextField UserAdmin = new JTextField(10);
    private JButton OkUserAdmin = new JButton ("Ok");
    
    private JPanel TextFieldPassAdmin = new JPanel();
    private JTextField PassAdmin = new JTextField(10);
    private JButton OkPassAdmin = new JButton ("Ok");
    
    private JPanel ButtonsAdmin = new JPanel();
    private JButton ButtonViewTimeShowing = new JButton ("View Time Showing");
    private JButton ButtonViewAvailableSeats = new JButton ("View Available Seats");
    private JButton ButtonViewSeatsDiagram = new JButton ("View seats diagram");
    private JButton ButtonBuyTicket = new JButton ("Buy Ticket");
    private JButton ButtonEditMovie = new JButton ("Edit movie");
    
    private JPanel PanelViewTimeShowing = new JPanel();
    private JPanel PanelViewAvailableSeats = new JPanel();
     
    
    public void init()
    { 
     MainMenu();
    
    }
    
    public void MainMenu()
    { 
     setLayout(new BorderLayout());
     p.add(admin);
     p.add(user);
     add(p, BorderLayout.SOUTH);
     
     admin.addActionListener(this);
     user.addActionListener(this);
    }
    
    
    public void UserAdmin()
    {
      
     TextFieldUserAdmin.setLayout(new FlowLayout(FlowLayout.CENTER,30,150));
     TextFieldUserAdmin.add(new JLabel("Username : "));
     TextFieldUserAdmin.add(UserAdmin);
     TextFieldUserAdmin.add(OkUserAdmin); 
    
     add(TextFieldUserAdmin);
     UserAdmin.addActionListener(this);
     OkUserAdmin.addActionListener(this);
    
    }
    
    public void PassAdmin()
    {
     TextFieldPassAdmin.setLayout(new FlowLayout(FlowLayout.CENTER,30,150));
     TextFieldPassAdmin.add(new JLabel("Password : "));
     TextFieldPassAdmin.add(PassAdmin);
     TextFieldPassAdmin.add(OkPassAdmin); 
    
     add(TextFieldPassAdmin);
     PassAdmin.addActionListener(this);
     OkPassAdmin.addActionListener(this);
    }
    
    public void MainMenuAdmin()
    {
     ButtonsAdmin.setLayout(new GridLayout(6,1,10,20));
     ButtonsAdmin.add(ButtonViewTimeShowing);
     ButtonsAdmin.add(ButtonViewAvailableSeats);
     ButtonsAdmin.add(ButtonViewSeatsDiagram);
     ButtonsAdmin.add(ButtonBuyTicket);
     ButtonsAdmin.add(ButtonEditMovie);
     
     add(ButtonsAdmin, BorderLayout.WEST);
     ButtonViewTimeShowing.addActionListener(this);
     ButtonViewAvailableSeats.addActionListener(this);
     ButtonViewSeatsDiagram.addActionListener(this);
     ButtonBuyTicket.addActionListener(this);
     ButtonEditMovie.addActionListener(this);
    
    }
    
    public void ViewTimeShowing()
    {
     PanelViewTimeShowing.setLayout(new FlowLayout(FlowLayout.CENTER,45,40));
     PanelViewTimeShowing.add(new JLabel("Date of Showing : "));
     PanelViewTimeShowing.add(DateOfShowing);
     PanelViewTimeShowing.add(new JLabel("Movie Title : "));
     PanelViewTimeShowing.add(MovieTitle);
     PanelViewTimeShowing.add(new JLabel("Time of Showing : "));
     PanelViewTimeShowing.add(TimeOfShowing);
     
     add(PanelViewTimeShowing);  
    
    }
    
    public void ViewAvailableSeats()
    {
     PanelViewAvailableSeats.setLayout(new FlowLayout(FlowLayout.CENTER,45,40));
     PanelViewAvailableSeats.add(new JLabel("Date of Showing : "));
     PanelViewAvailableSeats.add(DateOfShowing);
     PanelViewAvailableSeats.add(new JLabel("Movie Title : "));
     PanelViewAvailableSeats.add(MovieTitle);
     PanelViewAvailableSeats.add(new JLabel("Time of fdwing : "));
     PanelViewAvailableSeats.add(TimeOfShowing);
     
     add(PanelViewAvailableSeats);  
    
    }
    
    public void actionPerformed(ActionEvent e)
    {
     
     if (e.getSource() == admin)
     {
         remove(p);
         UserAdmin();
         validate();
         
     }
     
     if (e.getSource() == user)
     {
         remove(p);
     }
     
     if (e.getSource() == OkUserAdmin)
     {
       remove(TextFieldUserAdmin);
       UsernameAdmin = UserAdmin.getText();
       PassAdmin();
       validate();
     }
     
     if (e.getSource() == OkPassAdmin)
     {
       remove(TextFieldPassAdmin); 
       PasswordAdmin = PassAdmin.getText();
       Admin a1 = new Admin();
       Admin a2 = new Admin();
       boolean flag = a1.VerifyUsername(UsernameAdmin);
       boolean flag2 = a2.VerifyPassword(PasswordAdmin);
       
       if (flag == true && flag2 == true)
       { 
        MainMenuAdmin();
        validate();
      
        
       }
       
       else
       {
        PanelError.setLayout(new FlowLayout(FlowLayout.CENTER,30,150));
        PanelError.add(error);
        PanelError.add(back);
        back.addActionListener(this);
        add(PanelError);
        validate();
       
       }
     }
     
     if (e.getSource() == ButtonViewTimeShowing)
     {
      remove(PanelViewTimeShowing);
      ViewTimeShowing();
      validate();
     }
     
     if (e.getSource() == ButtonViewAvailableSeats)
     {
      remove(PanelViewTimeShowing);
      ViewAvailableSeats();
      validate();
     }
     
     if (e.getSource() == back)
     {
       remove(PanelError);
       UserAdmin();
       validate();
     } 
    }
    
    }
    above class is called Ticket Interface....

    Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    
    public class Admin
    {
    private String UserAdmin;
    
    public boolean VerifyUsername(String UserAdmin)
    {
    
    if (UserAdmin.equals("admin"))
    {
    return true;
    }
    
    else
    {
    return false;
    }
    
    }
    
    public boolean VerifyPassword(String PassAdmin)
    {
     
    if (PassAdmin.equals("pass"))
    {
    return true;
    }
    
    else
    {
    return false;
    }
     
    }
    }
    this is called class Admin....

    if you refer to the attachment....there are the output of the above program....but somehow it doesn't work correctly...

    if you see the problem1.jpg picture, after i resize the appletviewer in run time, the ok button is gone...but if i don't resize the appletviewer in run time...it will show some sort of overlapping output...i don't know why....

    for problem2.jpg...the output came when i press the button View Time Showing more than two times...why it show the output like that?

    p/s : anyway the username for admin is admin...and the password is pass....for the button user...i don't finished yet build the function
    Attached Images Attached Images

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I've adhusted some of this....

    First, you should initiate your GUI at startup, and not readd and recreate
    components along the way, and readd actionListeners either.
    Also, observe the layoutmanagement as you add/replace components.

    Note the setCenterPan method, it keeps track of the current center
    component and replaces it.

    I have also replaced some flowlayouts w. gridlayouts, flowlayout is not very
    considerate when it comes to empty components (comboboxes)...

    Also, I upgraded it to a JApplet since the rest is already swing.

    Code:
    public class TicketInterface
        extends JApplet
        implements ActionListener {
      private JPanel currentPan = new JPanel();
      private String UsernameAdmin;
      private String PasswordAdmin;
      private JComboBox DateOfShowing = new JComboBox();
      private JComboBox MovieTitle = new JComboBox();
      private JComboBox TimeOfShowing = new JComboBox();
    
      private JPanel PanelError = new JPanel();
      private JLabel error = new JLabel("Wrong username or password");
      private JButton back = new JButton("Back");
    
      private JButton admin = new JButton("Admin");
      private JButton user = new JButton("User");
    
      private JPanel TextFieldUserAdmin = new JPanel();
      private JTextField UserAdmin = new JTextField(10);
      private JButton OkUserAdmin = new JButton("Ok");
    
      private JPanel TextFieldPassAdmin = new JPanel();
      private JTextField PassAdmin = new JTextField(10);
      private JButton OkPassAdmin = new JButton("Ok");
    
      private JPanel ButtonsAdmin = new JPanel();
      private JButton ButtonViewTimeShowing = new JButton("View Time Showing");
      private JButton ButtonViewAvailableSeats = new JButton("View Available Seats");
      private JButton ButtonViewSeatsDiagram = new JButton("View seats diagram");
      private JButton ButtonBuyTicket = new JButton("Buy Ticket");
      private JButton ButtonEditMovie = new JButton("Edit movie");
    
      private JPanel PanelViewTimeShowing = new JPanel();
      private JPanel PanelViewAvailableSeats = new JPanel();
    
      public void init() {
        MainMenu();
        setupGUI();
        hookUpButtons();
      }
      private void hookUpButtons () {
        admin.addActionListener(this);
        user.addActionListener(this);
        UserAdmin.addActionListener(this);
        OkUserAdmin.addActionListener(this);
        PassAdmin.addActionListener(this);
        OkPassAdmin.addActionListener(this);
        ButtonViewTimeShowing.addActionListener(this);
        ButtonViewAvailableSeats.addActionListener(this);
        ButtonViewSeatsDiagram.addActionListener(this);
        ButtonBuyTicket.addActionListener(this);
        ButtonEditMovie.addActionListener(this);
      }
      private void setupGUI () {
    
        ButtonsAdmin.setLayout(new GridLayout(6, 1, 0, 5));
        ButtonsAdmin.add(ButtonViewTimeShowing);
        ButtonsAdmin.add(ButtonViewAvailableSeats);
        ButtonsAdmin.add(ButtonViewSeatsDiagram);
        ButtonsAdmin.add(ButtonBuyTicket);
        ButtonsAdmin.add(ButtonEditMovie);
    
        TextFieldUserAdmin.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 5));
        TextFieldUserAdmin.add(new JLabel("Username : "));
        TextFieldUserAdmin.add(UserAdmin);
        TextFieldUserAdmin.add(OkUserAdmin);
    
        TextFieldPassAdmin.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 5));
        TextFieldPassAdmin.add(new JLabel("Password : "));
        TextFieldPassAdmin.add(PassAdmin);
        TextFieldPassAdmin.add(OkPassAdmin);
    
        PanelViewTimeShowing.setLayout(new GridLayout(6, 1, 0, 5));
        PanelViewTimeShowing.add(new JLabel("Date of Showing : "));
        PanelViewTimeShowing.add(DateOfShowing);
        PanelViewTimeShowing.add(new JLabel("Movie Title : "));
        PanelViewTimeShowing.add(MovieTitle);
        PanelViewTimeShowing.add(new JLabel("Time of Showing : "));
        PanelViewTimeShowing.add(TimeOfShowing);
    
        PanelViewAvailableSeats.setLayout(new GridLayout(6, 1, 0, 5));
        PanelViewAvailableSeats.add(new JLabel("Date of Showing : "));
        PanelViewAvailableSeats.add(DateOfShowing);
        PanelViewAvailableSeats.add(new JLabel("Movie Title : "));
        PanelViewAvailableSeats.add(MovieTitle);
        PanelViewAvailableSeats.add(new JLabel("Time of fdwing : "));
        PanelViewAvailableSeats.add(TimeOfShowing);
    
    
      }
    
      public void MainMenu() {
        getContentPane().setLayout(new BorderLayout(20,5));
        currentPan.add(admin);
        currentPan.add(user);
        getContentPane().add(currentPan, BorderLayout.SOUTH);
      }
      public void UserAdmin() {
        setCenterPan(TextFieldUserAdmin);
    
      }
      public void PassAdmin() {
        setCenterPan(TextFieldPassAdmin);
      }
      public void MainMenuAdmin() {
        getContentPane().add(ButtonsAdmin, BorderLayout.WEST);
      }
      public void ViewTimeShowing() {
        setCenterPan(PanelViewTimeShowing);
      }
      public void ViewAvailableSeats() {
        setCenterPan(PanelViewAvailableSeats);
      }
    
      private void setCenterPan(JPanel pan) {
        if (currentPan!=null) {
          getContentPane().remove(currentPan);
        }
        currentPan=pan;
        getContentPane().add(currentPan, BorderLayout.CENTER);
        getContentPane().validate();
        getContentPane().repaint();
      }
    
      public void actionPerformed(ActionEvent e) {
    
        if (e.getSource() == admin) {
          getContentPane().remove(currentPan);
          UserAdmin();
        } else if (e.getSource() == user) {
          getContentPane().remove(currentPan); // this was kindof empty...
        } else if (e.getSource() == OkUserAdmin) {
          UsernameAdmin = UserAdmin.getText();
          PassAdmin();
        } else if (e.getSource() == OkPassAdmin) {
          getContentPane().remove(TextFieldPassAdmin);
          PasswordAdmin = PassAdmin.getText();
          Admin a1 = new Admin();
          Admin a2 = new Admin();
          boolean flag = a1.VerifyUsername(UsernameAdmin);
          boolean flag2 = a2.VerifyPassword(PasswordAdmin);
    
          if (flag == true && flag2 == true) {
            MainMenuAdmin();
          } else {
            PanelError.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 150));
            PanelError.add(error);
            PanelError.add(back);
            back.addActionListener(this);
            setCenterPan(PanelError);
          }
        } else if (e.getSource() == ButtonViewTimeShowing) {
          ViewTimeShowing();
        } else if (e.getSource() == ButtonViewAvailableSeats) {
          ViewAvailableSeats();
        } else if (e.getSource() == back) {
          UserAdmin();
        }
    
        getContentPane().validate();
        getContentPane().repaint();
      }
    }
    Last edited by sjalle; 09-04-2005 at 09:51 AM.
    eschew obfuscation

  3. #3
    Join Date
    Aug 2005
    Posts
    6
    ok...thanx for the reply mr sjalle....i really appreciate your help

Similar Threads

  1. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 07:46 AM
  2. Java applet window
    By crash75uk in forum Java
    Replies: 1
    Last Post: 04-20-2005, 04:32 PM
  3. Java Applet won't work via browsers
    By Graham in forum Java
    Replies: 1
    Last Post: 02-20-2001, 02:47 AM
  4. java applet and html output passed
    By Edmond Melkomian in forum Java
    Replies: 0
    Last Post: 04-21-2000, 02:22 AM
  5. Quotes on writing a java applet
    By Mike Buchanan in forum Java
    Replies: 0
    Last Post: 04-12-2000, 10:53 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center