Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Updating the interface

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Updating the interface

    I want to creat simple aplication which if I click a button it will do something and the value would update on the interface , the value updates but not on the interface
    Code:
    public class baza {
    public static void main(String[] args)
    {derivat f=new derivat("titlu");
    	f.initializare();
    	f.show();
    }
    }
    import java.awt.*;
    import java.awt.event.*;
    public class derivat extends Frame implements ActionListener{
    	private int k=0,a=0;             //it's declaration
    	public derivat(String titlu){
    		super(titlu);
    	}
    	public void initializare(){
    		setLayout(new GridLayout(2,0));
    		setSize(900,900);
    		Panel x=new Panel();
    		x.setLayout(new FlowLayout());
    		Button b1=new Button("Stand1");
    		x.add(b1);
    		x.add(new Label("Standuri:"+k));        // where it is called in the code 
    		Button b2=new Button("Vanzare1");
    		x.add(b2);
    		add(x);
    		Panel y=new Panel();
    		y.setLayout(new FlowLayout());
    		Button b3=new Button("Stand2");
    		y.add(b3);
    		y.add(new Label("Standuri:"+a));
    		Button b4=new Button("Vanzare2");
    		y.add(b4);
    		add(y);
    		b1.addActionListener(this);
    		b2.addActionListener(this);
    		b3.addActionListener(this);
    		b4.addActionListener(this);
    		}
    	public void actionPerformed(ActionEvent e){
    		String command=e.getActionCommand();
    		if (command.equals("Stand1"))
    	++k;                                               // the value which I update but doesn't show on the interface
    	    else if(command.equals("Stand2"))
    			a++;
    	}
    }


    Please help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Updating the interface

    Where does the code try to show the new value of the variable? After its value is changed, the GUI needs to be updated with the new value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Updating the interface

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    You should be using Swing (JLabel) instead of AWT (Label).

    Look at the API for Label to review the methods that will help you do what you've described.

Similar Threads

  1. Updating JList
    By luigi10011 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 31st, 2013, 11:29 PM
  2. [SOLVED] Updating The Components
    By beer-in-box in forum AWT / Java Swing
    Replies: 14
    Last Post: December 16th, 2012, 11:02 AM
  3. Updating JList
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 3
    Last Post: October 6th, 2011, 07:17 AM
  4. Updating timer
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 26th, 2011, 07:02 AM
  5. updating database
    By gurpreetm13 in forum JDBC & Databases
    Replies: 3
    Last Post: October 9th, 2009, 11:43 AM