Java Module 18
Desktop GUIs with JavaFX (and Swing for legacy code) interview questions
Interview questions on desktop GUIs with JavaFX (and Swing for legacy code): warm-ups, then what each harder question tests and how a good answer starts.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
What are the Stage, the Scene and the scene graph in JavaFX?
-
Fresher
What is the difference between Pane and the layout panes such as HBox or GridPane?
Answered in the lesson JavaFX panes, shapes, colours, fonts and images
-
Fresher
What is the difference between bind() and bindBidirectional() in JavaFX?
Answered in the lesson JavaFX properties, binding and listeners
-
Fresher
What is the difference between an event filter and an event handler in JavaFX?
Answered in the lesson JavaFX events: handlers, mouse and keys
-
Fresher
Why do radio buttons need a ToggleGroup in JavaFX?
Answered in the lesson JavaFX controls: labels, buttons, text and lists
-
Fresher to mid
When do you use a transition, a Timeline or an AnimationTimer in JavaFX?
-
Fresher to mid
What does FXMLLoader do with fx:controller, fx:id and onAction="#name" when it loads an FXML file?
Answered in the lesson JavaFX FXML, controllers, CSS and Scene Builder
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Fresher to mid
A JavaFX desktop tool starts on your laptop, but on a colleague's computer it stops before main() with InvalidModuleDescriptorException: Unsupported major.minor version 69.0. Both use the same JavaFX 27 SDK. What happened, and how do you make the build catch it before anyone runs the tool?
- What it tests
- Reading class-file versions, knowing that each JavaFX release has a minimum JDK, and making the build fail early instead of the user's start-up.
- First step
- Read the number: class-file version 69 is Java 25, and JavaFX 27 is compiled with --release 25. The colleague's java is older, so check java -version on that computer: a Java 21 runtime reads class files only up to version 65, so it cannot even read JavaFX's module descriptors.
- Follow-ups
-
- Why does the error name javafx.fxml.jar when the tool never uses FXML?
- How do you make Maven refuse to build on an older JDK?
- The tool must also run on JDK 21. What do you change?
From the lesson JavaFX setup and your first window
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A JavaFX app reads a 200 MB catalogue file in start() before calling stage.show(), and users see nothing for eight seconds. A teammate suggests moving the loading into init(). Is that enough? Say where each part of the work belongs, and why.
- What it tests
- The Application life cycle and its threads, what init() may and may not do, and keeping the JavaFX Application Thread free so the window appears and responds.
- First step
- Name the threads first. start() runs on the JavaFX Application Thread, which also draws and handles input, so work there freezes everything. init() runs on the launcher thread before start(), so the work leaves the UI thread, but the window still appears only after init() returns.
- Follow-ups
-
- What must init() never do?
- How do you show the window at once and fill it when the data arrives?
- Where do you save unsaved work when the user quits?
From the lesson JavaFX setup and your first window
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A registration form was built in a plain Pane, with relocate() for every label and field. It looks right at 800 x 600, but at 400 pixels wide the fields run past the edge, and the longer Hindi labels overlap the fields. What do you change, and how do you prove the new layout holds at both widths?
- What it tests
- Choosing layout panes over typed positions, how a GridPane sizes columns and grows fields, and testing a layout by measuring bounds at several sizes.
- First step
- A Pane never moves its children, so nothing reacts when the window or a label changes. Replace it with a GridPane: labels in column 0, which becomes as wide as the widest label, and the fields in column 1, set to grow so that they take whatever width is left.
- Follow-ups
-
- How do you stop the fields from growing very wide on a large screen?
- How would a test check the layout without anyone looking at the screen?
- When is a plain Pane still the right choice?
From the lesson JavaFX panes, shapes, colours, fonts and images
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A logo shows when you run the app from the IDE, but in the packaged JAR its ImageView is a blank square and the log has no exception. The code is new Image("file:src/main/resources/logo.png"). Explain what happened, fix it, and make this failure impossible to miss.
- What it tests
- How Image resolves its URL, why a relative file: path depends on the starting folder, class-path resources inside a JAR, and Image's silent error reporting.
- First step
- Start from the silence: a failed Image does not throw, so check isError() and getException() first. Then compare the folder the program starts in when the IDE runs it with the folder it starts in when it runs from the JAR.
- Follow-ups
-
- Why does getResource find the file inside the JAR?
- What changes when the image loads in the background?
- How do you catch this before a release?
From the lesson JavaFX panes, shapes, colours, fonts and images
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A 9 x 9 Sudoku board must fill the window and stay square while the user resizes it. Compare a GridPane with percentage constraints, a plain Pane whose cells you place yourself, and a Canvas that you redraw. Which do you choose, and what does each cost?
- What it tests
- Trade-offs between layout panes, your own layout code and immediate-mode drawing: node counts, resizing, mouse hit testing, accessibility and redraw cost.
- First step
- Count the nodes: 81 cells of two or three nodes each is a few hundred nodes, little for the scene graph. Then pin down "square": the board's side is the smaller of the width and height it is given, and each design computes and applies that side in a different place.
- Follow-ups
-
- How do you keep the board square?
- Where does a mouse click become a cell in each design?
- When would the Canvas win?
From the lesson JavaFX panes, shapes, colours, fonts and images
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
An editor marks a document as unsaved through an InvalidationListener on a binding of the edit count; the listener only sets a dirty flag. The first edit sets the flag, but after a save clears it, later edits never set it again. Explain why, and fix it.
- What it tests
- Lazy evaluation of bindings, when invalidation events fire, and choosing between reading the value, a ChangeListener and a binding.
- First step
- Bindings are lazy: they recompute only when someone reads them. So ask who reads the binding after the first edit, and what that means for the next invalidation event.
- Follow-ups
-
- Why did the first edit work?
- Which fix would you choose, and why?
- How do you test it without a window?
From the lesson JavaFX properties, binding and listeners
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A desktop app grows by about 4 MB each time a user opens and closes a report panel, and slows down after a long day. Each panel adds a ChangeListener to the application's theme property. Diagnose the leak, prove it, and fix it so that it cannot come back.
- What it tests
- How listeners keep objects reachable, proving a leak with weak references or a heap dump, and the ways to stop listening: removal, weak listeners, subscriptions and when().
- First step
- Follow the references from the long-lived object. The theme property holds its listeners, each listener is a lambda that refers to its panel, so every closed panel stays reachable from the theme for as long as the application runs.
- Follow-ups
-
- How do you prove the leak in a test?
- Why can a WeakChangeListener stop working at once?
- How do you stop new panels from bringing it back?
From the lesson JavaFX properties, binding and listeners
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A form's Submit button may be enabled only when the name is not blank, the e-mail looks valid and the terms box is ticked. A teammate wrote three listeners that each recompute a flag and enable or disable the button. What can go wrong, and how would you rewrite and test it?
- What it tests
- Replacing listeners that copy state with one binding, building boolean expressions, the initial state, and testing a model's rules without a window.
- First step
- Look for the states the listeners miss: the state before any change, a field set by code instead of by typing, and two listeners that each decide on their own. A binding of the button's disableProperty to one boolean expression has none of these gaps.
- Follow-ups
-
- Why is the button wrong before the user types anything?
- Where does the e-mail rule belong?
- How do you test the rule without starting JavaFX?
From the lesson JavaFX properties, binding and listeners
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
In a small JavaFX game the arrow keys move a ship. Once a player clicks into the name field above the board, the arrows stop moving the ship and never work again until the game restarts. Explain the cause, and give two fixes with their trade-offs.
- What it tests
- Key events go to the focus owner, text fields use and consume arrow keys, and the choice between managing focus and using a filter.
- First step
- Ask which node receives key events: the node with the keyboard focus. Then ask where the focus went when the player clicked the name field, and what a text field does with the arrow keys it receives.
- Follow-ups
-
- Why does it never recover by itself?
- What does a filter on the root change, and what does the field lose?
- How do you test that the fix works?
From the lesson JavaFX events: handlers, mouse and keys
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Escape must close the open side panel from anywhere in the window. Inside a search field, though, the first Escape should clear the text, and only a second one should close the panel. Where do you put the code, and how do you make both rules work?
- What it tests
- Using the capturing and bubbling phases on purpose, consume(), the target and the focus owner, and letting a control keep the keys it needs.
- First step
- Decide who gets the first say. The field must see Escape before the window does, so the shortcut belongs in a handler on the scene, which runs after the field in the bubbling phase, and not in a filter, which would run before the field.
- Follow-ups
-
- What goes wrong if the shortcut is a filter on the scene?
- How does the field tell the shortcut that it used the key?
- How would you test both rules?
From the lesson JavaFX events: handlers, mouse and keys
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Your team wants automated tests of a JavaFX screen's mouse and keyboard behaviour that run on a build server without a display. What would you test with Event.fireEvent, what with the Robot, and how do you keep the tests fast and repeatable?
- What it tests
- Choosing between synthetic events and real input, the headless platform of JavaFX 26 and later, the JavaFX Application Thread, and controlling time and chance.
- First step
- Split the question by what each test must prove: that an event reaches the right node, as it would for a user, or what a handler does once the event is there. Then pick for each the cheapest way to produce the event.
- Follow-ups
-
- What must run on the JavaFX Application Thread?
- How do you keep timers and random delays out of the tests?
- What still needs a person?
From the lesson JavaFX events: handlers, mouse and keys
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A form keeps its model in step through a ComboBox's onAction handler. It works on screen, but a unit test that builds the form without showing it and calls setValue sees a model that never changes. Explain why, fix the design, and say how you would catch such gaps.
- What it tests
- Events versus properties, behaviour that depends on a control being shown, and testing the model instead of event wiring.
- First step
- Compare the two situations: on screen the box sits in a shown scene, in the test it does not. Ask which notifications depend on the control being shown, and which come from its value itself.
- Follow-ups
-
- Why does a listener on valueProperty() see every change?
- What should the test exercise instead?
- Which other wiring deserves the same suspicion?
From the lesson JavaFX controls: labels, buttons, text and lists
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
An enrolment-number field must hold exactly 12 digits. Compare validating only on Submit, a listener that undoes bad text, and a TextFormatter together with a validation binding. Which do you choose, and which inputs do your tests try?
- What it tests
- Where to validate, what a TextFormatter can and cannot do, messages as bindings, and the edge cases of text input such as pasting and text set by code.
- First step
- Separate two questions: which characters may ever appear in the field, and whether the value is complete. A TextFormatter answers the first on every change; a binding answers the second and drives both the message and the Submit button.
- Follow-ups
-
- Why not let the filter demand exactly 12 digits?
- What does the filter do with a paste, or with setText from code?
- Where does the rule live when numbers also arrive from a file?
From the lesson JavaFX controls: labels, buttons, text and lists
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A JavaFX registration form works well with a mouse, but a student who uses a screen reader and the keyboard cannot complete it. What do you check and change, and how do you keep it working in later releases?
- What it tests
- Focus order, mnemonics and labelFor, names for icon-only controls, messages that are announced, colour that is not the only signal, and testing for accessibility.
- First step
- Use the form the way the student does: keyboard only, screen reader on. Check first that Tab reaches every control in a sensible order, and that each control is announced with its label.
- Follow-ups
-
- What does setLabelFor change for a screen reader?
- How do you make an icon-only button usable?
- What can an automated check verify, and what can it not?
From the lesson JavaFX controls: labels, buttons, text and lists
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A JavaFX game moves the player 4 pixels in every AnimationTimer call. One tester says it crawls when many enemies are on screen; another, with a 120 Hz monitor, says it runs too fast. Explain both reports, fix the movement and say how you would test the fix.
- What it tests
- Per-frame against time-based movement, how JavaFX drops frames when one takes too long, limiting large time steps, and testing timing logic without waiting in real time.
- First step
- Both reports have one cause: the step is per frame, so the speed is 4 pixels times the frame rate. At 60 frames a second that is 240 pixels a second, at 120 frames 480, and when heavy frames cut the rate to 20, only 80. Give the player a speed in pixels a second and multiply it by the seconds since the last frame.
- Follow-ups
-
- Why does the player also jump after the window was hidden for a few seconds?
- How do you test the movement code without waiting in real time?
- Would a Timeline with a 16 ms key frame fix it instead?
From the lesson JavaFX animation, audio and video
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A quiz app creates a new MediaPlayer for a buzzer sound on every wrong answer. In long sessions some buzzers never sound, and the thread count and memory of the process keep rising. Explain what is happening and redesign the sound code.
- What it tests
- How a MediaPlayer holds and releases native resources, what keeps a player alive, dispose(), and when an AudioClip fits better than a MediaPlayer.
- First step
- Separate the two symptoms and follow each player's life: what still refers to a player once the handler has returned, and what calls dispose() when its sound is over? Rising threads and memory say that something is never released; silent buzzers say that something is released too early. Then ask whether a short sound needs a MediaPlayer at all.
- Follow-ups
-
- How long may a sound be and still suit an AudioClip?
- Several buzzers may overlap. Does that change the design?
- How would you show in a test that the fix works?
From the lesson JavaFX animation, audio and video
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A lecture player lets students open any audio or video file they have. Some files never start and show no message; others show a stack trace. Design how the player opens a file, explains problems in plain words and never hangs.
- What it tests
- The formats and protocols JavaFX plays, MediaException and its types, onError, waiting for READY, and messages a user can act on.
- First step
- List the moments at which opening a file can go wrong: while the Media and MediaPlayer objects are created, later while the media loads or plays, and the case in which no event arrives at all. Then decide for each moment what the user should see and how long the player may wait before it gives up.
- Follow-ups
-
- What would you show for MEDIA_UNSUPPORTED and for MEDIA_UNAVAILABLE?
- How long should the READY timeout be for a local file, and for a URL?
- Should the app convert unsupported files itself?
From the lesson JavaFX animation, audio and video
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A JavaFX app with FXML screens runs from the IDE on the class path. Packaged as a named module, it fails when the first screen loads. A colleague proposes making every @FXML field and handler public. Explain the errors you can expect, the right fix, and why the proposal is wrong.
- What it tests
- How the module system restricts reflection, the difference between exports and opens, qualified opens to javafx.fxml, and reading LoadException chains.
- First step
- On the class path everything is reachable, so the screens worked. In a named module, FXMLLoader in the module javafx.fxml must first create the controller, which needs the package exported to it, and then set private fields, which needs the package opened to it. Read the cause at the bottom of the LoadException to see which step failed.
- Follow-ups
-
- The controllers live in three packages. What goes into module-info.java?
- Does the Application subclass need anything too?
- How do you catch this before release?
From the lesson JavaFX FXML, controllers, CSS and Scene Builder
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
Mid to senior
To flag an invalid field, a developer calls field.setStyle("-fx-text-fill: red") in one screen and label.setTextFill(Color.RED) in another. After a designer adds a theme stylesheet, the second one stops working and the first one breaks the dark theme. Explain the JavaFX CSS cascade and redesign the error state.
- What it tests
- The precedence of user agent stylesheets, values set in code, scene and parent stylesheets and inline styles, !important, and pseudo-classes as the bridge between code and styles.
- First step
- Find every source that sets the text colour of those two controls: the theme's stylesheet, Modena, the code and the inline style. Then look up how JavaFX ranks these sources against each other, and ask what the code is really trying to say: a colour, or a state of the field.
- Follow-ups
-
- Where do the colours of the error state live after the redesign?
- How would you test that the error state is visible in both themes?
- When is setStyle acceptable?
From the lesson JavaFX FXML, controllers, CSS and Scene Builder
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
Senior to staff
A 40-screen JavaFX app needs a light theme, a dark theme that follows the operating system, and a high-contrast theme users can pick, all without changing screen code. Design where colours live, how themes switch, and how you keep every theme readable as screens change.
- What it tests
- Looked-up colours and naming, media queries and conditional imports, Scene.Preferences, contrast requirements, and automated checks that scale to many screens.
- First step
- Separate naming from use. Every rule uses colour names such as -ink, -page and -brand, defined once in .root; each theme only redefines the names. Then decide the switch: the operating system's scheme through a media query, and a user's high-contrast choice through a style class on the root or an extra stylesheet.
- Follow-ups
-
- How do you stop a new screen from hard-coding a colour?
- The app must still run on JavaFX 21. What changes?
- How do you check contrast on 40 screens in both themes?
From the lesson JavaFX FXML, controllers, CSS and Scene Builder
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes