I am trying to change the text inside a jLabel from the main method, the reason for this is because there are conditions that I need to meet for the change to happen and it is not trigger based.
Code:
public class TheMain extends javax.swing.JFrame {
public TheMain() {
initComponents();
}
public void changeLabel1(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jLabel1.setText("looo");
}
});
}
public void changeLabel2(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jLabel2.setText("looo");
}
});
}
public static void main(String args[]) {
TheMain some = new TheMain();
if(condition){
some.changeLabel1();
}else{
some.changeLabel2();
}
}
}
I tried printing some stuff inside changelabel1 and changelabel2 just to check if they are successfully called and it did print but I'm guessing it is not possible to implement the UI changes inside them, or am I mistaken?
if(condition){
some.changeLabel1();
}else{
some.changeLabel2();
}
The above logice needs to be defined in TheMain class because that is where the label variables will be defined.
The main() method is just used to create the GUI. There should be no application logic in the main() method.
Related
public class Ouvrier extends javax.swing.JFrame {
private final int numOuvrier;
public Ouvrier(int numOuvrier) {
initComponents();
this.numOuvrier=numOuvrier;
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ouvrier().setVisible(true);
}
});
}
I have added an attribute to a constructor and an error appears in the main frame - why is that?
new Ouvrier(0).setVisible(true);
I dont know what numOuvrier means, but you need pass it.
I want to create one Jframe from another class (main class) and then I want to display Jframe only if no error in main method. Otherwise I want to create & display dialog by passing that same jframe.
I forgot thread concept anywhere give me the solution, I tried below code, that prints "abcde" but not display the frame program complete.
Note : there is no main method in JFrame od ErrorDialog. They are just custom container.
public class Start{
public static Main mf=null;
public static void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
mf = new Main();
}
});
try {
// Some extra code
System.out.println("abcde"); // this is print and then program complete
mf.setVisible(true); // this line will not run
} catch (Exception e) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ErrorDialog(mf, true).setVisible(true);;
}
});
}
}
}
Try my suggestion its working..
Just use invokeAndWait() method insted of using invokeLater() method.
Because invokeLater() method create NullpointerException.
But invokeAndWait() must be inside try-catch block.
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
mf = new Main();
}
});
} catch (Exception e) {
System.out.println(e);
}
I've got several unrelated swing classes that are instances of JFrame. The issue is, I'd like to be able to create a controller class with a static method to load the appropriate frames as needed. I'm experiencing difficulty with the following code as many of components are failing to load. I suspect that the issue is with the event dispatch thread. Below is the code that doesn't work:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Controller {
private Controller() {
}
public static void initialize(final JFrame frame) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame selectionFrame = frame;
}
});
}
public static void main(String[] args) {
Controller.initialize(new SampleFrame());
}
}
The program loads the frame and toolbar components but other components fail to load. NOTE: The program runs just fine if I refactor the Controller like so :
public class Controller {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new SampleFrame();
}
});
}
}
I have a class on which it hold a JFrame, and on my JFrame, it contains some methods that i would want to call. The problem is that my class is in the main thread and mFrame is now delegated to the EDT thread, so how do i sent instructions to EDT to run those methods?
Update:
This is my FrameRadar that extends JFrame, as you see i have a update method, basically there is also a update on mDisplay and mRadar, those are jpanel.
FrameRadar Class ...(
public class FrameRadar{
...
public void update() {
mAccessor.getmDisplay().update();
mAccessor.getmRadar().update();
}
This is my MainRadar class, it contains a FrameRadar on which i also have a update method. That update method will call jframe's update.
class MainRadar(
....
private FrameRadar mFrame;
....
public StateMainGame() {
init();
}
public void init() {
....
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
mFrame = new FrameRadar();
mFrame.setVisible(true);
}
});
}
....
public void update() {
mFrame.update();
}
So as you see, MainRadar.Update-> FrameRadar->Update-> Jpanels->Update. The problem is that i cannot use
mFrame.update();
I have read about that when programming Java Swing, we should put these components into Java Event Queue, because Java Swing thread is not-thread safe.
But, when I use Event Queue, I don't know how to update component properties (for example : set text for label or change something..). Here is my code :
public class SwingExample {
private JLabel lblLabel;
SwingExample(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
lblLabel = new JLabel("Hello, world!", JLabel.CENTER);
frame.getContentPane().add(lblLabel); // adds to CENTER
frame.setSize(200, 150);
frame.setVisible(true);
}
public void setLabel(){
lblLabel.setText("Bye Bye !!!");
}
public static void main(String[] args) throws Exception
{
SwingExample example = null;
EventQueue.invokeLater(new Runnable()
{
public void run()
{
example = new SwingExample(); // ERROR : Cannot refer to non-final variable inside an inner class defined in different method
}
});
// sometime in the futures, i want to update label, so i will call this method...
example.setLabel();
}
}
I know, if I write SwingExample example = new SwingExample(); the error won't appear again, but if i use that, I cannot process example.setLabel later.
Please tell me about this error and how to fix this.
Thanks :)
By having your SwingExample instance as a field, you can reference it inside the inner classes without it being final.
public class SwingExample {
private JLabel lblLabel;
private static SwingExample instance;
SwingExample() {
// code omitted
}
public void setLabel() {
lblLabel.setText("Bye Bye !!!");
}
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
instance = new SwingExample();
}
});
// ...
EventQueue.invokeLater(new Runnable() {
public void run() {
instance.setLabel();
}
});
}
}