Skip to content

Archive

Category: Java

1. Where to start after downloading the SDKs?

Having a standalone project manager is a good way to invite developers in your mobile world.

So I downloaded the SDKs (see my previous article) and now I’m trying to find out how fast I can get ‘hello world’ working on a simulator. Here’s what I got so far.

  • Nokia: there’s a getting-started section on the Nokia site. This is a step by step thing which would be great if I wanted to use NetBeans. I don’t know which IDE I’ll use. I guess I’ll download NetBeans to keep it simple, stupid. Over slow bandwidth, I’ll be waiting.
  • Sony Ericsson – can’t find a getting started section online.
  • Samsung… Here they have a getting started document that’s not really hard to find, and, apparently, a NetBeans/Eclipse free project wizard. Now, that seems easy enough.

After literally 10 minutes, I got HelloWorld running in a Samsung device simulator.

2. Hello 3D world?

It’s nice to show how to perform I/O operations. It’s even nicer when we know where to find the code samples.

So I decided to stay with the Samsung SDK for now, and try to get 3D stuff running in the simulator.

JSR184 Mobile 3D Graphics API is a tutorial with source code, explaining how to make a 3D app with JME.

It’s a pity the archive linked from the article only provides a .jad file, not the source (plus I run out of luck trying to run this directly.

Never mind, let’s see what we can do with this.

It turned out that the example fits in one file, with a bunch of inner classes. This failed gracefully, throwing a URI exception.

I uncommented a few lines just to get a clean run. Nice black screen. Then I poked around, and finally I got my hands on all code samples, neatly bundled with the SDK (yea, all the nice examples pop up when you choose, ‘Open Project’ in the Samsung SDK tool, but the tutorial doesn’t say that.

I kind of liked the so called Pogoroo example. It’s not very far from the kind of data I want to display, and blender has an .m3g exporter. So I guess I’ll need to slow down a bit and actually learn something.

Classes to import
javax.swing
--JTextPane
javax.swing.text
--StyledDocument
--DefaultStyleDocument
--Style
--StyleContext
--StyleConstants

Text and style support in Swing is elaborate and complex. The Swing tutorial provides an example for creating editable styled text, but glosses over the details. In this tutorial I explain the basic classes and methods used to style editable text using java and Swing. This tutorial just shows how to create styles and apply them to a text document. If you want to know how to apply text styles interactively, well… just ask me.

1) Creating a styled document and a text pane

You may have used JTextArea before. Unfortunately JTextArea supports only plain text; another approach may consist in using HTML with a JTextPane to style our document, but results when editing an HTML styled document may be ‘surprising’.

The approach explained here uses JTextPane and StyledDocument. The styled document contains the actual text data, linked with logical styles. The JTextPane is used to display and edit our styled document.

StyledDocument doc=new DefaultStyledDocument();
JTextPane text=new JTextPane(doc); 

2) Defining logical styles

Before we apply logical styles, we need to define them. Swing allows defining logical style hierarchies similar to what you can do using OpenOffice or Word. The base style is obtained using a special technique:

Style base = StyleContext.
  getDefaultStyleContext().
    getStyle(StyleContext.DEFAULT_STYLE);

Since Style is an interface, we never create style instances directly. Derived styles are factored indirectly from our StyledDocument instance.

Style emphasis = doc.addStyle(base,"emphasis");
StyledConstants.setItalic(emphasis,true);

Style doesn’t define standard style properties and attributes. To configure a style, we need to use StyledConstants. Fortunately, StyledConstants provides many many plain english methods, including font, font size, italic, bold, associating icons with style, etc…

3) Adding styled text and modifying text styles

We can insert a styled fragment directly in a document, as follows:

int offset=0;
String str="hello world";
Style em=doc.getStyle("emphasis")
doc.insertString(offset,str,em);

Most of the time, however, you’ll want to modify the style of an existing part of the document.

int offset=o;
int length=5;
Style em=doc.getStyle("emphasis");
boolean replace=true;
doc.setCharacterAttributes(offset,length,em,replace);

There’s not really a method to remove styles; instead, invoke setCharacterAttributes as above, passing the default style (in this example, base) as argument.

That’s it. Now we know how to created styled documents using Swing.