#Waiting for another GUI to finish

365 messages · Page 1 of 1 (latest)

viscid steeple
#

Hey all,
From one GUI class, I am calling another one, however, it does not wait for the newly made GUI to finish before running the code afterwards, which is an issue as I am retrieving values from the newly made window. My code:

            EditingQuestion editQuestionGUI = new EditingQuestion(id,question,answer);

            System.out.println(editQuestionGUI.new_question);
            System.out.println(editQuestionGUI.new_answer);

The EditingQuestion class is a class which creates a new JFrame window, but it doesn't wait for the editQuestionGUI to finish being used and it only prints null. Any ideas on how to fix this?
Thanks.

coral nimbusBOT
#

Hey, @viscid steeple!
Please remember to /close this post once your question has been answered!

wind cobalt
#

what are tou planning to do?

viscid steeple
#

I would like the code to pause while the editQuestionGUI is still being displayed, and then continue once it has been closed/done with

wind cobalt
#

and what do you want to happen if it's closed?

viscid steeple
#

for the code to continue running

wind cobalt
#

I see you will need while loop

#

I recommend using it on another thread

viscid steeple
#

Would it be possible for you to elaborate?

wind cobalt
#

on phone? not really

#

I may tell you what to do

viscid steeple
#

Well I don't understand what you mean by "while loop", it doesn't really tell me much .-.

wind cobalt
#

you dont know what it is?

#

while () {

viscid steeple
#

yeah it's an infinite loop, no?

wind cobalt
#

it CAN be infinite loop

#

but doesnt have to

#

alright

#

I'll tell you what to do

#

if you want

viscid steeple
#

Uhh, sure xd

wind cobalt
#
  1. create class
  2. implement Runnable
  3. implement method (run)
  4. put while (true) loop there
  5. tell me if done
#

do you understand everything?

#

for now

viscid steeple
wind cobalt
#

put true inside while brackets please

viscid steeple
#

i have

wind cobalt
#

while (true)

viscid steeple
#

i know i know, i just did it after screenshotting haha

wind cobalt
#

alright

#

now create field

viscid steeple
#

field?

wind cobalt
#

private final JFrame frame;

#

field

viscid steeple
#

oh ok

wind cobalt
#

then constructor

#

ALT + INSERT

#

Select constructor

#

and with JFrame parameter

viscid steeple
#

ok now this is where im losing u

viscid steeple
#

why would i need that here

wind cobalt
#

to check if frame is being opened?

viscid steeple
#

oh you want me to create the frame in this class?

wind cobalt
#

no

#

to get the frame

#

from class where you create it

viscid steeple
#

oh

#

ok

#

I'm lost now...

wind cobalt
#

just do what I type

#

have you created field?

#

or you know what? fuck it

#

I will make it simplier

viscid steeple
#

🤦‍♂️

wind cobalt
#

you vhoose

#

seperate class

#

or

#

in same class

viscid steeple
#

I would prefer to test it in a seperate class before changing things i've already amde

wind cobalt
#

alright

#

private final JFrame frame;

#

public HaltCodeWhileGUI(JFrame frame) {
this.frame = frame;
}

#

done?

viscid steeple
#

yes

wind cobalt
#

send ss

#

to make sure

viscid steeple
wind cobalt
#

alright its good

#

inside while loop

#

put if statement

#

if (frame.isIconified()) { (im not sure how to type that)

#

intellij should tell you

#

is there method like that?

viscid steeple
#

I'm not seeing something like that

wind cobalt
#

then

#

frame.getExtendedState() && Frame.ICONIFIED != 0

#

inside if ofcourse

viscid steeple
#

frame.getExtendedState() returns int

#

not boolean

wind cobalt
#

so make it

#

!= 0

viscid steeple
#

0 = false 1 = true?

#

alr or that

wind cobalt
#

frame.getExtendedState() != 0

viscid steeple
#

done xd

wind cobalt
#

go back to your class

viscid steeple
#

ok

wind cobalt
#

do:
new Thread(() -> new HaltCodeWhileGUI()).start();

viscid steeple
#

alr

#

it runs with no errors, but whats meant to go in here?

wind cobalt
#

just for test

wind cobalt
#

make sure you have it

viscid steeple
#

Doesn't print anything

#

still just prints the null before the gui is finished

wind cobalt
#

my bad

#

do

#

frame.getExtendedState() == Frame.ICONIFIED

#

inside if

viscid steeple
#

still doesnt do anything..

wind cobalt
#

did you hide your frame?

viscid steeple
#

wdym by hide? like setVisible()?

wind cobalt
#

no

#

just minimalize

viscid steeple
#

no

wind cobalt
#

then run program and do it

viscid steeple
#

it doesn't do anything, the same issue 😅

wind cobalt
#

nothing is being sent in console?

viscid steeple
#

nothing

wind cobalt
#

just to make sure

#

put System.out.println("abc");

#

before

#

the if

viscid steeple
#

hm, doesn't print

#

so the while loop isnt even started

wind cobalt
#

make:
HalfCodeWhileGUI gui = new HalfCodeWhileGUI(frame);
new Thread(gui).start();

viscid steeple
#

HalfCodeWhileGUI class being what?

#

oh Halt

wind cobalt
#

wait

#

now

wind cobalt
#

btw. I edited text

viscid steeple
#

tis ok

wind cobalt
#

alright

#

so now remove the System.out... before if

#

and run again

viscid steeple
#

that doesnt work

#

if i put the print before if, it spams

#

but whe n iremove it, the error stays

wind cobalt
#

can you put another system out println

#

before if

#

but now

#

System.out.println(frame.getExtendedState());

viscid steeple
#

0

#

and only prints it once

wind cobalt
#

any errors?

viscid steeple
#

nope

wind cobalt
#

can you go to place where you create jframe?

viscid steeple
#

yes?

#

in the class of the jframe?

wind cobalt
#

can you put this line:

#

yes

#

frame.setExtendedState(Frame.ICONIFIED);

viscid steeple
#

oh

#

yeah now it owrks

wind cobalt
#

does it send

#

message?

#

like random numbers

#

384584833

viscid steeple
#

yeah it prints the stuff in the if statement

#

and stops when the gui is done

wind cobalt
#

yes

#

is that what you wanted?

#

and put whatever you want inside

#

the if

viscid steeple
#

yeah but the gui starts minimised now?

wind cobalt
viscid steeple
#

and also chnage it in the if statement?

wind cobalt
#

no

#

it should run if it is minimalized, right?

viscid steeple
#

yeah

wind cobalt
#

alright, dont forget to close ticket and I'm glad I could help

viscid steeple
#

It doesn't work xd

wind cobalt
#

what

#

gotcha

wind cobalt
#

Frame with big F

viscid steeple
#

OH

#

still doesn't work 😅

wind cobalt
#

when u changed to normal?

viscid steeple
#

so it still prints the nulls after, but when i minimise the window it will run the print lines in the while loop too

viscid steeple
wind cobalt
#

I mean no

#

in the if

#

it has to be iconified

#

but when you create frame

wind cobalt
viscid steeple
#

no

viscid steeple
wind cobalt
#

why it doesnt work

#

cause you dont minimalize window

viscid steeple
#

i dont want it minimalize, i want the user to be able to interact with the window ofdsjdfsidps

#

and then close it when theyre done

wind cobalt
#

but you wanted this code to work when window is minimalized

viscid steeple
#

uhh

#

no??

#

xD

#

I said that?

wind cobalt
#

?!??!

wind cobalt
#

and maximalize

#

or you wanted it when you close frame?

viscid steeple
#

so

#

I am creating a GUI

#

from another class

#

while that GUI is open, I don't want it to run the code below

#

I want them to be able to interact with the newly made GUI

#

and once theyre done with it, close it

#

and then it'll continue running the code below

wind cobalt
#

so now

#

when gui is closed

#

run code

#

yes?!

viscid steeple
#

yeah, when the gui has been disposed continue running code

wind cobalt
#

omg

#
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent event) {
        your code
    }
});
viscid steeple
#

that's an event for when the window closes?

#

thats still not what i need 😭

#

Look, I am creating a GUI here

#

the variables i am printing out below

#

are null

wind cobalt
#

when window is closed, run program?

viscid steeple
#

because they print immediately after the gui is displayed

wind cobalt
#

alright

#

put

viscid steeple
#

i need the code to stop there while the editQuestionGUI is displayed, but when the GUI has been disposed

#

it will continue

wind cobalt
#

synchronized(this) {
this.wait();
}

#

before system out prontln

viscid steeple
wind cobalt
#

wait

#

to get

#

when program is closed

viscid steeple
#

oh, ok

wind cobalt
#

no

#

I have new idea

#

perfect idea

viscid steeple
#

oh?

wind cobalt
#

public Runnable runnable = () -> {};

#

put this as a field

#

in frame class

#

in EditQuestion

viscid steeple
#

ok

wind cobalt
#

done?

viscid steeple
#

yus

wind cobalt
#

then

#

do this:

#

editQuestionGUI.runnable = () -> {
System.oit......
};

#

you know what to put there

#

judt move this system out println there

#

done?

viscid steeple
#

ok

wind cobalt
#

ok then now

wind cobalt
#

put

#

this.runnable.run();

#

and bang

#

ready

viscid steeple
#

nothing...

#

i added the event into the class

#

created it like this

#

still just prints the 2 nulls

wind cobalt
#

try to close it

#

to close gui

viscid steeple
#

yeah it just prints the test

#

and doesnt close

wind cobalt
#

replace: DO_NOTHING_ON_CLOSE

#

with: EXIT_ON_CLOSE

wind cobalt
viscid steeple
#

I don't think you're understanding me still 🤦‍♂️

#

so

wind cobalt
viscid steeple
wind cobalt
#

yes

#

and check

#

if works

viscid steeple
#

it doesn't work fdsojfdsiodsfkp

#

it doesn't matter, I'll just figure this out some other time

wind cobalt
#

no messages?

#

or what

viscid steeple
#

It's okay, i'll just work on other things and come back to this later

#

Thank you for your help, even tho we didnt get to a solution in the end :c

#

I appreciate it, though 🙏

wind cobalt
#

alright

#

you know what

#

I found out

#

last try

#

move your 2 System out println

wind cobalt
#

alright?

viscid steeple
#

okok

wind cobalt
#

and remove editQuestionGUI.

#

just field

#

so System.out.println(new_question);

viscid steeple
#

yes but i dont want to access it in that class, i want to access those variables where they are rn xd

wind cobalt
#

but it works?

viscid steeple
#

yes

#

because it's in the same class

wind cobalt
#

do you still have runnable?

#

@viscid steeple

#

inside this class

#

etc?

viscid steeple
#

yea

wind cobalt
#

replace it to Consumer<EditingQuestion>

#

and () -> {} to eq -> {}

#

done?

viscid steeple
#

yea

wind cobalt
#

alright

#

now go to listener method

#

and do

#

runnable.accept(this);

#

done?

viscid steeple
wind cobalt
#

do🇺

#

EditingQuestion.this

viscid steeple
#

alr

wind cobalt
#

works?

viscid steeple
#

yeaa :D

wind cobalt
#

wait

#

have you run the code?

viscid steeple
#

yea

wind cobalt
#

and works?

viscid steeple
#

i guess i'll need to keep all the code after in this runnable -.-

#

but yea it works

wind cobalt
#

how the fuck it works

#

you hasn changed it

#

yet

viscid steeple
#

😂

wind cobalt
#

but

#

try replacing editQuestionGUI with eq

#

if work, keep it, if not, undo it

viscid steeple
#

where

wind cobalt
#

inside runnabel

#

runnable*

wind cobalt
#

yes

#

and send screenshot

viscid steeple
#

it works too

viscid steeple
wind cobalt
#

of code

#

to make sure

wind cobalt
#

cause that was plan

viscid steeple
#

putting code in that runnable wont be different to having it outside a runnable tho, right? it's not slower or anything?

viscid steeple
#

alr alr

wind cobalt
#

maybe 5 milliseconds

#

but what is it?

viscid steeple
wind cobalt
#

its all ok

viscid steeple
wind cobalt
#

perfect

viscid steeple
#

so it's alr

wind cobalt
#

yea

#

close ticket and have a nice day

#

and good luck

viscid steeple
#

Thanks for helping man

#

took a while but we did it haha

wind cobalt
#

np

viscid steeple
#

Thank you again, have a good day :)

wind cobalt
#

same

#

@viscid steeple close ticket