www.webdeveloper.com
Results 1 to 9 of 9

Thread: Counter in GUI

  1. #1
    Join Date
    Sep 2004
    Posts
    23

    Counter in GUI

    I'm making an airline program that keeps track of passengers, and I was wanting to keep a counter on the GUI so is there a way I do this?
    Maybe with a JTextArea?
    I'm very new to java BTW

    thanks.

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    I think there is a JLabel as well use that..and repaint it every time counter changes.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Sep 2004
    Posts
    23
    Can I do something like this? I just want to counter to add one when clicked.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class lab extends JApplet implements ActionListener
    {
    private static int count=0;


    public void init()
    {

    Container container=getContentPane();
    container.setLayout(new FlowLayout());
    JButton button=new JButton("Add");
    JLabel label=new JLabel("Passenger count");
    //has to have the "" to work
    JTextField field=new JTextField(count+"",4);


    container.add(new JScrollPane(button));
    container.add(new JScrollPane(label));
    container.add(new JScrollPane(field));

    setSize(350,100);
    setVisible(true);

    button.addActionListener(this);

    }//end main


    public void actionPerformed(ActionEvent event)
    {
    count++;
    Container.getContentPane().repaint();
    }
    }

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    don't have to repaint the whole of the container(too much work?)
    I think just repainting the label or textfield or text area will do.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  5. #5
    Join Date
    Sep 2004
    Posts
    23
    Ok, so I just make field static and then do I use field.repaint() like this?


    public void actionPerformed(ActionEvent event)
    {
    count++;
    field.repaint();
    }

  6. #6
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    instead of making counter static,
    get the existing value from the field,
    convert it to an integer
    and add one to it
    and then set the new value and finally repaint
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  7. #7
    Join Date
    Sep 2004
    Posts
    23
    Ok I did what you said and now I have this

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class lab extends JApplet implements ActionListener
    {
    private static int count=0;
    private static int number=0;
    private static JTextField field;

    public void init()
    {

    Container container=getContentPane();
    container.setLayout(new FlowLayout());
    JButton button=new JButton("Add");
    JLabel label=new JLabel("Passenger count");
    field=new JTextField("0",4);
    String num=field.getText();
    number=Integer.parseInt(num);

    //has to have the "" to work


    container.add(new JScrollPane(button));
    container.add(new JScrollPane(label));
    container.add(new JScrollPane(field));

    setSize(350,100);
    setVisible(true);
    button.addActionListener(this);

    }//end init


    public void actionPerformed(ActionEvent event)
    {
    number++;
    field.repaint();
    }


    }//end class
    but something isn't right, what am I doing wrong?

  8. #8
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    There is still allot thats missing...anyways..here is the working solution

    PHP Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class 
    lab extends JApplet implements ActionListener
    {
    private 
    JTextField field;

    public 
    void init()
    {

    Container container=getContentPane();
    container.setLayout(new FlowLayout());
    JButton button=new JButton("Add");
    button.setName("Add");
    JLabel label=new JLabel("Passenger count");
    field=new JTextField("0",4);

    container.add(new JScrollPane(button));
    container.add(new JScrollPane(label));
    container.add(new JScrollPane(field));

    setSize(350,100);
    setVisible(true);
    button.addActionListener(this);

    }
    //end init


    public void actionPerformed(ActionEvent event){
    if(
    event.getSource() instanceof JButton){
      
    JButton button = (JButton)event.getSource();
      if(
    button!=null && button.getName().equals("Add")){
        
    String num=field.getText();
        
    int number=Integer.parseInt(num);
        
    number number 1;
        
    field.setText(String.valueOf(number));
        
    field.repaint();
      }
    }

    }

    public static 
    void main(String[] args){
      
    lab applet = new lab();
      
    Frame frame;
          
    frame = new Frame();
          
    frame.setTitle("Applet Frame");
          
    frame.add(appletBorderLayout.CENTER);
          
    applet.init();
          
    applet.start();
          
    frame.setSize(400,320);
          
    Dimension d Toolkit.getDefaultToolkit().getScreenSize();
          
    frame.setLocation((d.width frame.getSize().width) / 2, (d.height frame.getSize().height) / 2);
          
    frame.setVisible(true);

    }
    }
    //end class 
    This will do....
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  9. #9
    Join Date
    Sep 2004
    Posts
    23
    Thanks a lot!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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



Recent Articles