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();
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 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.
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();
}
});
}
}
Class1 which is a NewJFrame Form and is also a calling class to class2, NewJFrame3 has a Jbutton component, jButton1, for which method actionPerformed has been over-ridden. I call the object with a thread. On calling, Class2, NewJFrame3, a new frame pops up. Problem is when I press the default cross of called class, NewJFrame3, button [X], both screens gets cancelled. I was not using threads before and was calling the layout method for JFrame3 with just object, but it had this problem, so i used thread but it wouldnt work.
Code of calling class (Layout is fine for this and on clicking its button JButton1, class NewJframe3’s layout pops up) :
public class NewJFrame extends javax.swing.JFrame implements ActionListener
{
public NewJFrame()
{
initComponents();
jButton1.addActionListener(this);
}
private void initComponents()
{
//layout code for JFrame1 – gives the desired layout/output
}
public void actionPerformed(ActionEvent e)
{
If(some_condition)
{
NewJFrame3 obj2 = new NewJFrame3();
Thread th1 = new Thread(obj2, "thread1");
th1.start();
}
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new NewJFrame().setVisible(true);
}
});
}//Main ends
}//Calling Class NewJFrame1 ends
Code for called class(same package) :
public class NewJFrame3 extends javax.swing.JFrame implements Runnable
{
public void run()
{
// System.out.println("Inside Run");
this.test();
this.setVisible(true);
}
protected void test()
{
//layout code for JFrame3 – gives the desired layout/output
}
PSVM()
{
//Main method code not relevant here, since layout function is called from run() which gets called on starting thread for this class.
}
} //Called Class NewJFrame3 end
Thanks a ton for your time!
you have to set for defaultCloseOperation, set to DISPOSE_ON_CLOSE in this case, last of JFrames to terminating an Current JVM
your current issue is default value HIDE_ON_CLOSE, then current JVM is still running, consume and increase RAM in PC
use CardLayout or JDialog with parent, and/or setModal/ ModalityTypes instead of two JFrames
I have two classes, and from the first jFrame1 (CotizacionGUI) I instantiate and make visible the other one (jFrame2), and I want to pass the instance of this jFrame1 (CotizacionGUI), to the other, in the constructor, to dispose it in an action triggered by the button at any moment...
public class CotizacionGUI extends javax.swing.JFrame{
public CotizacionGUI() {
initComponents();
}
private void buttonCallFrame2ActionPerformed(java.awt.event.ActionEvent evt) {
BuscarCotizacionGUI bC = new BuscarCotizacionGUI(thisjFrameinstance);
bC.setVisible();
}
}
And this is the Frame2 (BuscarCotizacionGUI), here is where I want to dispose the previous jFrame, triggered by the action performed event:
public class BuscarCotizacionGUI extends javax.swing.JFrame {
public BuscarCotizacionGUI(final JFrame otherFrame) {
initComponents();
this.setLocationRelativeTo(null);
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
otherFrame.dispose();
}
});
}
}
Can you help me guys please, I don't want to do it using other class, i want to pass the reference in the jFrame1, Thanks!
The instance of first JFrame is always available to you in the same class as this
public class CotizacionGUI extends javax.swing.JFrame{
public CotizacionGUI() {
initComponents();
}
private void buttonCallFrame2ActionPerformed(java.awt.event.ActionEvent evt) {
BuscarCotizacionGUI bC = new BuscarCotizacionGUI(this);
bC.setVisible();
}
}
Hope this is what you are looking for.
Good luck.