Results 1 to 7 of 7
  1. #1
    Fumomo is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default Updating a JLabel from another class.

    Hi all, I've been trying to get this done but really don't know how to.

    First of all I have 3 classes, a tabbed pane class, test class and sample class. I have a comboBox in test class and a few JLabels in sample class.

    What I'm trying to do to is that whenever I select the comboBox options, i want to update the JLabels in the sample class. Right now i'm able to update the JLabels in the test class but not sample class.

    Here all the codes

    tabbedpane class
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class LayoutDemo
    {
    	public static void main (String[] args)
    	{
    		JFrame frame = new JFrame ("Layout Manager Demo");
    		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    		JTabbedPane tp = new JTabbedPane();
    		tp.addTab("Intro", new mainMenu());
    		tp.addTab ("Test", new test());
    		tp.addTab ("Sample", new sample());
    		frame.add(tp);
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
    test class
    Java Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class test extends JPanel
    {
    	private JComboBox sizeCB,CB2;
    	private JLabel hurhur,lala, troll;
    	private String[] size = {"0","1","2","3","4","5"};
    	private String rofl ="";
    
    	sample s = new sample();
    
    	public test()
    	{
    		sizeCB = new JComboBox(size);
    		sizeCB.setSelectedIndex(0);
    		sizeCB.addActionListener(new sizeListener());
    		hurhur= new JLabel("");
    		troll = new JLabel("trololo");
    		hurhur.setPreferredSize(new Dimension(200,200));
    		hurhur.setBackground(Color.white);
    		add(new JLabel("hehe"));
    		add(sizeCB);
    		add(hurhur);
    		add(troll);
    
    	}
    
    
    	private class sizeListener implements ActionListener
    	{
    		public void actionPerformed (ActionEvent event)
    		{
    			int index = sizeCB.getSelectedIndex();
    			if(index==2)
    				hurhur.setText("rofl " + index);
    				s.updateLabel("I");
    
    			if(index==1)
    				troll.setVisible(true);
    				hurhur.setText("omg " +index);
    				s.updateLabel2("am");
    
    			if(index==3)
    				troll.setVisible(false);
    				s.updateLabel3("updating");
    
    
    			if(index==0)
    			hurhur.setText("");
    
    
    		}
    	}
    }
    sample class
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class sample extends JPanel
    {
    
    	JLabel label,label2,label3;
    
    	public void updateLabel(String a)
    	{
    		label.setText(a);
    	}
    
    	public void updateLabel2(String b)
    	{
    		label2.setText(b);
    	}
    
    	public void updateLabel3(String c)
    	{
    		label3.setText(c);
    	}
    
    	public sample()
    	{
    		label = new JLabel("Test");
    		label2 = new JLabel("Testing2");
    		label3 = new JLabel("Testing testing");
    
    		add(label);
    		add(label2);
    		add(label3);
    	}
    }
    Help would be greatly appreciated. Thank you

  2. #2
    Join Date
    May 2011
    Location
    Munich
    Posts
    15
    Rep Power
    0

    Default

    Try formatting the java code.

    notice any interesting indentations in the 'if' statements in the ActionListener?

  3. #3
    Fumomo is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by oliver_watkins View Post
    Try formatting the java code.

    notice any interesting indentations in the 'if' statements in the ActionListener?
    Not sure if i'm getting you but are you referring to the if statements missing the {} brackets?

    Java Code:
    	private class sizeListener implements ActionListener
    	{
    		public void actionPerformed (ActionEvent event)
    		{
    			int index = sizeCB.getSelectedIndex();
    
    			if(index==2)
    			{
    
    				hurhur.setText("rofl " + index);
    				s.updateLabel("I");
    			}
    			if(index==1)
    			{
    
    				troll.setVisible(true);
    				hurhur.setText("omg " +index);
    				s.updateLabel2("am");
    			}
    			if(index==3)
    			{
    
    				troll.setVisible(false);
    				s.updateLabel3("updating");
    
    			}
    			if(index==0)
    			hurhur.setText("");
    
    
    		}
    	}

  4. #4
    Join Date
    May 2011
    Location
    Munich
    Posts
    15
    Rep Power
    0

    Default

    yes. hasnt that fixed the problem now?

  5. #5
    Fumomo is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by oliver_watkins View Post
    yes. hasnt that fixed the problem now?
    No unfortunately :(

  6. #6
    Join Date
    May 2011
    Location
    Munich
    Posts
    15
    Rep Power
    0

    Default

    You are adding this to the tab.

    tp.addTab ("Sample", new sample());

    but you are updating a class variable :

    sample s = new sample();

    make sure you add 's' to the tabbed pane

  7. #7
    Fumomo is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by oliver_watkins View Post
    You are adding this to the tab.

    tp.addTab ("Sample", new sample());

    but you are updating a class variable :

    sample s = new sample();

    make sure you add 's' to the tabbed pane
    Ah yes! That makes so much sense when you mentioned it now! Thank you so much! :D

    For others who makes a similar silly mistake in the future, this is what i added to the codes.

    tabbedpane class
    Java Code:
    	
    [B]static	sample s = new sample();[/B]
    	public static void main (String[] args)
    	{
    		JFrame frame = new JFrame ("Layout Manager Demo");
    		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    
    
    		JTabbedPane tp = new JTabbedPane();
    		tp.addTab("Intro", new mainMenu());
    		tp.addTab ("Test", new test());
    		[B]tp.addTab ("Sample", s);[/B]
    		tp.addTab ("Track", new trackOrder());
    		frame.add(tp);
    		frame.pack();
    		frame.setVisible(true);
    	}
    test class
    Java Code:
    [B]sample sa = LayoutDemo.s;[/B]
    
    	public class sizeListener implements ActionListener
    	{
    		public void actionPerformed (ActionEvent event)
    		{
    			int index = sizeCB.getSelectedIndex();
    		//	int index1 = CB2.getSelectedIndex();
    			if(index==2)
    			{
    
    				hurhur.setText("rofl " + index);
    				[B]sa.updateLabel("I");[/B]
    			}
    			if(index==1)
    			{
    
    				troll.setVisible(true);
    				hurhur.setText("omg " +index);
    				[B]sa.updateLabel2("am");[/B]
    			}
    			if(index==3)
    			{
    
    				troll.setVisible(false);
    				[B]sa.updateLabel3("updating");[/B]
    
    			}
    			if(index==0)
    			hurhur.setText("");
    
    
    		}
    	}

Similar Threads

  1. Replies: 3
    Last Post: 12-16-2013, 06:35 PM
  2. Updating my gui
    By mrx89_7 in forum New To Java
    Replies: 4
    Last Post: 02-09-2011, 06:33 AM
  3. Swing JLabel Updating (Threading)
    By Skezza in forum AWT / Swing
    Replies: 3
    Last Post: 07-27-2010, 04:43 PM
  4. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-07-2010, 12:02 AM
  5. Updating my GUI
    By Catkill in forum AWT / Swing
    Replies: 6
    Last Post: 09-01-2009, 06:09 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
  •