HTML Tag in Java Swing Components - java

Does adding HTML tags to Swing components, for example JLabels, make rendering of JFrame slow?
I mean how is the performance of HTML rendering in Swing components?

Yes, depending on the complexity of your html.
Actually when you have html in your jlabel, the BasicLabelUI uses a View to paint the label instead of simple paint logic. Now you can check various different implementations of View class to check how it affects you.
You can read the code of BasicLabelUI.paint() for clarity.
But for simple decorated html, I don't think you need to worry at all.

Swing's HTML rendering support uses many classes, therefore users on older systems might notice a delay the first time a component with HTML formatting is shown. One way to avoid this delay is not to show the HTML-formatted component immediately and to create it (or another component that uses HTML) on a background thread.

Related

Multiple Page Development in Java with Eclipse and GWT

I have been writing some basic code for an application I am designing. I have learned the basics and gotten some simple database connection working with RPC calls etc. What I need to do now and am completely lost (as I am traditionally a c# developer with windows forms).
In c# if I wanted a new form I would just create it, and then call the show method.
How does one create multiple pages in GWT, and switch between them?
Thanks in advance,
Chris
The simplest way would be to
Make a new java class (GwtHome.java, GwtHelp.java etc)
Extend these classes by using the Composite class
Make the equivalent of a Master Page and add it to the rootPanel as a class with the appropriate headers, menu, footer and Content Placeholder (Could be any of the AbsolutePanel, VerticalPanel, HorizontalPanel objects provided by the GWT Framework)
By clicking on the menu clear the Placeholder and add the appropriate object of GwtHome, GwtHelp etc.
After getting aquanted with the above procedure, you might want to break up the code in many files using a design pattern as suggested by Andrei.
Simply clear the root panel (RootPanel.get().clear()) and add the widget for your new "page", the same way you added your first one.
If you're using LayoutPanels, do RootLayoutPanel.get().clear() instead.
Look at Activities and Places design pattern: https://developers.google.com/web-toolkit/doc/latest/DevGuideMvpActivitiesAndPlaces
I highly recommend it for a multipage GWT app. It explains pretty well how you create different "views", that are driven by their "activities", and tied to specific "places" (pages) that users can navigate.
Typically you use a LayoutPanel as your "page" container that occupies the entire available browser window. You split this LayoutPanel into 2-3 layers (zones), like top menu, side menu, main area. Each area contains one widget, usually a ScrollPanel, FlowPanel, or HtmlPanel. Then you use different widgets or HTML inside each of these widgets to display whatever you need. You may also create your own composite widgets that you can reuse in different pages.

Make all tooltips multiline in Java (Swing)

Tooltips in my application can be quite long, therefore I'd like them to have line breaks.
I don't want to use html as I'd prefer to set a (max) width of my tooltips instead and have the line breaks dynamically.
In the accepted answer the this similar question Multi-line tooltips in Java? I read about JMultiLineToolTip. Unfortunately the provided link doesn't work anymore and there are many different JMultiLineToolTip out there. Therefore my two questions:
Which JMultiLineToolTip is a good one to use?
How can I use such a class to represent all of the tooltips in my application?
EDIT: as everyone seems to recommend the use of html, is there a way to define the width of my tooltip in pixels (or some other unit than number of characters) using html?
If you are not afraid of extending swing tool tip, you can create your own JMultiLineToolTip:
Extend JTooltip In the extended Tool tip component implementation,
set a custom tool tip UI In customUI implementation
Implementpaint() method to write given string in multi line
Here is an example - it shows how to use it as well
However, to answer your questions:
Which JMultiLineToolTip is a good one to use?
Use <html>
How can I use such a class to represent all of the tooltips in my application?
Per compopnent, it is easy but tedious to achieve as you will have to override creatreToolTip() API. But if you want to change it globally, you may:
(i)Simple way - Register your custom tooltip UI with the UIManager at the beginning of your execution.
UIManager.put( "ToolTipUI", "SeNormToolTipUI" );
UIManager.put( "SeNormToolTipUI",Class.forName( multiLineToolTipUIClassName ) );
(ii) complex way
You will have to start implementing your own look and feel. In the look and feel implementation, you would provide defaults for ToolTipUI as your UI implementation and then set that look and feel to the application you are running. For instance take a look at the MetalLookAndFeel implementation. You may just extend that part and implement your on lnf.
So, it is better to use <html>
1) Html is easiest of ways for plain JToolTip
2) use JWindow(un_decorated JDialog) with JTextArea, better would be JTextPane (supporting stylled text),
the disadvantage is you have to manually set window to the Point, you have to manually set for setInitialDelay and setDismissDelay (Swing Timer), setVisible(true/false)
the advantage is that you using full manageable top level container with definitions for own parent
3) I use JLabel with Html formatted and stylled text added to the GlassPane, notice easiest alternative is use non_opaque JLayeredPane (Java6) or JLayer (Java7)

Is there a way to layout Swing components like divs in HTML?

Let's imagine situation where you're making the layout for a webpage. In HTML you can put the stuff in divs and use CSS to set positioning, e.g. size, positioning, etc.
Let's have another situatinon, but same requirements. You're programing in Java Swing, and you also want to make layout with similar requirements, e.g. size, positioning, etc.
I haven't found layout managers useful in this situation, because they make it one way, but never as you want it, e.g. one part will be next to another part, and it will have some size.
In short: I want the layout to handle positioning and setting sizes of GUI parts as happens with divs in HTML & CSS. I'm programing it in Java Swing.
I've tried swinghtmltemplate, but know about xito try to look through these projects, maybe you will find good solution for you. And yes, there is MiG layout

The Page Turn Effect in Java Swing/AWT

I would like to implement the page turn effect in Swing/AWT. The theory is pretty much explained here. My question is that since the page is to contain text, which is the correct swing/awt component to use? Also, to get a little deeper, how should I approach this problem? I mean I have the theory laid out before me, I know the tool I need to use, but I have never done anything like this with Swing/AWT, do I need to master all the minutiae of Swing/AWT to be able to do this?
Whatever you do, you will have to implement your own drawing routines. As the text you intend to draw is going to not share most characteristics of the text drawn with other components (it will be angled, rotating over time, and clipped at a moving visible "edge" of the page), you will also have to draw the text.
In SWING, and in all non-console programs the text is drawn to the screen as would any graphics primitive. It takes a bit of learning; especially in the details of line spacing, letter spacing, word wrapping, etc.
That means I'd consider a new swing UI component. Here's how to write custom painting for one. Depending on the details, you may want to reuse a model. Generally it is easier to only write a view than a view and a model at the same time; however, you must balance the needs of the view with the suitability of the existing model.
If you choose an existing model, the Document interface is what you should code your view against. You can take your lead from the JTextPane or JTextArea view components, depeding on if you want the ability to "add style" to your text.
Don't forget that the view actually doesn't do the painting in a well structured Swing component. If it did, then it would not be able to be skinned (to follow the platform's theme). This means that while you will provide a Swing view (typically with a name pattern like JPagedBook), that view will have to be coupled to a UIDelegate which does the actual drawing of the view. This allows multiple UIDelegates for each view, with each UIDelegate containing the instructions of how to render (draw) the view in a manner that is consistent with the look-and-feel of the platform.
The biggest issues around this is typically the extra work involved to properly support look-and-feel, and the lack of knowledge of proper UIDelegate registration (so when the view draws, it finds something that will draw it). It's worth the extra few hours (or couple of days if GUI component programming is sort of new to you) to make it work like the rest of Swing. In the end you'll have a component that doesn't feel like a "separate" part of the library.

TreeViewer with a Composite as content

I want to build a Tree with a custom bunch of widgets as content. So not only a simple label, but something more complex arranged in a Composite. Is this possible in current SWT/JFace (3.7)? If yes, how do I do that? TreeViewer does only allow me to set a LabelProvider, that has only a getImage() and getText() method. Or am I limited to just that, an image and a simple label without any markup?
You aren't quite that limited -- there is CellLabelProvider, which lets you do things like draw graphics inside cell -- but as far as I know, there is no way to put arbitrary controls inside table or tree cells. This is unsurprising, because SWT generally can only do something if it is supported on all OSes and window systems where SWT is available.

Categories

Resources