Unicode font for swing components

Hi all,
I'd like to set a Swing component (a JLabel) to a particular font so it will properly display Unicode Greek text. I've been reading all about physical vs. logical fonts, and I've read through the various Sun docs/faqs on Internationalization, Fonts, and Unicode, but I just can't get this to work. I think I'm missing something more basic here about selecting my font. My test word is the Greek "khairete," with an accent over the iota. Here is the Unicode:
\u03c7\u03b1\u1f77\u03c1\u03b5\u03c4\u03b5
The trick is that accented iota.
I can launch an xterm that shows the characters I want like this:
$ xterm -fn -misc-fixed-medium-r-normal--18-120-100-100-c-90-iso10646-1 &
That tells me that if I can get Java to use that font, then I'm all set. I'm trying the following test program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class GUI extends JFrame {
public static final String MESSAGE = "\u03c7\u03b1\u1f77\u03c1\u03b5\u03c4\u03b5";
public GUI() throws Exception {
super();
Font[] fonts;
Container container;
JLabel label;
fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (int j=0;j<fonts.length;j++) {
System.out.println(fonts[j]);
container = getContentPane();
container.setLayout(new GridLayout(0, 1));
// try with the default font
label = new JLabel(MESSAGE);
container.add(label);
printFont(label.getFont());
// see what the default font looks like when PLAIN (to compare with fonts below)
label = new JLabel(MESSAGE);
label.setFont(label.getFont().deriveFont(Font.PLAIN, 12));
container.add(label);
printFont(label.getFont());
// this looks the same as the default font, meaning we didn't find what we asked for.
label = new JLabel(MESSAGE);
label.setFont(new Font("-misc-fixed-medium-r-normal--18-120-100-100-c-90-iso10646-1", Font.PLAIN, 12));
container.add(label);
printFont(label.getFont());
// this also falls back to the default.
label = new JLabel(MESSAGE);
label.setFont(Font.getFont("-misc-fixed-medium-r-normal--18-120-100-100-c-90-iso10646-1"));
container.add(label);
printFont(label.getFont());
// this (of course) falls back to the default.
label = new JLabel(MESSAGE);
label.setFont(Font.getFont("not a font name"));
container.add(label);
printFont(label.getFont());
// try every font we've got!
for (int j = 0; j < fonts.length; j++) {
label = new JLabel(fonts[j] + ": " + MESSAGE);
label.setFont(fonts[j].deriveFont(Font.PLAIN, 12));
container.add(label);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
show();
public static void printFont(Font f) {
System.out.println(f);
if (f != null) {
public static void main(String[] args) throws Exception {
GUI me = new GUI();
But nothing in this code succeedings in printing \u1f77. As I've said, the plain old xterm can do it. I think my problem is that I haven't successfully referenced the desired font in the Java code. But how do I do this? These are the fonts available to me, according to the code above:
java.awt.Font[family=Bitstream Charter,name=Bitstream Charter,style=plain,size=1]
java.awt.Font[family=Bitstream Charter,name=Bitstream Charter Bold,style=plain,size=1]
java.awt.Font[family=Bitstream Charter,name=Bitstream Charter Bold Italic,style=plain,size=1]
java.awt.Font[family=Bitstream Charter,name=Bitstream Charter Italic,style=plain,size=1]
java.awt.Font[family=Courier,name=Courier,style=plain,size=1]
java.awt.Font[family=Courier 10 Pitch,name=Courier 10 Pitch,style=plain,size=1]
java.awt.Font[family=Courier 10 Pitch,name=Courier 10 Pitch Bold,style=plain,size=1]
java.awt.Font[family=Courier 10 Pitch,name=Courier 10 Pitch Bold Italic,style=plain,size=1]
java.awt.Font[family=Courier 10 Pitch,name=Courier 10 Pitch Italic,style=plain,size=1]
java.awt.Font[family=Courier,name=Courier Bold,style=plain,size=1]
java.awt.Font[family=Courier,name=Courier Bold Italic,style=plain,size=1]
java.awt.Font[family=Courier,name=Courier Italic,style=plain,size=1]
java.awt.Font[family=Cursor,name=Cursor,style=plain,size=1]
java.awt.Font[family=Lucida Bright,name=Lucida Bright Demibold,style=plain,size=1]
java.awt.Font[family=Lucida Bright,name=Lucida Bright Demibold Italic,style=plain,size=1]
java.awt.Font[family=Lucida Bright,name=Lucida Bright Italic,style=plain,size=1]
java.awt.Font[family=Lucida Bright,name=Lucida Bright Regular,style=plain,size=1]
java.awt.Font[family=Lucida Sans,name=Lucida Sans Demibold,style=plain,size=1]
java.awt.Font[family=Lucida Sans,name=Lucida Sans Demibold Oblique,style=plain,size=1]
java.awt.Font[family=Lucida Sans,name=Lucida Sans Oblique,style=plain,size=1]
java.awt.Font[family=Lucida Sans,name=Lucida Sans Regular,style=plain,size=1]
java.awt.Font[family=Lucida Sans Typewriter,name=Lucida Sans Typewriter Bold,style=plain,size=1]
java.awt.Font[family=Lucida Sans Typewriter,name=Lucida Sans Typewriter Bold Oblique,style=plain,size=1]
java.awt.Font[family=Lucida Sans Typewriter,name=Lucida Sans Typewriter Oblique,style=plain,size=1]
java.awt.Font[family=Lucida Sans Typewriter,name=Lucida Sans Typewriter Regular,style=plain,size=1]
java.awt.Font[family=Luxi Mono,name=Luxi Mono Bold,style=plain,size=1]
java.awt.Font[family=Luxi Mono,name=Luxi Mono Bold Oblique,style=plain,size=1]
java.awt.Font[family=Luxi Mono,name=Luxi Mono Oblique,style=plain,size=1]
java.awt.Font[family=Luxi Mono,name=Luxi Mono Regular,style=plain,size=1]
java.awt.Font[family=Luxi Sans,name=Luxi Sans Bold,style=plain,size=1]
java.awt.Font[family=Luxi Sans,name=Luxi Sans Bold Oblique,style=plain,size=1]
java.awt.Font[family=Luxi Sans,name=Luxi Sans Oblique,style=plain,size=1]
java.awt.Font[family=Luxi Sans,name=Luxi Sans Regular,style=plain,size=1]
java.awt.Font[family=Luxi Serif,name=Luxi Serif Bold,style=plain,size=1]
java.awt.Font[family=Luxi Serif,name=Luxi Serif Bold Oblique,style=plain,size=1]
java.awt.Font[family=Luxi Serif,name=Luxi Serif Oblique,style=plain,size=1]
java.awt.Font[family=Luxi Serif,name=Luxi Serif Regular,style=plain,size=1]
java.awt.Font[family=Utopia,name=Utopia Bold,style=plain,size=1]
java.awt.Font[family=Utopia,name=Utopia Bold Italic,style=plain,size=1]
java.awt.Font[family=Utopia,name=Utopia Italic,style=plain,size=1]
java.awt.Font[family=Utopia,name=Utopia Regular,style=plain,size=1]
java.awt.Font[family=dialog,name=dialog,style=plain,size=1]
java.awt.Font[family=dialog,name=dialog.bold,style=plain,size=1]
java.awt.Font[family=dialog,name=dialog.bolditalic,style=plain,size=1]
java.awt.Font[family=dialog,name=dialog.italic,style=plain,size=1]
java.awt.Font[family=dialoginput,name=dialoginput,style=plain,size=1]
java.awt.Font[family=dialoginput,name=dialoginput.bold,style=plain,size=1]
java.awt.Font[family=dialoginput,name=dialoginput.bolditalic,style=plain,size=1]
java.awt.Font[family=dialoginput,name=dialoginput.italic,style=plain,size=1]
java.awt.Font[family=monospaced,name=monospaced,style=plain,size=1]
java.awt.Font[family=monospaced,name=monospaced.bold,style=plain,size=1]
java.awt.Font[family=monospaced,name=monospaced.bolditalic,style=plain,size=1]
java.awt.Font[family=monospaced,name=monospaced.italic,style=plain,size=1]
java.awt.Font[family=sansserif,name=sansserif,style=plain,size=1]
java.awt.Font[family=sansserif,name=sansserif.bold,style=plain,size=1]
java.awt.Font[family=sansserif,name=sansserif.bolditalic,style=plain,size=1]
java.awt.Font[family=sansserif,name=sansserif.italic,style=plain,size=1]
java.awt.Font[family=serif,name=serif,style=plain,size=1]
java.awt.Font[family=serif,name=serif.bold,style=plain,size=1]
java.awt.Font[family=serif,name=serif.bolditalic,style=plain,size=1]
java.awt.Font[family=serif,name=serif.italic,style=plain,size=1]
javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=bold,size=12]
java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12]
java.awt.Font[family=dialog,name=-misc-fixed-medium-r-normal--18-120-100-100-c-90-iso10646-1,style=plain,size=12]
javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=plain,size=12]
javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=plain,size=12]
Apparently, I need to use the correct "name" to specify the font. So my next try was this:
$ xlsfonts -ll -fn -misc-fixed-medium-r-normal--18-120-100-100-c-90-iso10646-1
name: -misc-fixed-medium-r-normal--18-120-100-100-c-90-iso10646-1
direction: left to right
indexing: matrix
rows: 0x00 thru 0x30 (0 thru 48)
columns: 0x00 thru 0xff (0 thru 255)
all chars exist: no
default char: 0x0000 (0)
ascent: 14
descent: 4
font type: Character Cell
bounds: width left right asc desc attr keysym
min 9 0 0 -3 -13 0x0000
max 9 8 9 14 4 0x0000
properties: 23
FONTNAME_REGISTRY
FOUNDRY Misc
FAMILY_NAME Fixed
WEIGHT_NAME Medium
SLANT R
SETWIDTH_NAME Normal
ADD_STYLE_NAME
PIXEL_SIZE 18
POINT_SIZE 120
RESOLUTION_X 100
RESOLUTION_Y 100
SPACING C
AVERAGE_WIDTH 90
CHARSET_REGISTRY ISO10646
CHARSET_ENCODING 1
COPYRIGHT Public domain font. Share and enjoy.
XMBDFEDINFO 654
CAP_HEIGHT 10
X_HEIGHT 7
FONT -Misc-Fixed-Medium-R-Normal--18-120-100-100-C-90-ISO10646-1
WEIGHT 10
RESOLUTION 138
QUAD_WIDTH 9
name: -misc-fixed-medium-r-normal--18-120-100-100-c-90-iso10646-1
direction: left to right
indexing: matrix
rows: 0x00 thru 0x30 (0 thru 48)
columns: 0x00 thru 0xff (0 thru 255)
all chars exist: no
default char: 0x0000 (0)
ascent: 14
descent: 4
font type: Character Cell
bounds: width left right asc desc attr keysym
min 9 0 0 -3 -13 0x0000
max 9 8 9 14 4 0x0000
properties: 23
FONTNAME_REGISTRY
FOUNDRY Misc
FAMILY_NAME Fixed
WEIGHT_NAME Medium
SLANT R
SETWIDTH_NAME Normal
ADD_STYLE_NAME
PIXEL_SIZE 18
POINT_SIZE 120
RESOLUTION_X 100
RESOLUTION_Y 100
SPACING C
AVERAGE_WIDTH 90
CHARSET_REGISTRY ISO10646
CHARSET_ENCODING 1
COPYRIGHT Public domain font. Share and enjoy.
XMBDFEDINFO 654
CAP_HEIGHT 10
X_HEIGHT 7
FONT -Misc-Fixed-Medium-R-Normal--18-120-100-100-C-90-ISO10646-1
WEIGHT 10
RESOLUTION 138
QUAD_WIDTH 9
(Yes, xlsfonts prints two entries.)
Here is a bit more data:
[pjungwir@mccurdy unicode_gui]$ uname -a
Linux mccurdy.nfic.com 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT 2002 i686 i686 i386 GNU/Linux
[pjungwir@mccurdy unicode_gui]$ cat /etc/redhat-release
Red Hat Linux release 8.0 (Psyche)
[pjungwir@mccurdy unicode_gui]$ java -version
java version "1.4.2_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_01-b06)
Java HotSpot(TM) Client VM (build 1.4.2_01-b06, mixed mode)
If anyone has any suggestions, I would really appreciate it. I'd like to find an answer that doesn't involve editing the font.properties file.
Thanks,
--Paul

Hi Sojan,
first up, forget the notion of even tinkering with the font.properties file if you can. It's an old, outdated method of setting up the fonts that Sun doesn't even support any more. Use setFont() where you can, and die trying in the process! ;-)
One thing I would check is which font each AWT component currently thinks it has. It's easy enough to set the font system wide with Swing components, but I'm not sure if that capability extends to AWT components (I've certainly had trouble with it in various places in the past). While you've set up the Chinese font correctly, your AWT components might still be stuck with the Java default (Helvetica, or whatever it is, which is incapable of displaying Chinese), and hence displaying the rectangles because they don't know how to handle the foreign characters. You might need to set SimSun as the font for each of your AWT components individually at a worst-case scenario.
Hope that helps,
Martin Hughes

Similar Messages

  • GUI Builder: Where to get Icons for Swing components

    Hi,
    I am creating a GUI Builder for Swing GUIs and I am searching for icons representing Swing Components as seen in many GUI Builders like JBuilder.
    So I want to have ToggleButtons with icons in a toolbar, to let the user choose what Swing Component to create (e.g. JTextField, JList, etc.).
    Are there any free icons for Swing Components?
    Thank you very much for your help.
    Regards,
    Alex

    Hi ipooley,
    thanks for this reply but I know that page already.
    What I am searching for are icons of JComponents like JButton or JTextField etc. . You can see them for example in some GUI builders like JBuilder-Designer and they are looking as if they are Java native.
    Regards,
    Alex

  • Is it possible to create Thread for Swing Components or for JApplet?

    Can i create a Thread by extending Thread class or implementing Runnable interface for Swing Components?
    Can i create a Thread by extending Thread class or implementing Runnable interface with JApplet?
    thanks

    Does your website live on a Windows server? The above link you posted will require a Windows server for that to work but if you do, then you should be able to just follow the links instructions.
    If your site is not on a Windows server (maybe it's on Linux?) then you would most likely need a PHP solution - something like this: http://www.w3schools.com/php/php_mail.asp. Depending on your host, they might actually have a ready-built form script for you to use so it might be best to ask them first.
    You might also want some kind of validation to ensure you receive the correct information so something like jQuery Validate could work.

  • How to use stysheet for Swing components?

    Hi,
    Give me a guidence abt how to use Stylesheet for Swing components to crate good looking GUI...........thnx

    thank you for your reply,
    I want to know how many times a component is refreshed. I'm asumming the RefreshCondition attribute gets called everytime a component gets refreshed, hence it could be counted.
    I'm just not sure how to use it.

  • Order of focus for swing components

    Hi All,
    I am in search of the way to definine the order of focus for swing components.
    I have a JPanel to which I can add multiple JButtons and JLabels. My problem is that when ever JButton comes, it should get the focus even though JLabel exists.
    If I drag the JButton on to JLabel then also it should be placed on top of the JLabel.
    Can any body help me to solve this problem.
    Thanks in advance,
    Regards,
    Roja.

    Hi,
    I am giving my sample code here.
              if (textButton != null) {
                   setUserTextPresentFlag(true);
                   textButton.setFocusPainted(false);
                   textButton.setContentAreaFilled(false);
                   textButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
                   textButton.setBounds(
                        editPosX,
                        editPosX,
                        textButton.getWidth(),
                        textButton.getHeight());
                        textButton.setRequestFocusEnabled(true);
    //                    textButton.grabFocus();
                        textButton.requestFocus();
    System.out.println("requestFocus isDisplayable::"+userTextImageButton.isDisplayable()+" isEnabled "+userTextImageButton.isEnabled()+" isFocusPainted "+userTextImageButton.isFocusPainted()+" isVisible "+userTextImageButton.isVisible()+" userTextImageButton.hasFocus() "+userTextImageButton.hasFocus());
    I am getting the following values for the system.out.println values.
    isDisplayable::false isEnabled true isFocusPainted false isVisible true userTextImageButton.hasFocus() false
    I didn't find a method to set displayable to true.
    could u find where the problem is?
    Thanks in advance,
    roja.

  • Naming convention for Swing components?

    What are the official naming conventions for Swing components such as JButton, JFileChooser and so on?
    I know that "btn" is used for JButton variables. But where is a official list for all the prefixes?

    Well, if you unpack src.jar and consult the component classes you'll find how they set up their names.
    You can also call setName yourself to set a different name.
    - David

  • Skins for swing components

    HI All!
    Please tell me where I can get skins for swing components for free.

    http://www.javootoo.com/

  • How can I use two fonts in swing components?

    I want to implement a project. When a part of the JMenuBar, JButton or other swing components' text is English, I use Tahoma font, and other of these components' text is Chinese, I use SimSun font, just like the Chinese Windows OS. How can I implement it??
    Thanks!!

    Ooh, thats a tricky one. I'm not sure whether you can do that, but maybe someone else with more experienced can correct me.
    You'll probably have to create your own custom JMenu that displays two labels, one for English and one for the Chinese parts?

  • Arial Unicode font for Tamil, etc.?

    Looking for some style alternatives to the InaiMathi font included in OS X for the Tamil language, I noted at the the Gallery of Unicode Fonts that the Arial Unicode font includes Tamil characters, and remembered that that font is now included with OS X (10.5). I checked with Character Palette and indeed the Tamil characters are there, but they aren't recognized as equivalent to those in InaiMathi. And when I try to change some Tamil words in a document from InaiMathi to Arial Unicode, nothing happens.
    I don't understand a lot about Unicode, but I know the Arial Unicode font has been around for a while (it was more or less the original effort to create a single Unicode font including all the major alphabetic scripts), and don't understand why it would contain the Tamil character set but not work for that script. It doesn't seem to work for Tibetan either, though it contains that character set as well. In fact, according to Character Palette, it seems about 90% of Arial Unicode is "character variants" that "won't display correctly". What's it for, then, anyone have any idea?

    +I have always thought Windows fonts cannot be used for display. Could you describe how you use Character Palette to do that?+
    Well, not "display" in the sense of opening a file using an OpenType font on the Mac and seeing it display properly. What I do in Character Palette with an OpenType font like Sanskrit2003 is scroll through the font to find the elements I want and double-click them to enter them in a document. Of course this workaround is feasible only for the occasional word, not for whole sentences or paragraphs. I'm not a big time user of any of this, so I can manage for the occasional word I want.
    +The reason they do this is because a lot of documents and web pages could specify Arial in them and users would get the idea that OS X does not support Devanagari, Tamil, Tibetan, etc unless they switch the font, which is impossible in Safari.+
    Yeah, I can see the reason; but wouldn't it be easier in the long run to just enable OpenType fonts so everyone can use the same fonts? And it is annoying that you can't specify fonts in Safari like in Camino.
    +I agree, but have doubts this will happen soon. Perhaps more likely is an app like Mellel which does OpenType could be expanded to cover these scripts.+
    Several years ago the developer of Mellel assured me that v.2 would support Indic scripts. But it never happened.
    +In the meantime, OpenOffice/X11 does work for display I think.+
    Yes, OpenType fonts can be used in OpenOffice/X11 (which is no longer being developed, BTW, now that there's a native OS X version). But not AAT fonts.
    +This might interest you:+
    http://discussions.apple.com/thread.jspa?threadID=1577715&tstart=0
    Indeed, though much of it is over my head. I just want to be able to use the same fonts for Indic languages as everyone else, just as I can in Chinese, etc.

  • GetTreeLock during constructor for swing components

    I've noticed some dead locks in our application that are showing up in the call to getTreeLock that exists in the constructor for various JComponents. Specifically, the updateUI method winds up calling validateTree.
    I thought it was OK to construct JComponents from any thread, this suggests it is not.
    Has anyone else experienced this?

    It goes like this:
    On win32, thread 0 is our windows message pump. If dispatching one of our messages results in a call from C into Java that constructs a JComponent, we call getTreeLock from thread 0.
    Imagine that at the same time, the awt event disptatch thread is trying to show a frame. Since the awt event dispatch thread is part of our process, when the native implemention of the frame calls SendMessage to show the frame, the call to SendMessage blocks until thread 0 calls GetMessage.
    Since thread 0 is now blocking waiting for synchronized(getTreeLock()) to return and the awt event dispatch thread has the tree lock - fetched while executing the show() method - we are in a dead lock.
    This is not a problem with AWT components, only swing components.
    The stack looks like this:
         at java.awt.Component.getTreeLock(Component.java:811)
         at java.awt.Container.invalidateTree(Container.java:1116)
         at java.awt.Container.setFont(Container.java:1148)
         at javax.swing.JComponent.setFont(JComponent.java:2310)
         at javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:89)
         at javax.swing.plaf.basic.BasicButtonUI.installDefaults(BasicButtonUI.java:124)
         at com.sun.java.swing.plaf.windows.WindowsButtonUI.installDefaults(WindowsButtonUI.java:63)
         at javax.swing.plaf.basic.BasicButtonUI.installUI(BasicButtonUI.java:60)
         at javax.swing.JComponent.setUI(JComponent.java:449)
         at javax.swing.AbstractButton.setUI(AbstractButton.java:1616)
         at javax.swing.JButton.updateUI(JButton.java:119)
         at javax.swing.AbstractButton.init(AbstractButton.java:1952)
         at javax.swing.JButton.<init>(JButton.java:109)
         at javax.swing.JButton.<init>(JButton.java:64)
    It seems like we could simply have the invalidateTree method return early if there is no parent or there are no children or somesuch.
    A work around we've done is to have the updateUI method delegate to the event dispatch thread. This is a drag as we need to subclass all the JComponents and make sure developers use the safe subclass.

  • How can I select a smaller font for all components of a Swing application?

    The default size of font in Java Look and Feel is 12, how can I change it to 10 globally for my Swing application ?
    Thanks for your help.
    Helen

    It is not very simple because all components don't use the same fonts and if you don't want to subclass the l&f you can do this :
    after setting the l&f (in the main for example)
    Font f = new Font("dialog", Font.PLAIN, 11);
    UIManager.put("MenuItem.font", f);
    UIManager.put("Menu.font", f);
    UIManager.put("MenuItem.acceleratorFont", new Font("dialog", Font.PLAIN, 10));
    UIManager.put("Label.font", f);
    UIManager.put("Button.font", f);
    UIManager.put("ToggleButton.font", f);
    UIManager.put("ToolTip.font", f);
    UIManager.put("List.font", f);
    UIManager.put("Table.font", f);
    UIManager.put("TextField.font", f);
    UIManager.put("ComboBox.font", f);
    UIManager.put("RadioButton.font", f);
    UIManager.put("CheckBox.font", f);
    UIManager.put("RadioButtonMenuItem.font", f);
    UIManager.put("CheckBoxMenuItem.font", f);
    UIManager.put("TableHeader.font", f);
    UIManager.put("Spinner.font", f);
    UIManager.put("Panel.font", f);etc...
    Denis

  • Explicit Event Enabling for Swing Components

    Hi all swing-experts,
    I want to enable some events for an JInternalFrame explicitely,
    but I can't find the corresponding EVENT_MASK anywhere...
    Who knows??
    Anja

    Hello again,
    (besonderes HALLO an Stephen fuer die Muehe!!!)
    there exist two ways of handling an event, at least for awt events:
    1) writing an action listener and adding it to the component who will handle the event
    2) letting the component handle the event itself by "explicit event enabling"
    I am talking about no 2.
    This works so:
    a) Subclass component
    b) in the constructor of the subclass, call
    enableEvents(AWTEvent.XXX_EVENT_MASK)
    c) provide the subclass with a processXXXEvent() method (that should call the superclass' version of this method)
    An other version of my question: is this also possible with Swing?
    Where do I find the appropriate XXX_EVENT_MASK and processXXXEvent method?
    Seems to be an unusual problem...
    Anja
    PS: sieht so aus...

  • Determine event handler for swing components from the API browser

    Is there a way to determine what event handlers are assocaited with the different swing classes. For example JTextField is associated to ActionListener, and JCheckbox uses the event handle ItemListener. Is there a way to determine this by looking at the class via the Java API?

    Yes, there is. You'll observe that JTextField has an "addActionListener(ActionListener)" method, for a start. And JTextField is a subclass of JTextComponent, which has "addCaretListener(CaretListener)" and "addInputMethodListener(InputMethodListener)" methods. And JTextComponent is a subclass of JComponent, which has an "addAncestorListener(AncestorListener)" method. And so on... there are more. All of this can be found in the API documentation by looking for methods whose names start with "add".

  • Size settings for swing components and resizing of a JFrame

    Hi all!
    I'd have the following question:
    There is a JFrame, which content panel has the GridBagLayout. In some places on that layout's grid there are JScrollPanels (let's name each as "inner panel") with JTables in the view ports.
    When user resizes the main JFrame, I'd like that all inner panels and corresponding tables would resize synchronously: if user enlarges the main JFrame, then the sizes of all those components should increase proportionally, when user makes the main JFrame smaller, then all the sizes should become proportionally less.
    I am confused with the settings for max, min and preffered sizes for inner panels as well as for their JTables. I have read about those size attributes and tried to play with them, but I can't obtain the needed result :-/
    Each inner panel has the "BOTH" value for the fill attribute in the GridBagLayout's constraints.
    Might someone help me?
    Thanks in advance!

    Hmm...
    The JPanel that contains all stuff is the JFrame's content pane...
    The entire JFrame's content pane contains another panels (JScrollPanels), which are all set to fill all available space on the parent panel (JFrame's content pane) with "both" value of the "fill" attribute.
    Should I put the only one JPanel on the JFrame's content pane and all other stuff at that additional JPanel...?

  • Alpha levels for swing components

    Hello All,
    I am trying to control the alpha levels for a JLabel which is essentially just a rectangle. I have seen examples of how to use the AlphaComposite class and apply alpha levels to graphics, but my goal is generate dynamically sized rectangles and then control their alpha levels.
    Here is the rectangle I am currently generating, with no transparency.
        protected JLabel createColoredRectangle(Color color, Point origin, int w, int h) {
             JLabel label = new JLabel();
             label.setBorder(BorderFactory.createLineBorder(color, 1));
             label.setBounds(origin.x, origin.y, w, h);
             label.setForeground(color);
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setOpaque(false);
            label.setVerticalAlignment(JLabel.TOP);
            return label;
        }Any suggestions would be greatly appreciated.
    Cheers,
    Cypher

    in any case, setting the foreground isn't going to do anything for your boxes, what you need to do is set the background. Here is my test case to get you started:
    public class Test {
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(new GridLayout(3,3));
            for (int i=0;i<9;i++) {
                //create an alpha-green for each box
                frame.getContentPane().add(createColoredRectangle(new Color(100,255,100,255/9*i)));
            frame.setSize(300,300);
            frame.setVisible(true);
        static protected JLabel createColoredRectangle(Color color) {
            JLabel label = new JLabel();
            label.setBorder(BorderFactory.createLineBorder(color, 1));
            label.setBackground(color);
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setOpaque(true);
            label.setVerticalAlignment(JLabel.TOP);
            return label;
    }

Maybe you are looking for