Saving state of a Window in Java/SWT - java

I'm trying to build a small application using Java and SWT for the GUI. I'm looking for a way to store in some file the preferences set by the user, I mean, if the window was resized then that new size should be saved for next time the application starts. The same thing with some controls like textbox and so on.
I came up with two ideas to solve this:
Use the classic hand-made .ini files to store those preferences. But it appears to be ugly and hard to maintain.
Serialization/Deserialization of the whole class that contains the GUI. Dunno if this is even possible.
Any ideas? What should I do?
Thanks in advance.
Added:
I'm not doing an Eclipse RCP project, it's just a normal SWT application.
More:
I've already tried with serialization, but SWT widgets doesn't support it, so the only other option that I've found is create another class, say Configuration, mark it as serializable and hold there the values of my widgets. Almost like using java.util.prefs.Preferences, but not as simple.
I'm still waiting to see if there is another way to do this, maybe with some binding involved.

Have a look at Memento design pattern.
Eclipse RCP already reuses this principle, you can refer to the following interface for more information:
org.eclipse.ui.IMemento

Related

Netbeans Removing JPanel does not remove from source

I made a JTabbedPane using Netbeans 7.4. After adding a few things, I decided I didn't want all of the tabs I have created anymore. I then simply compied the JPanel inside of the JTabbedPane onto the same JFrame, and deleted the now unused JTabbedPane. Thing is, all the variables used are still being initialised in source code, even though they do not show up in the Design Navigator or on the JFrame in design mode.
I know I can simply edit this out with a external editor, but I am afraid that the Form data will be corrupted. (This has happened to me before and I lost the ability to edit using Design Mode.)
I used the inspector and FindBugs, but I failed to remove it from there.
"Why am I using Netbeans!?" - A question I won't be asking myself again after I sort this out.
Your first real problem is you are using the Design Mode. Netbeans isn't the problem as is is more than capable of working by itself with code only (I will always only use code).
It is really easy to break a program with a incorrect move within design view.
There isn't really an easy way to go about fixing it, although IMHO sift through the code and find what you need to remove. Make a copy of the .java file first so you have a backup if it breaks!

Possible ways to create console like screen in java

I am trying to create a screen like this one
Initially I was trying to port a console program to java. I have found things like jcurses and charva but there is almost no documentation on them and I really cant understand how to use them.
So I figured that I could create a console like screen that resembled the one above.
what library / framwork would I be best using. Should I use swing as it if fully portable?
what would be the best approach being that I need to be able to navigate and alter the 00 in the picture above?
Easily usable would be great but as long as it has good documentation that I can learn it from that would be fine.
(Answering as if you're looking for a hex editor.)
http://jhecomponent.sourceforge.net/
http://www.fifesoft.com/hexeditor/
http://hexedit-lib.sourceforge.net/
Played a bit with the fifesoft.com offering, kinda cool.
If you're not looking for a hex editor, can you be more specific? If you just want a cursor-addressable window you'll probably have to suck it up and figure out something like jcurses or libjcsi :)
If you prefer to create a full fledged GUI with Java you could certainly use Swing. I would prefer SWT as a matter of personal taste, the widget library of Eclipse. You will find a lot of snippets and tutorials (same for Swing). Here is the Widget Library.
Here is a discussion about SWT versus Swing.
You could use the table layout manager to create the layout shown in your screenshot. If you rewrite the application from scretch, you should be able to handle the GUI events and update the widgets according to your application needs. You will easily find articles when you search for swt and table. If you would like to keep the code base and just exchange the GUI, I 'm not sure about the best approach. Maybe, the libs jcurses and charva are the way to go.
Unless you are looking to run the app on a headless VM (where a console/tty is all you've got), I'd use Swing. Create a JTable with a custom TableModel (to provide the data) and custom renderers (to provide the hex formatting) and everything should just work. Consider deriving from the various DefaultXXX implementations to save a lot of work.

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.

Creating a "netbeans property" style dialog box

I've been trying to create a GUI using netbeans, that includes a dialog similar to Netbean's own "property dialog" that appears when designing. ie. a dialog that contains a table that can be expanded by tree nodes. Something like the dialog on this page, http://platform.netbeans.org/tutorials/nbm-property-editors.html
As far as I can understand, the page I linked to describes a custom editor I can set up for use within netbeans while designing - I'm just looking for a way to doing something similar for my actual application I'm writing. Is there an easy way to do this?
I came across this and have tried it but it doesn't quite work in the same way as the standard Netbeans dialog. weblogs.java.net/blog/timboudreau/archive/2008/06/egads_an_actual.html
Any tips greatly appreciated.
This will sound flip... but I am being serious.
The easiest way to do this is to write your app using the NetBeans RCP platform.
You may want to look at the Swing Application Framework, too.
There are a couple SAF samples integrated into NetBeans... You can read about them here: http://netbeans.org/kb/docs/java/gui-saf.html
I could not find a property editor dialog sample, though.
I found a couple other pointers, using this query: http://www.google.com/search?q=how+to+write+a+property+editor+dialog+in+swing
You could take a look into the l2fprod components
(source: l2fprod.com)

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