简体   繁体   中英

Mixing Swing and JavaFX in a Java desktop chat application

I know that there are already some question about this, but I can't find my way!

I want to implement a desktop chat application with java which is able to send text,image, video, etc. Now I am using swing component for my chat conversation window. I create a JFrame and add JTabbedPane to it inorder to have tab for each new conversation. for creating each tab I act as follow :

  • create JPanel (I add this to my JTabbedPane as tab)

     newtab = new JPanel(); \nnewtab.setLayout(new BoxLayout(newtab, BoxLayout.PAGE_AXIS)); 
  • create JTextPane for display's part of the chat (to have style for conversation like android application such as viber, ....)

I want to be able for following styling:

  • diffrent alignment
  • change font, color
  • insert JComponent (to show other type of messages )
  • setborder of each message round (I don't want the squre one)
  • ...
context = new StyleContext();
kit = new HTMLEditorKit();
chatPane = new JTextPane();
chatPane.setEditable(false);
chatPane.setContentType("text/html");
chatPane.setEditorKit(kit);
chatPane.setText("");
doc = (HTMLDocument) chatPane.getStyledDocument();
CSS(); // it is for adding ccs style to stylesheet of document
JScrollPane scroll = new JScrollPane(chatPane);
newtab.add(scroll , BorderLayout.CENTER);

My problem is to set perfect stying to my display part, since javax.swing.text.html.CSS provides HTML 3.2 support, so the CSS properties that are supported are limited! while searching on Internet I find JavaFX , but I don't know is it good to use JavaFX and swing together or even is it possible?! also which layout manager is better for the JPanel ( newtab ) to have my JTextPane with scroll.

It IS possible to mix JavaFX and Swing. But I have no experience in it. I just did a little FX-UI for a small project which was pretty nice. Especially the CSS-Feature is great.

According to mixing, I just recently found a blog which discouraged mixing both technologies: http://dlemmermann.wordpress.com/2014/07/17/javafx-tip-9-do-not-mix-swing-javafx/

Maybe this gives you a little help.

As its already been said, it is possible to mix Swing and JavaFX especially since Java 8 you can do it both ways:

  • Embed Swing Components in JavaFX with SwingNode
  • Embed JavaFX Components in Swing with JFXPanel

Recently we had to make the same decision. We had an application and wanted to migrate to JavaFX to get a more modern design and to make use of all the introduced language features like PropertiesBindings etc., which is nicely supported by JavaFX Components.

So first we tried to embed JavaFX in Swing. All new components were embedded with the help of JFXPanel. It was really easy, but from time to time we had some rendering issues which got more and more annoying. "Unfortunatly" we got used to the new JavaFX API, which is why we deceided redesigning our appliction to make it a JavaFX application with some Swing Parts in it, which was possible, when Java 8 was released, since we didn`t wonna waste time on fixing thoses kind of rendering issues. The redesign was actually some work since some concepts are just different. Benefitting from the new API caused some refactorings, we didnt really wonna do in first place.

But then again mixing Swing and JavaFX got a bit fuzzy, and the look and feel of the application didnt really feel convincing, so then we finally removed all Swing Parts and replaced them by JavaFX Components. So far we don`t regret that step, but it was more work then we expected it to be, eventhough we already used patterns like MVP, where only Views had to be refactored, since presenters were (mostly) free from UI stuff (which was really an interesting process, were we learned a lot about MVP and designing an application).

So in conclusion I just can suggest to create a list of views you have and think of all the components you would need and try to find the corresponding components in JavaFX. Make small examples for the most complex components to see if they fullfill all your usecases. If that is the case and you still have enough time to switch to JavaFX I personally would go for a pure JavaFX approach, because of the experiences I made with mixing JavaFX/Swing, especially since your UI design seems to be in an early state. In the end it is just a question of time you have available for your project and if you are really up to learn about the new concepts and components of JavaFX.

Concerning the JavaFX CSS Support, you find a reference here .

Recently I was asked same question. About one month ago I started new project with my team. We use Java 8.0 + JavaFX 2.2. What problems did I find in JavaFX?

  • It's new technology, so many issues still not answered. And you must look for it own.
  • No tray supporting, so you must use java.awt.SystemTray .

Also I found one problem in the design.
For example you want to make beautiful list with cells which contains label which stuck to left side and checkbox about right side. But there is no good way to do it and you have to calculate length of cells and etc..

But JavaFX provide great opportunities for cutomizing GUI. And you can incapsulate your design in jxml file. It's very convient, because it even more separate code from design.

About mixing I think that if platform allows it than you have to use a solution that provided a platform.

And I think that the decision to use JavaFX correct, if only because it is a relatively new technology, developed by Oracle and it probably will soon replace the swing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM