Automatic resize in Java Swing - java

Currently I am developing a GUI using Java (Window Builder) and it looks like this so far:
http://imageshack.com/a/img843/4818/75no.png
The problem is, when I maximize it, all the graphical objects stay focused in the left upper corner: http://imageshack.com/a/img713/5349/g7iz.png
I've already made some searches in order to discover how to automatically resize and/or change the locations of these graphical objects, but I could not find anything. Sorry if the solution for this is easy, but I am still a newbie in Java.

You start by using Layout Managers.
I am still a newbie in Java.
And don't forget to click on the "trail (ie. table of contents)" link to find information about all the basic Swing features.

Related

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();
newtab.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.
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.
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.
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.

How to show GUI design view in java.class?

I'm linking a database to NetBeans now and I need to create a interface for this. But when I need to adjust the position of button, label is difficult. Is it possible that show a GUI design view that easy for me to adjust without use the JFrame component?
If I understood your question correctly, you're looking for a GUI builder for Swing.
Netbeans ships with the Matisse Swing GUI Builder which will help you build GUIs easily and quickly. Tutorials aren't uncommon (such as the official tutorial or some user created videos on YouTube).
Some folks are fine with this work flow (since it speeds up development quite a bit). Others want to use custom frameworks and have requirements to use specific layouts in which case I recommend reading about different layout managers.
Take your pick and happy development! :)
Instead of trying to design your entire application in the GUI editor, you may be able to adapt the approach shown here. This will let you focus on a single container at a time. Also, remember to backup your .form files; more here.

Converting an AWT application to SWT/JFace

I am currently toying with the idea of converting a small/medium sized project from AWT to SWT, although Swing is not totally out of the picture yet.
I was thinking about converting the main window to an SWT_AWT bridge object, but I have no idea how the semantics for this work. After that, I plan to update dialog for dialog, but not necessarily within one release. Is this possible?
Has someone done a conversion like this and can give me some hints? Is there maybe even a tutorial somewhere out there? Is there maybe even a tool that can automate parts of this? I have tried googling, but to no avail.
Update: One additional thing is: Currently, this is a netbeans project. Might be of help or not, I don't know.
We have done this quite a few times. But only because we are going from a Swing application to an Eclipse RCP application not because we like messing with things. This project will really let you know whether you've separated your controller/model code from your view code.
One suggestion is to not try and convert everything all at once. You will end up with a bunch of mixed code that doesn't work at all. You can start at converting portals. I would consider a portal anything within a Tab, Dialog, or Window, essentially self contained unit. If you have a window that opens up, create the Window in SWT, but make it's contents the existing AWT/Swing. This should be fairly straight forward and allow you to get used to the (I really hope they weren't drunk and had a good reason for this) way of instantiating and associating parent/child controls.
One gotcha that can occur is with transparent components. Swing, with the exception of a "window" class is all rendered in Java. This makes it very easy to render things the way you want them. In SWT, there are some restrictions:
Borders. If you use SWT.BORDER you are stuck with whatever color the native component uses. Your best bet is to use a PaintListener and render your own borders if you want them in a different style or color.
Transparent labels, progress bars. I have not been able to get Labels or Progress Bars to have a transparent background. If you want them to take on the parent color, or drawing you will need to render the text and other controls yourself.
Controls. There are composites and controls in SWT. Think of Controls as the basic native controls that do all the native API calls. These cannot be subclassed, which makes things difficult.
Tables will give you the most trouble. Make sure everything is stable before you attempt to convert a JTable to a Table or TableViewer. You will spend some time on these, especially if you have custom editors and viewers.
I have not researched why SWT was designed the way it was. I am guessing there HAD to be a good reason. It would be great if someone had a blog or defense to it's design so I don't have to search for it. Once it's posted I'll remove these lines since they have no relevance to the question.
Addition
I want to add that since you have an existing product I assume works. The best piece of advice I can give you is to never let your code get into a state that it cannot compile and run. If you work on your conversion and whatever you check in always runs and executes (despite the visual differences between SWT/AWT/Swing) you will save yourself many headaches in the long run. The worst thing you can do is try to tackle this all at once and get your code in an unstable state for weeks at a time.
I would suggest importing it into a WindowBuilder project, as WindowBuilder gives you the ability to parse existing code and create a GUI mock-up, then morph components to either SWT or Swing.
If you're thinking of using a mix of SWT and Swing in the same application, this Eclipse Corner Article will be immensely useful.
We are preparing the same step: Swing to SWT/JFace. First we try to determine the bottlenecks: reimplement special components derived from JComponent with SWT/JFace, search for a replacement of JIDE docking (we want to use SWT/JFace, not RCP to avoid too much hassle). The worst thing we already imagine is, that in Swing you could create components and adding it later to the parent. With SWT this is not possible: the parent component must be passed as a reference to the child component's constructor. This will require major refactoring in the Swing application before using SWT.
Frankly, we rate the conversion a very heavy change, because we expect the time where nothing can be compiled as quite long. We try to decrease this time by preparing everything as good as possible, but we'll see how good it will work.
Update from April 6th 2011:
We now refactored our Swing application to always create components with their parent (as in SWT). Our subclasses of JFrame and JDialog got refactored to just have a JDialog instance to make it easier to switch to SWT's Shell. In parallel, we rewrite sophisticated components in SWT.

Turn photoshop design into Java GUI

I can't seem to find anybody who has done or posted something like this; Essentially I want to design my own UI in photoshop and then slice down the images to use it in a Java application. Essentially coding in the PSD file as the GUI. Is this possible? If so, can anybody lead me in the right direction?
I'm not sure what editor to use for this sort of stuff. I am using the Eclipse IDE and I know there is a Visual Editor but, I already have the actual design for every component in a PSD file. All I want to do is to start incorporating this into the application. Thanks.
It depends on how far your design goes. If you simply want to have normal Swing components on top of your image this is easy. Convert your PSD into (for example) PNG, create a custom JPanel subclass that loads the image and overwrite the paintComponent() method to draw the image instead of the normal background. All child components can then be set to be transparent with setOpaque(false). This puts your image into the background and puts the components float on top of it.
If you want to change how individual components look, its a lot more work. You basically need to implement a new Look&Feel for Swing. I wouldn't recommend going that route, unless you really have to, we are talking about weeks of work here, and it requires a lot of testing to really make it work properly on all platforms.
Alternately, there are already tons of custom Look&Feels available, I suggest you take a look at some freely available ones (just google "java look and feel"). Many of them can be customized to some degree (how much depends on the actual implementation, so take a close look at the source/documentation for each of them).
You might want to take a look at NetBeans which has a Swing GUI Builder. You would have to redraw your components there, and then write all the code to process the events. It is sometimes good to start with that, though often times it is less frustrating to lay them out with code by hand as it can difficult to make changes in code and have the builder keep up. There is nothing I know that will let you start from a photoshop image and proceed to building a GUI. Sounds like a good project to make someone rich. :-)

Java: Adding a User Interface to a program

If I have Java program and I need to alter it to an interface and include icons,
is there any easy I can do this and is there a good application that can help me to do it ?
or do I have to code it in myself?
Nop, /me thinks ur need 1337 mad Java programin' skillz!
Translation for the rest of the world: Sorry, you'll need to program in Java.
Added: Hey, what's with the downvotes? He started it! :P Besides - no matter if he wants to add or modify (the original text wasn't clear on this) the UI of a Java program, he will need to program in Java to bring his UI together with the code. There is no miracle tool that can allow you to draw an UI and it will suddenly do what you do.
Netbeans has a Swing GUI Builder. Quoting from their website. Let's hope this doesn't count has hidden advertising :)
Design Swing GUIs by dragging and
positioning GUI components from a
palette onto a canvas. The GUI builder
automatically takes care of the
correct spacing and alignment. Click
into JLabels, JButtons, ButtonGroups,
JTrees, JTextFields, ComboBoxes and
edit their properties directly in
place. You can use the GUI builder to
prototype GUIs right in front of
customers.
If you want to add a UI to your Java program there are tools to help you, such as the Swing GUI Builder inside of IntelliJ Idea. However, you're still going to have to write the appropriate code to hook into the UI.
It's just a website? Well depending on whether it uses CSS you might be able to just modify a .css file. This will only let you modify how the site looks as opposed to works.
See here for an example of how this technology works. However this depends on how css-dependent the website is and it's possible you may still run into some difficulties.
You want to use a Java framework to help you with the UI. For example, you can use JSF (Javaserver faces), which allows you to drag and drop components for a UI onto the site. Otherwise, you can use web programs such as Dreamweaver to design the UI, before coding the backend logic yourself in java.

Categories

Resources