Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure spring-boot with swing application

Tags:

I'd like using spring-boot-starter-data-jpa features to create a non-web aplication. In the 52.4 documentation says:

Application code that you want to run as your business logic can be implemented as a CommandLineRunner and dropped into the context as a @Bean definition.

My AppPrincipalFrame looks like:

@Component public class AppPrincipalFrame extends JFrame implements CommandLineRunner{  private JPanel contentPane;  @Override public void run(String... arg0) throws Exception {     EventQueue.invokeLater(new Runnable() {         public void run() {             try {                 AppPrincipalFrame frame = new AppPrincipalFrame();                 frame.setVisible(true);             } catch (Exception e) {                 e.printStackTrace();             }         }     }); } 

And boot application class looks like:

@Configuration @ComponentScan @EnableAutoConfiguration public class Application {     public static void main(String[] args) {    ApplicationContext context = SpringApplication.run(Application.class, args);    AppPrincipalFrame appFrame = context.getBean(AppPrincipalFrame.class);   } } 

But does not work. Anybody have a sample about this?

Edited and exception added.

Exception in thread "main" org.springframework.beans.factory.BeanCreationException:      Error creating bean with name 'appPrincipalFrame'.  Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [es.adama.swing.ui.AppPrincipalFrame]: Constructor threw exception; nested exception is java.awt.HeadlessException  

Regards.

like image 488
Dapaldo Avatar asked Apr 04 '14 13:04

Dapaldo


People also ask

Is Java Swing still used in 2022?

JavaFX new fixes will continue to be supported on Java SE 8 through March 2022 and removed from Java SE 11. Swing and AWT will continue to be supported on Java SE 8 through at least March 2025, and on Java SE 11 (18.9 LTS) through at least September 2026.

Which is better JavaFX or swing?

In short, Swing and JavaFX are both GUI toolkits for Java programs. Swing is the old standard toolkit that features a bigger library of GUI elements and mature IDE support. JavaFX is the newer standard with a smaller library, more consistent updates, and consistent MVC support.

What is @configuration in spring boot?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.


2 Answers

It's been a while since you posted the question, but I run out with the same problem in a old project that I'm migrating and figured a different way, I think that more clear, to make the things work.

Instead of using SpringApplication.run(Application.class, args); you can use: new SpringApplicationBuilder(Main.class).headless(false).run(args); and it isn't necessary to make toy Application class extends from JFrame. Therefore the code could look like:

@Configuration @ComponentScan @EnableAutoConfiguration public class Application {       public static void main(String[] args) {     ConfigurableApplicationContext context = new SpringApplicationBuilder(Application.class).headless(false).run(args);     AppPrincipalFrame appFrame = context.getBean(AppPrincipalFrame.class); } 
like image 51
Rumal Avatar answered Nov 19 '22 16:11

Rumal


The Swing application must be placed on the Swing Event Queue. Not doing so is a serious mistake.

So the correct way to do it:

public static void main(String[] args) {      ConfigurableApplicationContext ctx = new SpringApplicationBuilder(SwingApp.class)             .headless(false).run(args);      EventQueue.invokeLater(() -> {         SwingApp ex = ctx.getBean(SwingApp.class);         ex.setVisible(true);     }); } 

In addition, we can just use the @SpringBootApplication annotation.

@SpringBootApplication public class SwingApp extends JFrame { 

See my Spring Boot Swing integration tutorial for a full working example.

like image 23
Jan Bodnar Avatar answered Nov 19 '22 15:11

Jan Bodnar