AWT components

Hi,
I want to have some GUI buttons in my applet. They appear in my applet but don't obey the size and location.
     public void init()
          username = new TextField();
          username.setSize(100, 23);
          username.setLocation(240, 200);
          add(username);
          password = new TextField();
          password.setSize(100, 23);
          password.setLocation(240, 250);
          password.setEchoChar('*');
          add(password);
          login = new Button("Login!");
          login.setSize(100, 50);
          login.setLocation(200, 300);
          login.addActionListener(this);
          add(login);
     }

Javaisthefuture wrote:
Thanks! I just forgot the setLayout(null);But still my question remains unanswered: why are you using AWT and not Swing? Is it because the sample code you downloaded uses AWT? Do you understand the difference? Do you know the difference between heavy-weight and light-weight components?

Similar Messages

  • Characters show up as boxes when using AWT components.

    Having installed on a Windows XP english OS the asian languages packs (with fonts included, from Microsoft) and knowing that with MS Gothic, Gulim and Simsun you can display japanese, korean and chinese characters it seems that on JRE 1.5 and up this is not possible (boxes appear instead) on any applet.
    How to reproduce:
    Label l2 = new Label("Japanese chars: \u8457\u4f5c\u6a29");
    Workaround:
    Step 1. Rename ..\jre\lib\fontconfig.properties.src to ……\fontconfig.properties
    Step 2. Modify the following line:
    sequence.allfonts=alphabetic/default,dingbats,symbol
    to
    sequence.allfonts=alphabetic/default,dingbats,symbol,japanese,chinese-ms936,chinese-ms950,korean
    How is this possible? On JRE 1.4 this was working. Also another fix is to use Swing components instead but this is not realistic for a big project.
    Any help will be greatly apprecied.

    The problem is related to the JRE v. 1.5.0_11 or greater. In JRE 1.4.2_13 for each language there was a configuration file that Java was using to determine which fonts to use for different categories of objects.
    The problem is encountered only for AWT components.
    Starting with JRE 1.5 (http://java.sun.com/j2se/1.5.0/docs/guide/intl/fontconfig.html) there is only one configuration file (fontconfig.properties.src) for all the languages. Since the character subsets supported by given fonts often overlap, in the file separate search sequence properties are used to define in which order to try the fonts when rendering a character. In the default version of the file this properties do not specify also the asian languages.
    We have to do the following things:
    *1.* You must first install into Windows the support for asian languages: Control Panel - > Regional and Language Options
    � On the Advanced tab add the conversion tables for all Asian languages (this action may not be required).
    � On the Languages tab check �Install files for East Asian Languages (this action is required)
    Restart the computer.
    *2.* Assure you have the needed asian fonts : batang(fopr korean), simsun, gulim, mingliu, msgothic(for japanese) installed.
    *3.* Rename ...\jre\lib\fontconfig.properties.src to ��\jre\lib\fontconfig.properties. By doing this the fontconfig.properties file will be used by the JRE as the default configuration file.
    *4.* Modify the following line:
    sequence.allfonts=alphabetic/default,dingbats,symbol
    to
    sequence.allfonts=alphabetic/default,dingbats,symbol,japanese,chinese-ms936,chinese-ms950,korean
    *5.* Assure that in the file the logical to physic font binding, binds to existing fonts.
    Obs: If step 1 is skipped the titles of the windows will still have boxes.
    Hope it helps.
    Donoiu Cristian

  • Multilanguage support for awt components..

    hi all,
    when i tried to use .setFont() method for awt componets like labels and buttons it is displayed as rectangles insted of showing the appropriate characters. actually i tried to set the font to "simsun" (unicode font for chinese). but it works fine for swing components.
    how can we achieve that for awt without changing the font.properties file? or how can we insist to use the font.properties file supplied by us insted of default font.property file for that language?
    can we run the application by putting our font.properties file which is meant to be used for only our application, in the current working folder insted of putting in javahome\jre\lib path?
    thanks in advance..
    regards
    sojan

    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

  • Some country native characters in AWT components

    Hi All,
    I have problem with visualization some native characters on AWT components. For instance, when I put on the Frame some Labels components, then I can't see correct some special polish native characters, I can only see some boxes. It seems the same with some other AWT components with a text entry.
    This problem is only in GUI, because some other issues like Strings and Stream processing works good.
    (I tested it both on WinXP and Linux SuSE 9.1 with the same result)
    Thanks in advance for some help
    Krzysztof

    Have you tried doing this in Swing? Works for me:
    import java.awt.*;
    import javax.swing.*;
    public class FontExample {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable(){
                public void run() {
                    buildGUI();
        static void buildGUI() {
            Font font=new Font("Dialog",Font.PLAIN,12);
            String text="re\u0123istr\u0113tiem";
            JTextArea textarea=new JTextArea();
            textarea.setFont(font);
            textarea.setText(text);
            JComboBox combo=new JComboBox(new String[]{text});
            combo.setFont(font);
            JFrame f = new JFrame("FontExample");
            Container cp = f.getContentPane();
            cp.add(combo, BorderLayout.NORTH);
            cp.add(textarea, BorderLayout.CENTER);
            f.pack();
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    }

  • Changing AWT components to Swing components

    I'm having a little trouble changing some AWT components to Swing components. I received an error message when I tried to run my program. I looked up the component in the java docs. But, I did not see the option the error was talking about. The error and the area of code is listed below. Thank you for any help you can provide.
    Error message:
    Exception in thread "main" java.lang.Error: Do not use P5AWT.setLayout() use P5A
    WT.getContentPane().setLayout() instead
    at javax.swing.JFrame.createRootPaneException(JFrame.java:446)
    at javax.swing.JFrame.setLayout(JFrame.java:512)
    at P5AWT.<init>(P5AWT.java:56)
    at P5AWT.main(P5AWT.java:133)
    Press any key to continue . . .
    Code:
    JPanel p3 = new JPanel();
    p3.setLayout(new FlowLayout(FlowLayout.CENTER));
    ta = new JTextArea(20, 60);
    p3.add(ta);
    setLayout(new FlowLayout(FlowLayout.CENTER));
    add(p3);

    you need to change the line...
    setLayout(new FlowLayout(FlowLayout.CENTER)); to
    getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));

  • Overlapping AWT components

    Hi, is there anyway to control how awt components are added when they overlap over each other? (such as sending it to the front or back of all components) I have a container that has several canvases, where the last component added should be on top. Unfortunately, the canvas is always covered by the other canvas. Any advice?

    Look at his program : the last canvas is on top of the second that is no top of the first,
    mport java.awt.*;
    import java.awt.event.*;
    public class Overlap extends Frame
    public Overlap() 
         addWindowListener(new WindowAdapter()
        {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);}});
         setBounds(10,10,500,400);
         setLayout(null);     
         Canvas c1 = new Canvas();
         c1.setBounds(30,30,100,100);
         c1.setBackground(Color.pink);
         add(c1,0);
         Canvas c2 = new Canvas();
         c2.setBounds(100,100,130,130);
         c2.setBackground(Color.red);
         add(c2,0);
         Canvas c3 = new Canvas();
         c3.setBounds(200,200,100,100);
         c3.setBackground(Color.orange);
         add(c3,0);
         setVisible(true);
    public static void main (String[] args) 
         new Overlap();
    Noah

  • Lots of AWT components

    Im just wondering are there any good free tools to quickly layout lots of AWT components?
    Also how easy is it to produce tabs in AWT? is it as simple as assigning different panels to each tab?
    One more question, if there are lots of components on a screen, how fast is the AWT? for example, will it have a problem displaying 40 something components?
    Thanks for any help

    For the tab thing: Use JTabPane (which internally probably uses CardLayout), it�s very very easy: Check out the JtabePane example in SwingSet in Java Web Start, the source code is included.
    Layouting like 40 components isn�t really a performance question, because it�s nothing compared to drawing a 100*100 image.
    It takes advanced programming stuff before you will have perfomance problems, without using infinity loops or blocking the thread (by sleep or so)
    To layout many components, use several of the basic layout managers wich each other or use GridBagLayout or use my open source, free ProportionLayout (written to solve some probs I had with GridBag):
    http://users.pandora.be/lunatic/ProportionLayout/

  • Concurrently update awt components

    Hello all,
    What I understand is that it's impossible to update awt components concurrently in a thread save way. For example; If you want to implement a game in which when you push on a button a counter starts to run (in a JTextField for example). While that counter is running you want that button to be disabled for a fixed time, let's say 1 second, and want to count down on that button's text. At the same time other components should be updated by other code in the game logic ( things like mana regenration counters etc.).
    Is this possible in a thread save way with standard libraries?

    Thank you for replying.
    Yes that is what I understood. My problem is though, for example in the case of the mana regeneration, that I want it to regenerate at a fixed rate. So x amount in y seconds. I know this comes close to real time programming and is not really possible, but that's not the point here. What I'd like is that the current mana is available to the game logic and displayed at the same time in the gui, while it's being updated. While this mana regeneration is running, the user can press another button (a spell) and the mana takes another hit, while still being updated by the regeneration thread. Other game logic may affect the mana aswell (like abilities of monsters).
    The problem here is that I need none user events to update awt components and that it should be displayed in the gui real time (ish).

  • Why the AWT components are called heavy-weight and SWING as light-weight?

    Why the AWT components are called heavy-weight and SWING as light-weight?.
    Building our own components means overhead on JVM. hence in what sence the SWING components are called light weight.?
    I know that the Swing componets are the components which are not going to depend upon peer classes and render, OS independent LOOK n FEEL.
    Thanks,
    Sanath Kumar

    All extensions of Component are considered lightweight since, as you point out, they don't have their own dedicated native peer.
    Essentially a lightweight component only has a canvas on which to draw itself and some means of receiving events from the system. In fact, there is no need to have one of these "canvas" and "event" objects for each component - the parent component can manage these abilities on behalf of its children. Eventually you end up with only top-level components needing to be heavyweight in order to communicate with the windowing system.
    A lightweight component therefore has the minimal dependency on the underlying windowing system giving you maximum cross-platform flexibility.
    It is usually true that lightweight => slower and heavyweight => faster since it is reasonable to assume that native peers will be optimal for the windowing system. However, that's not the point of the lightweight/heavyweight distinction.
    Hope this helps.

  • AWT Components without Border

    I've made subclasses of all awt components and overwritten pain() metod so that the component don't have border. But this technik don't work with TextField and TextArea ( Button, Choice, List is working fine). Does any body kwno what should I do to have components with NO border or with 2D border?

    Its a pain() isn't it! Guess the only way is to do a workaround. Maybe this could be a way?
    class MyTextField extends Panel
       public TextField m_TextField;
       MyTextField()
          m_TextField = new TextField();
          add(m_TextField);
       public void doLayout()
          m_TextField.setBounds(-2, -2, getSize().width+4, getSize().height+4);
    Ragnvald Barth
    Software engineerhttp://blake.prohosting.com/ragnvald/

  • CardLayout & AWT Components

    Switching between panels when using AWT components with a CardLayout is not as smooth as when using Swing components.
    At best the 2 panels seem to merge into one another until only one is displayed, while at worst a part of the previous panel can remain on the screen for a half second or so before the switch is complete.
    Kind of hard to explain but there is no instantaneous switch like you get when using Swing components.
    As I hope to eventually run this GUI as an Applet - when the problem may, I imagine, become even more pronounced - I would be grateful for any assistance anyone could offer on this matter.
    When switching the panels ALL I do is:
    cardLayout.show(deck, "PanelName");

    Well it's too big to post the whole thing. Some snippets:
    // add the panels to the cardLayout
    deck = new Panel();
    deck.setLayout(cardDisplay);
    deck.add(menuPanel, "MenuPanel");
    deck.add(notePanel, "NotePanel");
    deck.add(logonPanel, "LogonPanel");
    deck.add(tabbedPane, "TabbedPane");
    this.add(deck);
    cardDisplay.show(deck, "LogonPanel");
    // switch on some button press
    if (e.getSource() == appendNote)
    cardDisplay.show(deck, "NotePanel");
    noteArea.requestFocus();

  • About the difference between the AWT components and the swing components

    As we know that an AWT component has a peer, but dose a lightweight component(e.g: a swing component) has also a peer? And could somebody tell me that who and how creates the CLICKED_EVENT when I click a JButton(a swing component), and where the CLICKED_EVENT to go util it was processed by a method like btn_ok_clicked(Event e)? And if I click a Button(an AWT component), what's the difference?
    Thanks a lot!

    The main difference is that AWT are native components, and Swing components are entirely in java written. That's besides the look and feel. There are a lot of other differences between AWT and Swing, and there are better described in the documentation.

  • Repaint/Refreshing AWT components

    Hi there,
    I'm writing a chess game and for the GUI I've added 8x8 panels into a frame with a grid layout. Currently I'm just using labels for the pieces, in the middle of panels. I'm having a problem updating my GUI after a move has been made - a call to repaint() does nothing. I'm just using basic AWT.
    I've also tried removeAllComponents() of the frame and adding them again with the updated data, but nothing happens - I just lose all my panels. Can anyone help please?
    Many thanks!

    scsi-boy :
    I want to have a more general method of refreshing the
    graphics so that later if/when I 'upgrade' to using
    images (or whatever) instead of labels I can simplyi believe my suggestion is certainly compatible with that.
    adapt it. I would think that calling setText for the
    Labels would mean that I would have to give the
    location of the panel as a parameter somewhere, and
    would also have to remove the label where the piece
    was previously (ie if a knight moves, I'd have to
    target its destination to setText and remove it from
    where it was).here is what i would do, please note that this will work with a variety
    of implementations including using all Graphics later on or whatever.
    // here we have a bunch of constants that represent the possible status
    // of board postions
    final int EMPTY_SPACE = 0;
    final int B_KING = 1;
    final int W_KING = 2;
    final int B_QUEEN = 3;
    final int W_QUEEN = 5;
    final int B_BISHOP = 6;
    final int W_BISHOP = 7;
    final int B_KNIGHT = 8;
    final int W_KNIGHT = 9;
    final int B_ROOK = 10;
    final int W_ROOK = 11;
    final int B_PAWN = 12;
    final int W_PAWN = 13;
    // the game board is used to store the status of all the postitions in
    // a game
    int[][] gameBoard;
    // the game display is the array of Labels that we use to present the
    // board in our GUI
    Label[][] gameDisplay;
    // in the constructor;
    gameBoard = new int[8][8];
    for(int i=0;i<gameBoard.length;i++){
      for(int j=0;i<8;j++){
        gameBoard[i][j] = EMPTY_SPACE;
    // here is where you would add the various pieces to gameBoard...
    gameDisplay = new Label[8][8];
      Now wherever you interaction and logic are you can use a method. You
      can call this method repaint(); so that it will be compatible with
      what you do later.
    public void repaint(){
      for(int i=0;i<gameBoard.length;i++){
        for(int j=0;j<8;j++){
          switch(gameBoard[i][j]){
            case B_KING:
              gameDisplayj].setText("Black King");
    break;
    case W_KING:
    gameDisplay[i]j].setText("White King");
    break;
    case B_QUEEN:
    gameDisplay[i]j].setText("Black Queen");
    break;
    // and so on and so forth... until
    default:
    gameDisplay[i]j].setText("");
    anyway the above code is just a sample of how you can build this so you
    can seperate out your logic from your GUI so you can change your GUI
    later without hassle.
    finally i would ask how are you changing the text on the screen. are you
    creating new Labels and sticking them in the Panels? i suspect something
    like this... don't do that. two reasons, one as you may have noticed it
    doesn't work that way or you have to use some sort of hack to get it to
    work and two it's more efficient in terms of memory and speed if you
    re-use the awt ot Swing components by making use of methods such as
    setText rather than creating new objects each time.
    i hope you find this helpful.

  • JavaFX and heavyweight awt components

    Can somebody point me to documentation on what's the deal with javaFX and heavyweight components?
    I have been wrapping an awt.Canvas into a SwingComponent. This basically seems to work. It does not obey layout rules, but I think I can work around that. However, soon as the mouse goes over the node, the canvas starts to move around (while the SwingComponent stays in place). I have - to my best knowledge - not set up mouse interaction in any way.
    Using
    visible:false
    on the node, housing the SwingComponent with the Canvas is the only thing I found to make the canvas stay in place (it does not hide it..). But that looks like a hack, likely to fail sooner or later. So, I'd like to learn more about what's the contracts here. I don't care for much more than desktop profile in the moment.
    thanks
    nils

    I got this problem too
    For the moment, i just put a rectangle on top of the awt component , and set the blockMouse "True".
    But when i try to translate the component location, i couldnot find a proper solution, the component just dont move at all.

  • AWT components in swing not working

    Hi,
    We are developing a swing based GUI application.
    Here we add a third party AWT based component into the JPanel. But if we add that AWT component, it does not show our JMenuItems properly.
    Can somebody tell me what is the problem with the code below? Why it is not displaying my JMenuItems ?????
    import java.awt.*;
    import javax.swing.*;
    public class ButtonTest extends JFrame
         JMenuBar mbar;
         JMenu file;
         JMenuItem i1, i2, i3;
         Container con;
         public ButtonTest()
              con = this.getContentPane();
              con.setLayout(new BorderLayout());
              mbar =      new JMenuBar();
              file = new JMenu("FILE");
              i1 = new JMenuItem("Item1");
              i2 = new JMenuItem("Item2");
              i3 = new JMenuItem("Item3");
              this.setJMenuBar(mbar);
              file.add(i1);file.add(i2);file.add(i3);
              mbar.add(file);
              con.add(new Button("OK"));
         public static void main(String args[])
              JFrame mf = new ButtonTest();
              mf.setSize(400 ,400);
              mf.setVisible(true);
    regards,
    Anand

    Replace them with this.
    import java.awt.*;
    import javax.swing.*;
    public class ButtonTest extends JFrame
         JMenuBar mbar;
         JMenu file;
         JMenuItem i1, i2, i3;
         Container con;
         public ButtonTest()
              con = this.getContentPane();
              setSize(600,600);
              mbar = new JMenuBar();
              setLayout(new FlowLayout());
              file = new JMenu("FILE");
              i1 = new JMenuItem("Item1");
              i2 = new JMenuItem("Item2");
              i3 = new JMenuItem("Item3");
              file.add(i1);file.add(i2);file.add(i3);
              mbar.add(file);          
              con.add(new JButton("OK"));
              setJMenuBar(mbar);
              setVisible(true);
         public static void main(String args[])
              ButtonTest mf = new ButtonTest();
    }

Maybe you are looking for