Question

Firstly I want to emphasize that I'm talking about TextField in J2ME (javax.microedition.lcdui.TextField) not TextField in AWT (java.awt.TextField).

Could you tell me if this is possible in J2ME? I want to change the font (font family, font size, font color) of the TextField, I also want to change the width it is rendered on the form. I tried using setMaxSize() but this also limits the number of characters the TextField can contain. What I want is changing the TextField's width without limiting that number of characters.

If these couldn't be done, I don't think programmers can make their J2ME application look best. Or at least they would have another control which supports these features.

Was it helpful?

Solution 2

You can't change width and font of TextField in Java ME, API Javadocs for this class have nothing related to font (actually, the very word font isn't mentioned in these docs).

If these couldn't be done, I don't think programmers can make their J2ME application look best.

You are right but the reason for this limitation is that TextField and other high level UI API are intended to allow programmers write simple highly portable code (leaving details on what font to use on large/small screen to the system).

This is pretty well explained in lcdui package API docs, in section Structure of the MIDP UI API:

The high-level API is designed for business applications whose client parts run on MIDs. For these applications, portability across devices is important. To achieve this portability, the high-level API employs a high level of abstraction and provides very little control over look and feel. This abstraction is further manifested in the following ways:

  • The actual drawing to the MID's display is performed by the implementation. Applications do not define the visual appearance (e.g., shape, color, font, etc.) of the components.
  • Navigation, scrolling, and other primitive interaction is encapsulated by the implementation, and the application is not aware of these interactions.
  • Applications cannot access concrete input devices like specific individual keys.

In other words, when using the high-level API, it is assumed that the underlying implementation will do the necessary adaptation to the device's hardware and native UI style...

Regarding fonts, of high level UI API classes Choice implementations such as List and ChoiceGroup have some capabilities, but these are pretty limited, consult with API docs for details if you are interested.


For fuller access to features you look for, study the "low level" part of the API, described in the package docs referred to above:

This API is designed for applications that need precise placement and control of graphic elements, as well as access to low-level input events. Some applications also need to access special, device-specific features... Applications that program to the low-level API are not guaranteed to be portable, since the low-level API provides the means to access details that are specific to a particular device...

In particular, Graphics class has methods to get and set font - called respectively, getFont and setFont.

Note by the way that features provided by Graphics could be "embedded" within Form using CustomItem API.

OTHER TIPS

If you want to customize the components like TextField in J2ME, I strongly suggest you to use LWUIT(Light Weight User Interface Toolkit) for it. You can create good UI through LWUIT for java mobiles and customize them as you wish. As name tells, it is really light wight.

Simple Code to Customize TextField in LWUIT is as follows :

TextField myTextField = new TextField();
myTextField.getStyle().setPadding(9,9,9,9); // Top , Left , Bottom , Right padding
myTextField.getStyle().setMargin(7,7,7,7);  // Top , Left , Bottom , Right padding
myTextField.getStyle().setFont(*use the font whatever you wish*);

Note : Please see about the .RES file and Resource Editor which is every help for the change the look and feel of the application. You can import the fonts into .RES file and use it in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top