How do I use Java Swing in Eclipse on Mac OS X?

14,869

On Mac OS X 10.5.8, Eclipse 3.4.2 and Xcode 3.1.4, the example below builds and runs using recent revisions of either Java 1.5 or Java 1.6. As Xcode includes the JDK, what version of Mac OS X and Xcode are you using?

Also verify that your Swing project hasn't inadvertently included SWT.

Addendum: Check these two dialogs:

Eclipse > Preferences > Java > Build Path
Eclipse > Preferences > Java > Installed JREs

You should see references to /System/Library/Frameworks/JavaVM.framework/.

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class X {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JPanel() {
            private final int SIZE = 200;
            private final int INSET = 20;
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(SIZE, SIZE);
            }
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                g2.setColor(Color.blue);
                Line2D line1 = new Line2D.Double(INSET, INSET,
                    getWidth() - INSET, getHeight() - INSET);
                Line2D line2 = new Line2D.Double(getWidth() - INSET,
                    INSET, INSET, getHeight() - INSET);
                g2.setStroke(new BasicStroke(16,
                    BasicStroke.CAP_ROUND,
                    BasicStroke.JOIN_BEVEL));
                g2.draw(line1);
                g2.draw(line2);
            }
        });
        frame.pack();
        frame.setVisible(true);
    }
}
Share:
14,869

Related videos on Youtube

Jonas
Author by

Jonas

Passionated Software Developer interested in Distributed Systems

Updated on May 14, 2022

Comments

  • Jonas
    Jonas 18 minutes

    I am new to Mac OS X and is using version 10.6.5. JDK 1.6_u22 seems to be preinstalled on the system. I have downloaded Eclipse 3.5.2.

    It works fine if I create a simple hello world, but I can not import JFrame or use Swing. The error message that I get is:

    Access restriction: The type JFrame is not accessible due to restriction on required library /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar

    Here is the simple program that I have tried with:

    import javax.swing.JFrame;
    public class Test {
        public static void main(String[] args) {
            new JFrame();
        }
    }
    

    How do I use Java Swing in Eclipse on Mac OS X?

    • Mot
      Mot over 11 years
      Unfortunately, I can't help you with Eclipse, because I'm using a real Java-IDE: IntelliJ. ;) What happens if you try to launch it from command line?
    • David Gelhar
      David Gelhar
    • Mot
      Mot
      Did you post the entire Test class? Are you using the empty package or maybe package javas.swing;?
    • David Gelhar
      David Gelhar
      @Jonas the answer to that question suggests that the error message can result from a jar file in your classpath that's trying to replace a standard class.
    • Jonas
      Jonas
      @David: I don't understand how that question help me. JFrame is a standard class in Java SE.
    • Jonas
      Jonas
      @mklhmnn: yes, it's the entire program. I get the same error if I use package com.example.
  • Jonas
    Jonas over 11 years
    I'm using Xcode 3.2.5. The computer is brand new and I have updated all software.
  • Jonas
    Jonas over 11 years
    Under Eclipse > Preferences > Java > Installed JREs the JVM 1.4 was selected, I selected JVM 1.6.0 instead and created a new project. Now is it working. Thanks.