Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting JLabel Text at Runtime
    primarykey
    data
    text
    <p>I am pretty new to Java(about 2 weeks in), and I am trying to set the text of a JLabel. The only problem is I am doing calculations in another class and I don't know how to reference the Jlabel I have already created. Here are the two classes in question.</p> <pre><code>package fightsim; import javax.swing.JPanel; import net.miginfocom.swing.MigLayout; import javax.swing.JLabel; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class FightSimPane extends JPanel { FightManager FightManager = new FightManager(); /** * Create the panel. */ public FightSimPane() { setLayout(new MigLayout("", "[][][][][][][][][][]", "[][][][]")); JLabel lblChampionleft = new JLabel("ChampionLeft"); add(lblChampionleft, "cell 1 3"); JButton btnGo = new JButton("Go"); btnGo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { FightManager.startFight(); FightManager.runTheFight(); } }); add(btnGo, "cell 5 3"); JLabel lblChampionright = new JLabel("ChampionRight"); add(lblChampionright, "cell 9 3"); } public void setLeftChampionLabel(String s){ //not able to reference Jlabel lblChampionLeft here??? System.out.println("Setting Left Champion text to"+s); } public void setRightChampionLabel(String s){ //not able to reference Jlabel lblChampionRight here??? System.out.println("Setting Right Champion text to"+s); } } </code></pre> <p>And the class that is trying to set the Label.</p> <pre><code>package fightsim; public class FightManager { Champion LeftChamp = new Champion(); Champion RightChamp = new Champion(); public FightManager() { } Thread LeftChampThread = new Thread(LeftChamp); Thread RightChampThread = new Thread(RightChamp); ; public void startFight() { LeftChamp.setHealth(200); RightChamp.setHealth(300); LeftChamp.setATKsp(1000); RightChamp.setATKsp(1000); LeftChamp.setAD(20); RightChamp.setAD(20); } public void runTheFight() { System.out.println("Starting Threads"); LeftChampThread.start(); RightChampThread.start(); while ((LeftChamp.getHealth() &gt; 0) &amp;&amp; (RightChamp.getHealth() &gt; 0)) { if (RightChamp.isReadyToAttack()) { LeftChamp.setHealth(LeftChamp.getHealth() - RightChamp.getAD()); RightChamp.setNotReady(); System.out.println("Setting Left Champion test to" + Integer.toString(LeftChamp.getHealth())); // This is where I'd like to update the left Jlabel in // FightSimPane } if (LeftChamp.isReadyToAttack()) { RightChamp .setHealth(RightChamp.getHealth() - LeftChamp.getAD()); LeftChamp.setNotReady(); System.out.println("Setting Right Champion test to" + Integer.toString(RightChamp.getHealth())); // This is where I'd like to update the right Jlabel in // FightSimPane } } } } </code></pre> <p>So, the question...How do I let my FightManager Class access and change the JLabel in my FightSimPane Class/Gui. Thanks in advance, and sorry if this is a stupid question. I am terribly new to programming and I'm still trying to take it all in. With that said, any other advice would be great. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload