LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Inconsistent behaviour of java.awt in Linux/Windows (http://www.linuxquestions.org/questions/programming-9/inconsistent-behaviour-of-java-awt-in-linux-windows-361077/)

liangzan 09-07-2005 10:35 AM

Inconsistent behaviour of java.awt in Linux/Windows
 
hi,

i'm just trying out some simple java gui programming using awt classes.

Code:

/*
Calculator_GUI.java
*/

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//start of class
public class Calculator_GUI extends JFrame {

//attributes
private JTextField textBox;
private JTextArea answerBox, msgBox;
private JButton resetBtn, suggestBtn;


//constructor
public Calculator_GUI() {
        super("Calculator");
       
        Container container = getContentPane();
        container.setLayout(new FlowLayout());

        //text box for input
        textBox = new JTextField("0", 10);
        textBox.setEditable(true);
        container.add(textBox);
       
        //reset button
        resetBtn = new JButton("RESET");
        container.add(resetBtn);
       
        //register the event handlers
        TextFieldHandler fieldHandler = new TextFieldHandler();
        textBox.addActionListener(fieldHandler);
       
        ButtonHandler btnHandler = new ButtonHandler();
        resetBtn.addActionListener(btnHandler);

        setSize(400, 300);
        setVisible(true);

       
}

//inner class for handling textbox events
private class TextFieldHandler implements ActionListener {
       
        //handle textbox event
        public void actionPerformed(ActionEvent e) {}
}


//inner class for handling button events
private class ButtonHandler implements ActionListener {
       
        //handle button event
        public void actionPerformed(ActionEvent e) {
                String text = textBox.getText();
                JOptionPane.showMessageDialog(null, text);
                textBox.setText("0");

        }
}


//main function
public static void main (String args[]) {

        Calculator_GUI cal = new Calculator_GUI();
        cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

} //close brkt for class

the code is very simple. just a text field and a reset button.

i get funny problems when i try to run this on linux. the main problm is with the text editing. it doesn't show the real time changes when i try to edit. nor do i see the blinking cursor. i got frustrated, and tried running the class file on windows. and it worked! now, isn't java designed to be portable?? why am i getting inconsistent behaviour across platforms?? odd... do i really need to switch to windows??

anyone knows why??

platform: FC4
java 1.5

jlliagre 09-07-2005 11:58 AM

Your code seems to works for me on Solaris, with both JDK 1.4 and 1.5.

Are you sure of the JVM you use on FC ?

Can you explain more what happens on Linux, you do not see the text entered in the textfield ? never ?
Did you try with a different window manager ?

liangzan 09-08-2005 09:34 AM

thanks for testing it on solaris jlliagre! i tried it out on both KDE and Gnome, it gives me the exact same funny behaviour.

the text field is editable only at the first time. after i pressed the 'reset', the text field is frozed. i cant type in anything. the textfield does get back to '0', but you cant do anything with it after the first time. isn't that mightily odd? i don't get that in windows.

i wonder if its the JVM. i can only wonder. how do you check its its the culprit? :scratch:


All times are GMT -5. The time now is 07:24 PM.