How to Create a Window

What’s the code that libraries use to create an application window (for Windows)?

That’s a bit of an open question… got anything more specific in mind?

1 Like


Swing (JFrame class) and the JavaFX screen graph come to mind. But also there are libraries that make use of OpenGL (e.g., LWJGL, JOGL, but I don’t know the specific classes in these that handle the top-level Window container. I’m not clear what you are trying to determine.

1 Like

The most important Win32 API function is CreateWindowExW.


Well I’ve already tried searching through the createWindowEx method in another code and didn’t find anything, anything specific? Where’s the method declaration?

It’s a Win32 API function. So, in windows.h. We are talking about platform APIs functions here and not Java “methods”.
And it’s still completely unclear what you actually want to know.

1 Like


Basically I want to know how libraries create blank windows which you can do stuff on.

I also have a few more questions I just thought of :

  1. How would I edit pixels on the window? Is it possible to do this with just the pixel’s position and the ARGB values?

  2. How would I allow inputs such as the Left Click or the keyboard character A?

So how do I call this function? Simply typing it into my java program doesn’t seem to work.

“blank windows you can do stuff on”
Do these qualify?

Part of core Java:

From JavaFX

1 Like

Yes! This is exactly what I want to remake. Can you show me how?

Sure, can show some basics. But I’m a little wary about jumping in when it seems like you might be new to Java and not fully up on the differences between the various choices. It would be very helpful to know a bit more about your goal.

For example, if your idea of “do stuff” includes the display of Controls, like labels or text fields or drop downs, that might make the basic screen graph of JavaFX or the JPanel of Swing a much better choice than either of the Canvas choices.

If you are using Java 9 or later, that also has implications as JavaFX was split off from core Java. So, even though JavaFX has many advantages over Swing (imho, opinions vary), using it will make compilation and packaging more complicated, to the extent that you’d probably want to also make use of Maven–yet another thing to learn about. Fortunately, IDE integration with Maven has made it fairly easy for the most common purposes.

Sorry to get into all this hoo-haw for a simple request.

This page points to the many options. Client Technologies

Using the AWT library: 2D Graphics Tutorial
Using the Swing library: Creating a GUI with Swing
Using the JavaFX library: Basics of JavaFX (learning to use the Scene Graph–which includes drawing images, shapes and placing controls)
JavaFX Canvas tutorial

This doesn’t scratch the surface of libraries that make use of OpenGL, of which there are also a number of choices.

So, to restate, we can probably help you navigate through this if you give us more info about your goal. But I’m also hoping that in showing you these links, it will be easier for you to figure out the terms to use for searching for tutorials on whichever approach you want to take. Our site has many tutorials written by members, but there are also many tutorials and examples in the web.

1 Like

I want to know how :

  • Windows are made, including :

    • A display in which I am able to change the pixels displayed based on position and ARGB color
    • A general window decoration with a minimize, maximize, and close button
  • Files are loaded, including :

    • A way to get the position and ARGB values of each pixel of a .png
    • A way to play the audio of a .mp3
  • Controls are inputted, including :

    • A mouse, with its left click, right click, scroll wheel, and whatever you call the thing that changes the cursor’s position
    • A keyboard, with all its keys

I am slammed, maybe others will also help out here?

First step is to work through a beginning guide to Swing or JavaFX. This tutorial by Nam H Minh looks good to me: Java Swing Hello World Tutorial for Beginners Using Text Editor I’ve had a lot of positive results working with Nam’s tutorials, and am currently enrolled in a Spring Boot course of his on Udemy. This tutorial gives you the basic window display. It is written so that all you need is a simple text editing program. But it should be easily adaptable to an IDE like Eclipse, IntelliJ or NetBeans. Once you have that done, I or someone else here will show you how to handle displaying graphics that can be modified at the pixel level, and most importantly, how to set up a game loop.

Alternatively, I wrote a beginning level guide that covers most of your topics, using JavaFX. But this would not work well unless you have Eclipse and Java 8. It would be better to use a more current version of Java. It might be easier to just start out with Swing and Java 2D. Getting something meaningful out of this tutorial probably requires commitment to an IDE and Maven (and me rewriting the first part, or someone else helping with that).

I should have a bit more time for further questions tomorrow. I’ll definitely have something to say to you about audio (my main expertise).

This covers none of my topics

I’ve already setup a game loop, I made my own Timer class utilizing System.currentTimeMillis() and instructed my application to start a looping timer that runs my game class’s loopGame() method.

I was teaching myself JavaFX for about 3 months. I think I have a pretty good grasp on how both Swing and JavaFX work.

It seems your replacing my questions with something else; I want to know not how to use JavaFX, but how to make components used in JavaFX.

Thanks for clarifying. You would have saved me a great deal of trouble if you had been clearer about this from the start.

The JavaFX tutorial I cited has sections that cover keyboard and mouse events.

JavaFX classes for image editing: WritableImage, PixelReader, PixelWriter.

AFAIK, both AudioClip and Media in JavaFX are able to use mp3 files as an input.
If you already have experience using JavaFX, using a Canvas should be clear enough from the API.

I’m not using JavaFX for this.

(So uh did this forum just abandon BBCode or something why isn’t my rainbow text working)

If I did actually need help on JavaFX, I could easily just search up on Google “how to create a window” and I’d get my answer.

The reason why you are not getting an answer here is that you haven’t actually asked a question, or at least not one that comes anywhere close to narrowing down enough what it is that you are looking for.

Your initial question gave us absolutely no clue about where you stand and what you do know so far, whether you are a complete beginner trying to find footing or whether you are an experienced developer just looking for a specific API call. The fact that you did ask the question like that leads most people to assume that the former is true; because asking questions is a learned skill.

The way your question sounds to me, it seems as if you are assuming that libraries like JavaFX or Swing simply have a few lines of code somewhere that do the actual window handling stuff, and that the purpose of the libraries is to add those components on top.

That is not true. The libraries you do interact with are themselves built on top of other libraries/APIs, which in turn are built and ineract with yet another sub-level of APIs and libraries.

@KaiHH told you that it’s a Win32 API function.

Assuming (from your reply) that you do no know what that means:
It means you need access to a native API. This is normally not doable within a simple Java project. You will have to use something like JNI to do that - and even then, it will only work on Windows, not on other operating systems, because it is a Windows-only API.

If you are hoping to get a way to interact with windows that is simpler, or more basic than simply using JFX or Swing, then by going this way you are out of luck. Those libraries exist for a reason. Manipulating pixels on a screen is not a simple as you seem to think it is.

If you really do want to go deeper into this, try looking into APIs like OpenGL and GLFW. But it won’t be easier. It will, in fact, be much more complex.

You gave no indication on whether or not you are aware of it, so I’ma also point you towards Java2D. If all you want to do is manipulate some pixels, this might be what you are looking for.

Also… why the hell would you even want to use “rainbow text”?