tags:

views:

45

answers:

1

Hey guys, see this snipped of code:

JFrame loginFrame = new JFrame("Login");

  loginFrame.setSize(500,400);
  JPanel loginPanel = new JPanel();
  loginPanel.setLayout(new GridLayout(0,2));

  JLabel header = new JLabel("Login");
  JLabel header2 = new JLabel("blahvlah");
  JLabel loginLabel = new JLabel("Login");
  JTextField loginField = new JTextField(24);
  JLabel passLabel = new JLabel("Password");
  JTextField passField = new JTextField(24);

  loginPanel.add(header);
  loginPanel.add(header2);
  loginPanel.add(loginLabel);
  loginPanel.add(loginField);
  loginPanel.add(passLabel);
  loginPanel.add(passField);

  loginFrame.add(loginPanel);
  loginFrame.setVisible(true);
loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

the JTextFields are present on the app, but i cannot enter anything into them? There is no cursor either, what is the problem? Also my exit on close does not seem to work?

thanks

+6  A: 

The code you posted does not compile (on the last line you typed loginframe instead of loginFrame). Having fixed that, the following program:

public class JFrameTest {
    public static void main(String[] args) {
        JFrame loginFrame = new JFrame("Login");

        loginFrame.setSize(500,400);
        JPanel loginPanel = new JPanel();
        loginPanel.setLayout(new GridLayout(0,2));

        JLabel header = new JLabel("Login");
        JLabel header2 = new JLabel("blahvlah");
        JLabel loginLabel = new JLabel("Login");
        JTextField loginField = new JTextField(24);
        JLabel passLabel = new JLabel("Password");
        JTextField passField = new JTextField(24);

        loginPanel.add(header);
        loginPanel.add(header2);
        loginPanel.add(loginLabel);
        loginPanel.add(loginField);
        loginPanel.add(passLabel);
        loginPanel.add(passField);

        loginFrame.add(loginPanel);
        loginFrame.setVisible(true);
        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

runs just fine. I can enter text in both JTextFields and can also close the JFrame:

alt text

Bart Kiers
it looks quite different on my system, im running linux and using eclipse, maybe my jdk isnt set up properly?
KP65
I've resized the JFrame a bit before creating a screenshot of it. And the GUI components may differ based on the underlying Window manager (I'm running Ubuntu+Gnome). Did you run the class I posted? What JRE do you have installed? I recommend creating a new class `JFrameTest` and copy the above in it. Then compile and run it from your command line.
Bart Kiers
Great answer/level of detail. Love the fact that you added the screenshot.
Bill K
The default Linux JDK tends to suck pretty hard. You might try to grab the distro from Sun if you haven't, and make sure that your classpath points to the correct libraries for the JDK that you are using.
Bill K
Thanks Bill. Yeah, I also have Sun's JRE installed (no IcedTea or GCJ heavens forbid!). Although I've always been content with Eclipse's compiler.
Bart Kiers
right eclipse says im using java-1-5-0-gcj, i have sun java 6 installed as well, how can i switch it over?
KP65
Get rid of GCJ! With a bit of luck, Eclipse will fall back on Sun's JRE.
Bart Kiers
cheers, sorted it now thanks
KP65
You should run the UI code in the AWT thread by wrapping it a Runnable and passing to SwingUtilities invokeLater.
Steve Kuo
Bart Kiers
@keval: good to hear it!
Bart Kiers