Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Saturday, July 16, 2022

Turning a JFrame into a JDialog

 I recently had to turn a JFrame into a JDialog. This means transforming a regular window into a dialog. In turn, this means blocking the main application window until the user does something in the newly created dialog (also known as "modal dialog"). It's actually quite easy to do. Even though at first I was tempted to start creating a new dialog from scratch, it turns out it's just a simple change in a few lines of code.

Here is an extract from my JFrame code:

JFrame mainFrame = new JFrame("Settings");

buildContent();

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainFrame.pack();

mainFrame.setSize(1200, 900);

mainFrame.setLocationRelativeTo(null);

mainFrame.setVisible(true);


And how it looks like after conversion to JDialog (I only changed 2 lines of code, those in red):

JDialog mainFrame = new JDialog(this.appFrame,"Settings",true);

buildContent();


//mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainFrame.pack();

mainFrame.setSize(1200, 900);

mainFrame.setLocationRelativeTo(null);

mainFrame.setVisible(true);


You probably want to avoid JFrame.EXIT_ON_CLOSE but this is how it was implemented. You may also want to do some other checks, like making sure appFrame is indeed set to the main application frame. And maybe you don't want the dialog to be that large on the screen. But for a quick JFrame to JDialog conversion, this should work ok.

 

Tuesday, June 21, 2022

Quickly run log4j2 without properties files

Sometimes I just want to use log4j without a properties file and log everything to console. It seems that especially for this reason there exists the DefaultConfiguration class. This applies to log4j2.


import org.apache.logging.log4j.Level;

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

import org.apache.logging.log4j.core.config.Configurator;

import org.apache.logging.log4j.core.config.DefaultConfiguration;

public static void main(String... aArgs)  {
Configurator.initialize(new DefaultConfiguration());
Configurator.setRootLevel(Level.DEBUG);
 

 

A game jam

I recently (the last weekend) developed a game for a "game jam" hosted on ItchIO. For those new to this term, the "game jam...