Simple JFrame Doubt

Why is the jextfield on the jframe so small.
Also How do I update the textField of MyButton JFrame from JButtonActionListener?
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MYButton extends JFrame {
     JButton jbnButton1;
     JTextField jtfClick;
     MYButton() {
          jbnButton1 = new JButton("Click Me");
          jbnButton1.addActionListener(new JButtonActionListener(MYButton.this));
          jtfClick = new JTextField();
          setLayout(new FlowLayout());
          getContentPane().add(jbnButton1);
          getContentPane().add(jtfClick);
          setSize(400, 400);
          pack();
          setTitle("Example JFrame");
          setVisible(true);
     public static void main(String args[]) {
          new MYButton();
     public JTextField getJtfClick() {
          return jtfClick;
     public void setJtfClick(JTextField jtfClick) {
          this.jtfClick = jtfClick;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class JButtonActionListener implements ActionListener {
     JFrame parent;
     public JButtonActionListener(JFrame p) {
          parent = p;
     public void actionPerformed(ActionEvent e) {
//How do I update the textField of MyButton JFrame from here
}

hemanthjava wrote:
Why is the jextfield on the jframe so small. Because it hasn't been given a preferred size and FlowLayout isn't going to assign it one. Why not initialize it like:
        jtfClick = new JTextField(12);
Also How do I update the textField of MyButton JFrame from JButtonActionListener?You could pass a reference to the JFrame to the action listener by way of the listener's constructor.

Similar Messages

  • A simple / silly doubt for Gail

    Hi Gail,
    I was going through the Creator Field Guide book. On page 120 you have described about adding event handler for the Resest button. There are two lines of code for setting the two textfields to empty strings. My question is do we really need to do this. The button type can be set to Reset and it does the requierd work.
    I am lonely, Am I happy ? Life goes on

    Hi,
    Thank you so much. :-) I tried it myself and you are right the old values get retained even if the reset button is clicked.
    Then again a question rears its ugly head in my mind. How many times out of 100 would a person hit the submit button by mistake instead of the reset button. Secondly, if a person has hit the submit button by mistake then he is taken to the login failed or login success page. So my question is what does setting the strings to null really achieve?
    One scenario I noticed was hitting the browser's back button has the old value in the username field if it has nto been set to null. I hope my doubts are clear.
    Thank you once again for the really wonderful explanation Brad.
    I am lonely, lonely is good, hence I am good :-)

  • Help!!! Simple JFrame doesn't work in jdk1.4

    If I run the following code in jdk1.2 it works fine, but in 1.4 only one button displays initially. If I click on the button, the other will display, but it displays in the last location that the window was in. For example, if I move the window first, then click the button, it draws itself at the location that it was previously at. Even more frustrating, when I close the window, once again, it draws itself in the last location and this remains on the screen, even after the vm exits.
    Source:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class FrameExample extends JFrame {
    public FrameExample(String title) {
         super(title);
         setSize(350, 200);
         setLocation(300, 300);
         JButton button1 = new JButton("Button 1");
         JButton button2 = new JButton("Button 2");
         JPanel buttonPanel = new JPanel();
         buttonPanel.add(button1);
         buttonPanel.add(button2);
         getContentPane().add(buttonPanel);
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setVisible(true);          
    public static void main(String args[]) {
         FrameExample fe = new FrameExample("Frame Example");

    try to set the width and height of the frame - setSize(int, int). or use pack()

  • Cairngorm 3 Simple Application doubt

    Either I am missing something OR there might be a good reason for the following:
    SaveContactEvent/SearchEvent etc does not extend any Event class, then why we should be calling them events. It is ambiguous and leads to a lot of confusion. These could be appended with something else, but not event.
    More about Cairngorm 3 (in general):
    There are multiple libraries such as Observer 1.8 and ObserverParsley 1.8.
    Do I need both of them to be included in my app OR one of them will fullfil my need based on some preconditions?
    Cairngorm 3 provides guidelines and various approaches to handle multiple problems, but same time it is much more confusing to get started with a simple application.
    Every lib has it's own docs, but atleast they all could be combined under an umbrella "cairngorm docs" providing a single landing page to get an access to all the different libs.
    Thanks.

    Some of our projects append them with *Message as they are really Parsley Messages not Flex events. Would you prefer that better or do you have another suggestion? Insync indented to use the Event suffix in a more logical meaning. Technically, they could also be subclassing flash.events.Event, but Parley's messaging doesn't require that.
    Can you explain in what situation that caused you confusion though? Did you try to dispatch a Parsley message via the display list?
    Regarding the Observer libraries, we should improve our documentation to make this clearer. If you read the how-to document, only the last paragraph: "Loosely Coupling Parsley Domains - Parsley Extension" describes an extention that lives within ObserverParsley and therefore requires Parsley. The rest of the Observer library is framework independent and contains all the other components described.
    Have you seen the Introduction paragraph in Guidelines? Those explain the core concepts. Many of the libraries are optional so we don't want to imply users "have" to use all of them. The libraries section is the single entry point for all libraries.

  • JFrame doubt?

    Hi,
    I need some help regarding JFrame. Actually i want to add the component in particular position in the GridLayout. I need to add a label in the label at
    my code like this,
    myLabel[0] = new JLabel(new ImageIcon(myUrl), SwingConstants.CENTER);
    ---- // array of labels here
    JFrame f = new JFrame();
    --- // frame code and GridLayout code
    f.getContentPane().add(myLabel[0]);
    myLabel[0].revalidate();My problem is somewhere i 'm revalidating. I want to put labels in specified locations like NORTH or SOUTH. but By revalidating it goes in to default mode. can you give any suggestions please..
    Thanks
    - balaji

    The last 3 or 4 times I've seen you post a question you have posted it in at least two forums.
    If you have a Swing question post it in the Swing forum.
    If you have a general question post it in the Java Programming forum.
    Don't cross post. I for one get frustrated when I answer a question in one forum only to find it has been answered by someone else in another forum. My time can be better spent answering questions that nobody has answered.
    With regards to this question I suggest you read the Swing tutorial on "Using Layout Managers":
    http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

  • Simple Math Doubt (I hope....)

    Hello
    I was wondering if anyone knows how to make a variable always positive, regardless of any values added or subtracted to it....
    like this:
    int a = 10;
    int b = 5;
    int c = b-a;
    i want c to be positive......
    appreciate the help,
    Thank you

    GMX750 wrote:
    int a = 10;
    int b = 5;
    int c = b-a;
    i want c to be positive......How would you do it by hand? (Hint: Absolute value?)
    Check out the Math API: [http://java.sun.com/javase/6/docs/api/java/lang/Math.html|http://java.sun.com/javase/6/docs/api/java/lang/Math.html]

  • Drawing with double buffering to a JFrame

    I want to create a simple JFrame and draw stuff to it via Graphic object using double buffering.
    If I create a loop and draw constantly using JFrame.getGraphic().xxx, am i doing double buffering. ( 'cause i read somewhere that swing components automaticly use double buffering ). And is this the righ approuch ( or whatever it's spelled ) ?

    I want to create a simple JFrame and draw stuff to it
    Don't do that.
    If you want to do custom rendering then use JPanel or JComponent, not JFrame.
    If I create a loop and draw constantly using JFrame.getGraphic().xxx, am i doing double buffering.
    No.
    You're also painting outside the standard paint cycle, which is a Bad Idea.
    So, essentially, everything your suggesting is the wrong way to go about things.
    In its most simple form, double buffering works something like this, though there are a whole load of subtleties to consider (such as component resizing for a start)
    public class Foo extends JComponent
        private BufferedImage image = null;
        protected void paintComponent(Graphics g)
            if (image != null)
                image = createImage(getWidth(), getHeight());
                paintBuffer(ig);
            g.drawImage(image, 0, 0, this);
        private void paintBuffer()
            Graphics ig = image.getGraphics();
            // do the rendering
            ig.dispose();
    }

  • Enlarge JFrame and its components properties  proportionally

    Hi,
    We have a simple JFrame � Container - uses Flow Layout and has got Buttons, Text Fields� All of them are using Java�s default fonts, sizes..�
    And we want to increase or enlarge all of the JFrame and its sub components properties proportionally (especially Font�.) like in for example MS-Word.
    In the Ms-Word, there is ZOOM option in the Standard tool bar menu. If you set zoom to 150%, the font is automatically increases. And if you set it to 100% the font sets back to normal. And if the window is not enough to fit, it adds the scroll bars.
    For example, if we increase the Labels Font, the height of that component also should increase proportionally. So the Frame also should increase.
    How could we achieve this sort of functionality ( i.e. Proportionally increasing the component�s properties ) in Swing?
    I believe it is hard to set/adjust the all of the Frame�s components properties (I.e. Height, Font�) proportionally by using the Component listeners.
    (Another example, you might have noticed that the Frame�s Font, hight,width �sizes are proportionally bigger in Systems screen�s 800 by 600 pixels mode compare with 1024 by 768 pixels mode. May be this is windows feature. But we are looking at achieving the same sort of feature)
    Any help or thoughts would be appreciated.
    Thanks in advance.
    Vally.

    Here is a working sample:
    package ui_graphics;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.LayoutManager;
    import java.util.Arrays;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.SpinnerListModel;
    import javax.swing.SpinnerModel;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    public class ZoomableFrameTest {
       private ZoomablePanel zoomPanel;
       private JLabel label;
       public ZoomableFrameTest() {
          label = new JLabel("Essai pour le zoom");
          label.setBounds(0,0,200,16);
          zoomPanel = new ZoomablePanel();
          zoomPanel.add(label);
          JFrame frame= new JFrame("Zoom test");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(getZoomSpinner(), BorderLayout.NORTH);
          frame.getContentPane().add(zoomPanel, BorderLayout.CENTER);
          frame.pack();
          frame.setVisible(true);
       public static void main(String[] args) {
          new ZoomableFrameTest();
       public JSpinner getZoomSpinner() {
          String[] zoomValues = { "50", "100", "150" };
          SpinnerModel model = new SpinnerListModel(Arrays.asList(zoomValues));
          final JSpinner spZoom = new JSpinner(model);
          Dimension dim = new Dimension(60, 22);
          spZoom.setMinimumSize(dim);
          spZoom.setPreferredSize(dim);
          spZoom.setMaximumSize(dim);
          spZoom.setFocusable(false);
          spZoom.addChangeListener(new ChangeListener() {
             public void stateChanged(ChangeEvent e) {
                // R�cup�re le facteur d'�chelle
                int factor = Integer.parseInt((String)spZoom.getValue());
                setScaleFactor(factor);
          spZoom.setValue("100");
          return spZoom;
       private void setScaleFactor(int factor) {
          zoomPanel.setZoomFactor(factor);
    class ZoomablePanel extends JPanel {
       private int zoomFactor;
       public ZoomablePanel() {
          super(null);
       public void setZoomFactor(int factor) {
          this.zoomFactor = factor;
          if (this.isShowing()) this.repaint();
       public void paint(Graphics g) {
          super.paintComponent(g);
          double d = zoomFactor / 100d;
          Graphics2D g2 =(Graphics2D)g;
          g2.scale(d, d);
          super.paint(g2);
    }

  • Source codes and jframes

    Hello ,
    I am trying to make it so that I can somehow place this source code,
    *Hi_LoGame.java*
    Peter Hadlaw
    *2009/10/12*
    *This is a simple guessing game that tells the user if they got the number right or if the are too low or too high, and then asks if they wish to continue. It then prints the results, and the best score.*
    import java.util.*;
    public class Hi_LoGame {
        public static void main(String[] args) {
           Scanner userInput = new Scanner (System.in);
           int userC, userG=0, bestGNum=0, currentGNum=0, gamesP = 0, gNum = 0, cpuNum;
           Random cpuRandNumGen = new Random();
           System.out.println("Welcome to the Hi - Lo game showdown!!!\nWould you like to play: (1)Yes or (2)No?");
           userC = userInput.nextInt();
           while(userC<=0||userC>=3){
                System.out.println("Please enter a valid choice of 1 or 2.");
                userC = userInput.nextInt();
           cpuNum = cpuRandNumGen.nextInt(100)+1;+
    +       while(userC==1){+
    +                 currentGNum++;
                     System.out.print("Please enter a number from 1-100 for you guess: ");
                     userG=userInput.nextInt();
                     while(userG<=0||userG>100){
                          System.out.println("Please enter a valid guess:");
                          userG = userInput.nextInt();
                     if(userG==cpuNum){
                          System.out.println("Congradulations you got it right!");
                          System.out.println("You got it right in "  +currentGNum+  " guesses.");
                          System.out.println("Do you want to play (1)again or do you want to (2)stop");
                          userC = userInput.nextInt();
                          while(userC<=0||userC>=3){
                               System.out.println("Please enter a valid choice of 1 or 2.");
                               userC = userInput.nextInt();
                          if(gamesP==0)
                               bestGNum = currentGNum;
                          if(gamesP>1&&currentGNum<bestGNum){
                               bestGNum = currentGNum;
                          else{//nothing
                          gamesP++;
                          cpuNum = cpuRandNumGen.nextInt(100)+1;+
    +                      if(userC==1){+
    +                           currentGNum=0;+
    +                      }+
    +                      else{+
    +                      }+
    +                 }+
    +                else if(userG>cpuNum){+
    +                     System.out.println("Sorry but you are to high!");+
    +                     System.out.println("Do you want to guess (1)again or do you want to (2)give up");+
    +                      userC = userInput.nextInt();+
    +                      while(userC<=0||userC>=3){+
    +                           System.out.println("Please enter a valid choice of 1 or 2.");+
    +                           userC = userInput.nextInt();+
    +                      }+
    +                      if(userC==2){+
    +                           gamesP++;
                               System.out.println("You gave up ,your guess count was: "  +currentGNum);+
    +                      }+
    +                      else{+
    +                      }+
    +                }+
    +                else{+
    +                     System.out.println("Sorry but you are too low!");+
    +                     System.out.println("Do you want to guess (1)again or do you want to (2)give up");+
    +                      userC = userInput.nextInt();+
    +                      while(userC<=0||userC>=3){+
    +                           System.out.println("Please enter a valid choice of 1 or 2.");+
    +                           userC = userInput.nextInt();+
    +                      }+
    +                      if(userC==2){+
    +                           gamesP++;
                               System.out.println("You gave up ,your guess count was: "  +currentGNum);+
    +                      }+
    +                      else{+
    +                      }+
    +                }+
    +       }+
    +       if(gamesP==0){+
    +            System.out.println("Thanks anyways, please come again.");+
    +       }+
    +       else{+
    +            System.out.println("Thanks for playing, you last score is: "+  currentGNum  +" but your best guess number was in "+  bestGNum + " guesses.");
    }into a jframe, I heard something about text panes or textareas maybe being able to print this, but i just want one simple jframe with the above, and the user can input stuff right then and their.
    Thank You

    What you currently have is a non-OOP class, one that is nothing but a big main method without methods, constructors, etc.
    If this is your code, it suggests that you still have a bit to learn before tackling Java GUI such as writing classes, methods, constructors, etc. I mean feel free to read up on how to create a Swing GUI here [http://java.sun.com/docs/books/tutorial/uiswing/index.html] and jump right in, but just be forewarned that if you're jumping too early, it'll be a frustrating swim. Much luck.

  • Launching html file from JFrame

    I wish to launch an html file called "Help.html" when a user clicks on the help button on my toolbar in the GUI. I know how to open a simple program using the Runtime.exec( ) command but the problem is how would i open a specified file in a specified program...any help will be appreciated.
    PS: I do not know any C unfortunately, haven't done that yet at uni but would be glad to learn now :)

    I think you missed the question, opening a simple JFrame with a JTextArea won't have the same effect of opening a HTML file in which user can click on stuff and all that, even if I do open another JFrame imagine all the programming i have to do to get the URL's working for all the links in the help file and little search commands etc...I hope someone can tell me how to open the clients web-browser with the help.html file supplied by me!

  • What's Wrong with this JFrame???  I can't see the label.

    I was making a simple JFrame window and I was previewing my work but I couldn't see the label I put in it. It shows a window but no text from the label or red background.
    Thanks.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class FirstClass extends JFrame{
         final static long serialVersionUID = 0;
         public FirstClass(){
              this.setSize(new Dimension(600,300));
              this.setVisible(true);
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              Dimension frameSize = this.getSize();
              int x = (screenSize.width/2) - (frameSize.width/2);
              int y = (screenSize.height/2) - (frameSize.height/2);
              this.setLocation(x,y);
              this.addWindowListener( new WindowAdapter(){
                   public void windowClosing(WindowEvent e){
                        System.exit(0);
              JPanel p = new JPanel();
              p.setBackground(Color.RED);
              p.setLayout(new BorderLayout());
              getContentPane().add(p);
              Label l = new Label();
              l.setText("Hello World");
              p.add(l);
         public static void main(String[] args) {
              FirstClass fc = new FirstClass();
              fc.setTitle("Test");
    }

    your sequence for setVisible() is incorrect.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class FirstClass extends JFrame{
         final static long serialVersionUID = 0;
         public FirstClass(){
              this.setSize(new Dimension(600,300));
              //this.setVisible(true); //remark this line
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              Dimension frameSize = this.getSize();
              int x = (screenSize.width/2) - (frameSize.width/2);
              int y = (screenSize.height/2) - (frameSize.height/2);
              this.setLocation(x,y);
              this.addWindowListener( new WindowAdapter(){
                   public void windowClosing(WindowEvent e){
                        System.exit(0);
              JPanel p = new JPanel();
              p.setBackground(Color.RED);
              p.setLayout(new BorderLayout());
              getContentPane().add(p);
              Label l = new Label();
              l.setText("Hello World");
              p.add(l);
              this.setVisible(true); //add this line
         public static void main(String[] args) {
              FirstClass fc = new FirstClass();
              fc.setTitle("Test");
    }

  • Problem to show a simple window when dowaloading ?

    Hi
    I created a pure java swing application. When run this app and when it start to download some files from server, it will take long time. I launched/new a simple JFrame window to show a simple "please waiting" message when downloading files. But only window frame show up, and no context. Actually I can see only empty frame and without any content. Only when downloading files is finished, content show up/come out. So I can not show some warning message to user when programm is downloading some files. Who can tell me what is problem?? I use Windw XP. Thanks in advance
    gary

    HI
    I have tried to open another thread and inside another thread I new/launch my GUI class. I met same problem (only show frame and context come out after download is finished). My coding is below: Thanks
    //my main class for downloading files from server
    public class download(){
    ShowProgress progress=new ShowProgress("waiting");
    progress.start();
    downloading .....
    //open new thread
    public class ShowProgress extends Thread {
    String text;
    public ShowProgress(String s) {
    super(s);
    text=s;
    public void run(){
    ShowEnd showend=new ShowEnd(text);
    //GUI class to show "wait message"
    public class ShowEnd extends JFrame {
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel1 = new JPanel();
    JEditorPane jEditorPane1 = new JEditorPane();
    public JButton Ok = new JButton();
    String text;
    public ShowEnd(String s) {
    super("Finished");
    text=s;
    try {
    jbInit();
    catch(Exception e) {
    e.printStackTrace();
    private void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    jEditorPane1.setBackground(Color.lightGray);
    jEditorPane1.setEditable(false);
    jEditorPane1.setText(text);
    jEditorPane1.setPreferredSize(new Dimension(400,120));
    Ok.setText("Ok");
    Ok.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    Ok_actionPerformed(e);
    this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
    jPanel1.add(Ok, null);
    this.getContentPane().add(jEditorPane1, BorderLayout.CENTER);
    this.pack();
    this.setVisible(true);
    void Ok_actionPerformed(ActionEvent e) {
    System.exit(0);
    }

  • Simple Keyboard input?

    I am a new programmer trying to create a simple keyboard input for a Jframe, if that is possible. Here is a simple jframe creation:
        public static void main(String[] args) {
            JFrame frame = new JFrame("Keyboard input");
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            // make window visible
            frame.pack();
            frame.setVisible(true);
        If I wanted to make a class that would put a ball in the frame and have the ball move up, down, left, or right according to which arrow keys were pressed and held- how would I do that?
    Thanks for your help,
    Sam

    See classes KeyListener and KeyAdapter
    And reed the Manual

  • JFrame validation

    Hi all,
    I have something that I can't understand. Please help.
    My problem is in simple JFrame defined like this :
    private JFrame frame = null;
    public void runFrame(){
              if (this.frame == null){
                   frame = new JFrame();
              frame.setBounds(10,10,900,600);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.getContentPane().setLayout(null);
              frame.getContentPane().addMouseListener(new MouseAdapter(){
                         public void mousePressed(MouseEvent e){
                        buttonAt(e.getX(), e.getY());
    }Then I have in same class, but in other method code like this
         protected void buttonAt(int xxx, int yyy){
              JButton button= new JButton("halo");
              button.setBounds(xx,yy,xxx,yyy);
              button.setVisible(true);
              frame.getContentPane().add(button);
              frame.validate();
         }and my problem is : Why I can see only some buttons created. Sometimes it validates three at once, sometimes two... but why this frame is not validating everytime the validation is called. Any help? Some improovement of this code could be great. Thanks a lot.
    Esteban

    Here it is...
    package test;
    import java.awt.Color;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    public class Main {
         protected static JFrame frame;
         protected static int xStart = 0, yStart = 0;
         public static void main(String[] args) {
              SwingUtilities.invokeLater(new Runnable(){
                   public void run(){
                        if (frame == null){
                             frame = new JFrame();
                        frame.setBounds(10,10,900,600);
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.getContentPane().setLayout(null);
                        frame.getContentPane().addMouseListener(new MouseAdapter(){
                             public void mousePressed(MouseEvent e){
                                  xStart = e.getX();
                                  yStart = e.getY();
                             public void mouseReleased(MouseEvent e){
                                  addButton(e.getX(), e.getY());
                        frame.setVisible(true);
         protected static void addButton(int xEnd, int yEnd){
              JButton button = new JButton("halo");
              button.setBackground(new Color(180,180,255));
              button.setBounds(xStart,yStart,xEnd - xStart,yEnd - yStart);
              button.setVisible(true);
              frame.getContentPane().add(button);
              frame.getContentPane().validate();
    }Hope you'll help me now...

  • Trying to use SetLayeredWindowAttributes ...

    Hello all, I've been experimenting with http://www.javaworld.com/javaworld/javatips/jw-javatip86.html and I'm currently trying to get transparency with JNI using SetLayeredWindowAttributes ... However I'm running into problems (duh). Anyway, here's my source code, all I'm trying to do is adding 50% transparency to the smiley face.
    #include <afxwin.h>
    #include <WinUser.h>
    #include <assert.h>
    #include "jawt_md.h"
    #include "MyWindow.h"
    #define X(x) (int)(xLeft + (x)*xScale/100)  // Scaling macros
    #define Y(y) (int)(yTop + (y)*yScale/100)   // so scale is 0 - 100
    #define CX(x) (int)((x)*xScale/100)
    #define CY(y) (int)((y)*yScale/100)
    #define _WIN32_WINNT 0x0500
    #define WINVER 0x0500
    #define LWA_ALPHA  0x00000002
    #define WS_EX_LAYERED  0x00080000
    void DrawSmiley(HWND hWnd, HDC hdc);
    HRGN hrgn = NULL;
    JNIEXPORT void JNICALL
    Java_MyWindow_paint(JNIEnv* env, jobject canvas, jobject graphics)
            JAWT awt;
            JAWT_DrawingSurface* ds;
            JAWT_DrawingSurfaceInfo* dsi;
            JAWT_Win32DrawingSurfaceInfo* dsi_win;
            jboolean result;
            jint lock;
            // Get the AWT
            awt.version = JAWT_VERSION_1_3;
            result = JAWT_GetAWT(env, &awt);
            assert(result != JNI_FALSE);
            // Get the drawing surface
            ds = awt.GetDrawingSurface(env, canvas);
            if(ds == NULL)
                return;
            // Lock the drawing surface
            lock = ds->Lock(ds);
            assert((lock & JAWT_LOCK_ERROR) == 0);
            // Get the drawing surface info
            dsi = ds->GetDrawingSurfaceInfo(ds);
            // Get the platform-specific drawing info
            dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
            HDC hdc = dsi_win->hdc;
            HWND hWnd = dsi_win->hwnd;
    // This is the critical portion of the code that I'm having trouble with!          
         SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
         int i = SetLayeredWindowAttributes( hWnd, 0, 128, LWA_ALPHA );
    // etc... the rest of the code is the same as in the tutorial.The code compiles and runs, but there are two problems:
    1. SetWindowLong and GetWindowLong both give me "Access is denied" errors.
    2. SetLayeredWindowAttributes gives me a "The parameter is incorrect." error. (I have no idea why...)
    Can anyone help me out with this?

    My guess is you are trying to set the transparency of a non top-level window, and I strongly doubt if this is even possible. That is maybe why you are getting the "Access is denied". For testing purposes, my best bet is to try making a simple JFrame, and set its transparency to see if it works. If it does, then my guess about top-level windows is correct (don't do it in the paint () method, just create a small method named setTransparent or something and pass a Frame as a parameter if you want).

Maybe you are looking for