Choice in AWT

Can anyone tell me how to make the items in tthe choice in java.awt, editable

If you mean that you want a Drop Down Listbox like in VB or C++ in MS Land then you use a JComboBox.
Choice is not editable other than changing the list of items that you can choose from.

Similar Messages

  • Help Needed with Itemlsteners for Choice in AWT

    Hi,
    I am new to AWT prgramming .
    I have created a menu called " Menu System Test window " and it contains toplevel menu item "File" and File Menu contains subitems "Product Evaluation" and "Exit".
    When i click File->Product Evaluation it displays a new Frame named "ProductEvaluationMeasurementTool" with some check boxes ,radiobuttons, buttons , text fields and choice lists.
    The two Choices ie c1 and c2 contains items choice,choice2 and scale 1 and scale 2 respectively and i have added ItemListeners for both the choices.
    My question is when i select Both choice1 and scale 1 it shoulddisplay a new frame.
    (NOTE : THE NEW FRAME SHOULD BE DISPLAYED IF I SELECT BOTH CHOICE1 AND SCALE1 NOT EITHER CHOICE1 OR SCALE1)
    Similarly when i select Both choice2 and scale2 it should display another new frame. How can i do that? i am sending my code.
    Thanks in advance
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Component;
    import java.awt.Checkbox;
    import javax.swing.*;
    // Make a main window with a top-level menu: File 
    public class MainWindow extends Frame {
      public MainWindow() {
        super("Menu System Test Window");
        setSize(500, 500);
        // make a top level File menu
        FileMenu fileMenu = new FileMenu(this);
        // make a menu bar for this frame 
        // and add top level menus File and Menu
        MenuBar mb = new MenuBar();
        mb.add(fileMenu);
        setMenuBar(mb);
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            exit();
      public void exit() {
        setVisible(false); // hide the Frame
        dispose(); // tell windowing system to free resources
        System.exit(0); // exit
      public static void main(String args[]) {
        MainWindow w = new MainWindow();
        w.setVisible(true);
    // Encapsulate the look and behavior of the File menu
    class FileMenu extends Menu implements ActionListener {
        private MainWindow mw; // who owns us?
      private MenuItem itmPE   = new MenuItem("ProductEvaluation");
      private MenuItem itmExit = new MenuItem("Exit");
      public FileMenu(MainWindow main)
        super("File");
        this.mw = main;
        this.itmPE.addActionListener(this);
        this.itmExit.addActionListener(this);
        this.add(this.itmPE);
        this.add(this.itmExit);
      // respond to the Exit menu choice
      public void actionPerformed(ActionEvent e)
        if (e.getSource() == this.itmPE)
         Frame f = new Frame("ProductMeasurementEvaluationTool");
         f.setSize(1290,1290);
         f.setLayout(null);
         TextField t1 = new TextField("textField1");
         t1.setBounds(230, 630, 50, 24);
         f.add(t1);
         TextField t2 = new TextField("textField2");
         t2.setBounds(430, 630, 50, 24);
         f.add(t2);
         Choice c1 = new Choice();
         c1.addItem("Choice1");
         c1.addItem("Choice2");
         c1.addItemListener(new MyItemListener());
         f.add(c1);
         c1.setBounds(630, 600, 120, 24);
         Choice c2 = new Choice();
         c2.addItem("scale1");
         c2.addItem("scale2");
         c2.addItemListener(new MyItemListener());
         f.add(c2);
         c2.setBounds(840, 600, 120, 24);
         Label l1 = new Label("Select the appropriate metrics for Measurement Process Evaluation");
         l1.setBounds(380, 50, 380, 20);
         f.add(l1);
         Label l2 = new Label("Architecture Metrics");
         l2.setBounds(170, 100, 110, 20);
         f.add(l2);
         Label l3 = new Label("RunTime Metrics");
         l3.setBounds(500, 100, 110, 20);
         f.add(l3);
         Label l4 = new Label("Documentation Metrics");
         l4.setBounds(840, 100, 130, 20);
         f.add(l4);
         JRadioButton rb1 = new JRadioButton("Componenent Metrics",false);
         rb1.setBounds(190, 140, 133, 20);
         f.add(rb1);
         JRadioButton rb2 = new JRadioButton("Task Metrics",false);
         rb2.setBounds(540, 140, 95, 20);
         f.add(rb2);
         JRadioButton rb3 = new JRadioButton("Manual Metrics",false);
         rb3.setBounds(870, 140, 108, 20);
         f.add(rb3);
         JRadioButton rb4 = new JRadioButton("Configuration Metrics",false);
         rb4.setBounds(190, 270, 142, 20);
         f.add(rb4);
         JRadioButton rb6 = new JRadioButton("DataHandling Metrics",false);
         rb6.setBounds(540, 270, 142, 20);
         f.add(rb6);
         JRadioButton rb8 = new JRadioButton("Development Metrics",false);
         rb8.setBounds(870, 270, 141, 20);
         f.add(rb8);
         Checkbox  c10 = new Checkbox("Size");
         c10.setBounds(220, 170, 49, 20);
         f.add(c10);
         Checkbox c11 = new Checkbox("Structure");
         c11.setBounds(220, 190, 75, 20);
         f.add(c11);
         Checkbox c12 = new Checkbox("Complexity");
         c12.setBounds(220, 210, 86, 20);
         f.add(c12);
         Checkbox c13 = new Checkbox("Size");
         c13.setBounds(220, 300, 49, 20);
         f.add(c13);
         Checkbox c14 = new Checkbox("Structure");
         c14.setBounds(220, 320, 75, 20);
         f.add(c14);
         Checkbox c15 = new Checkbox("Complexity");
         c15.setBounds(220, 340, 86, 20);
         f.add(c15);
         Checkbox c19 = new Checkbox("Size");
         c19.setBounds(580, 170, 49, 20);
         f.add(c19);
         Checkbox c20 = new Checkbox("Structure");
         c20.setBounds(580, 190, 75, 20);
         f.add(c20);
         Checkbox c21 = new Checkbox("Complexity");
         c21.setBounds(580, 210, 86, 20);
         f.add(c21);
         Checkbox c22 = new Checkbox("Size");
         c22.setBounds(580, 300, 49, 20);
         f.add(c22);
         Checkbox c23 = new Checkbox("Structure");
         c23.setBounds(580, 320, 75, 20);
         f.add(c23);
         Checkbox c24 = new Checkbox("Complexity");
         c24.setBounds(580, 340, 86, 20);
         f.add(c24);
         Checkbox c28 = new Checkbox("Size");
         c28.setBounds(920, 170, 49, 20);
         f.add(c28);
         Checkbox c29 = new Checkbox("Structure");
         c29.setBounds(920, 190, 75, 20);
         f.add(c29);
         Checkbox c30 = new Checkbox("Complexity");
         c30.setBounds(920, 210, 86, 20);
         f.add(c30);
         Checkbox c31 = new Checkbox("Size");
         c31.setBounds(920, 300, 49, 20);
         f.add(c31);
         Checkbox c32 = new Checkbox("Structure");
         c32.setBounds(920, 320, 75, 20);
         f.add(c32);
         Checkbox c33 = new Checkbox("Complexity");
         c33.setBounds(920, 340, 86, 20);
         f.add(c33);
         ActionListener action = new MyActionListener(f, t1, t2);
         Button b1  = new Button("Button1");
         b1.setBounds(230, 600, 120, 24);
         b1.addActionListener(action);
         f.add(b1);
         Button b2  = new Button("Button2");
         b2.setBounds(430, 600, 120, 24);
         b2.addActionListener(action);
         f.add(b2);
               f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
              System.exit(0);
         f.show();
        else
       { mw.exit();}
    class MyActionListener implements ActionListener
      private Frame     frame;
      private TextField textField1;
      private TextField textField2;
      public MyActionListener(Frame frame, TextField tf1, TextField tf2)
        this.frame = frame;
        this.textField1 = tf1;
        this.textField2 = tf2;
      public void actionPerformed(ActionEvent e)
        String s = e.getActionCommand();
        if (s.equals("Button1"))
            Component[] components = this.frame.getComponents();
      int numOfCheckBoxes = 81;
      int numChecked = 0;
      for (int i = 0; i < components.length; i++)
            if (components[i] instanceof Checkbox)
              if (((Checkbox)components).getState())
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    this.textField1.setText(Double.toString(ratio));
    else
    if (s.equals("Button2"))
    Component[] components = this.frame.getComponents();
    int numOfCheckBoxes = 81;
    int numChecked = 0;
    for (int i = 0; i < components.length; i++)
    if (components[i] instanceof Checkbox)
    if (((Checkbox)components[i]).getState())
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    this.textField2.setText(Double.toString(ratio));
    class MyItemListener implements ItemListener {
         public void itemStateChanged(ItemEvent e) {
                   String command = (String) e.getItem();

    The easy answer is that you need to add state to one of your classes. What does this mean? Somewhere you will have to keep track of which items are selected in each Choice. There are a lot of ways to do this. One way is to pass a reference for each Choice, ie, c1 and c2, in to the constructor of MyItemListener and for each ItemEvent.SELECTED type of event:
    1 - check the selected item of each Choice and see if they have the same ending number, or are the same index in an array kept in your MyItemListener class or ... many ways;
    2 - decide what to do depending on what you find after checking.
    Since you have a separate class for the ItemListener this might be a good place to keep track of the selection status of the two Choice components. The member variables you use to keep track of things is the state.
    Another thing to consider is whether to add a separate listener to each Choice or to create a single instance and add it to each component. If you add a separate one then the Choice selections won't know about each other - each separate listener receives events from only one of the components. If you use a single instance for both components then you can get selection information from each of the Choice components. This way you can track things, or keep state, inside the class.
    I abbreviated the class name so you can run this without name-clashing in your computer.
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Component;
    import java.awt.Checkbox;
    import javax.swing.*;
    public class MW extends Frame {
        public MW() {
            super("Menu System Test Window");
            setSize(500, 500);
            // make a top level File menu
            FileMenu fileMenu = new FileMenu(this);
            // make a menu bar for this frame 
            // and add top level menus File and Menu
            MenuBar mb = new MenuBar();
            mb.add(fileMenu);
            setMenuBar(mb);
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    exit();
        public void exit() {
            setVisible(false); // hide the Frame
            dispose();         // tell windowing system to free resources
            System.exit(0);    // exit
        public static void main(String args[]) {
            MW w = new MW();
            w.setVisible(true);
    // Encapsulate the look and behavior of the File menu
    class FileMenu extends Menu implements ActionListener {
        private MW mw; // who owns us?
        private MenuItem itmPE   = new MenuItem("ProductEvaluation");
        private MenuItem itmExit = new MenuItem("Exit");
        public FileMenu(MW main) {
            super("File");
            this.mw = main;
            this.itmPE.addActionListener(this);
            this.itmExit.addActionListener(this);
            this.add(this.itmPE);
            this.add(this.itmExit);
        // respond to the Exit menu choice
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == this.itmPE) {
                final Frame f = new Frame("ProductMeasurementEvaluationTool");
                f.setSize(1290,1290);
                f.setLayout(null);
                MyItemListener itemListener = new MyItemListener(mw);
                TextField t1 = new TextField("textField1");
                t1.setBounds(230, 630, 50, 24);
                f.add(t1);
                TextField t2 = new TextField("textField2");
                t2.setBounds(430, 630, 50, 24);
                f.add(t2);
                Choice c1 = new Choice();
                c1.addItem("Choice1");
                c1.addItem("Choice2");
                c1.addItemListener(itemListener);
                f.add(c1);
                c1.setBounds(630, 600, 120, 24);
                Choice c2 = new Choice();
                c2.addItem("scale1");
                c2.addItem("scale2");
                c2.addItemListener(itemListener);
                f.add(c2);
                c2.setBounds(840, 600, 120, 24);
                Label l1 = new Label("Select the appropriate metrics for " +
                                     "Measurement Process Evaluation");
                l1.setBounds(380, 50, 380, 20);
                f.add(l1);
                Label l2 = new Label("Architecture Metrics");
                l2.setBounds(170, 100, 110, 20);
                f.add(l2);
                Label l3 = new Label("RunTime Metrics");
                l3.setBounds(500, 100, 110, 20);
                f.add(l3);
                Label l4 = new Label("Documentation Metrics");
                l4.setBounds(840, 100, 130, 20);
                f.add(l4);
                JRadioButton rb1 = new JRadioButton("Componenent Metrics",false);
                rb1.setBounds(190, 140, 133, 20);
                f.add(rb1);
                JRadioButton rb2 = new JRadioButton("Task Metrics",false);
                rb2.setBounds(540, 140, 95, 20);
                f.add(rb2);
                JRadioButton rb3 = new JRadioButton("Manual Metrics",false);
                rb3.setBounds(870, 140, 108, 20);
                f.add(rb3);
                JRadioButton rb4 = new JRadioButton("Configuration Metrics",false);
                rb4.setBounds(190, 270, 142, 20);
                f.add(rb4);
                JRadioButton rb6 = new JRadioButton("DataHandling Metrics",false);
                rb6.setBounds(540, 270, 142, 20);
                f.add(rb6);
                JRadioButton rb8 = new JRadioButton("Development Metrics",false);
                rb8.setBounds(870, 270, 141, 20);
                f.add(rb8);
                Checkbox  c10 = new Checkbox("Size");
                c10.setBounds(220, 170, 49, 20);
                f.add(c10);
                Checkbox c11 = new Checkbox("Structure");
                c11.setBounds(220, 190, 75, 20);
                f.add(c11);
                Checkbox c12 = new Checkbox("Complexity");
                c12.setBounds(220, 210, 86, 20);
                f.add(c12);
                Checkbox c13 = new Checkbox("Size");
                c13.setBounds(220, 300, 49, 20);
                f.add(c13);
                Checkbox c14 = new Checkbox("Structure");
                c14.setBounds(220, 320, 75, 20);
                f.add(c14);
                Checkbox c15 = new Checkbox("Complexity");
                c15.setBounds(220, 340, 86, 20);
                f.add(c15);
                Checkbox c19 = new Checkbox("Size");
                c19.setBounds(580, 170, 49, 20);
                f.add(c19);
                Checkbox c20 = new Checkbox("Structure");
                c20.setBounds(580, 190, 75, 20);
                f.add(c20);
                Checkbox c21 = new Checkbox("Complexity");
                c21.setBounds(580, 210, 86, 20);
                f.add(c21);
                Checkbox c22 = new Checkbox("Size");
                c22.setBounds(580, 300, 49, 20);
                f.add(c22);
                Checkbox c23 = new Checkbox("Structure");
                c23.setBounds(580, 320, 75, 20);
                f.add(c23);
                Checkbox c24 = new Checkbox("Complexity");
                c24.setBounds(580, 340, 86, 20);
                f.add(c24);
                Checkbox c28 = new Checkbox("Size");
                c28.setBounds(920, 170, 49, 20);
                f.add(c28);
                Checkbox c29 = new Checkbox("Structure");
                c29.setBounds(920, 190, 75, 20);
                f.add(c29);
                Checkbox c30 = new Checkbox("Complexity");
                c30.setBounds(920, 210, 86, 20);
                f.add(c30);
                Checkbox c31 = new Checkbox("Size");
                c31.setBounds(920, 300, 49, 20);
                f.add(c31);
                Checkbox c32 = new Checkbox("Structure");
                c32.setBounds(920, 320, 75, 20);
                f.add(c32);
                Checkbox c33 = new Checkbox("Complexity");
                c33.setBounds(920, 340, 86, 20);
                f.add(c33);
                ActionListener action = new MyActionListener(f, t1, t2);
                Button b1  = new Button("Button1");
                b1.setBounds(230, 600, 120, 24);
                b1.addActionListener(action);
                f.add(b1);
                Button b2  = new Button("Button2");
                b2.setBounds(430, 600, 120, 24);
                b2.addActionListener(action);
                f.add(b2);
                f.addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        //System.exit(0);
                        f.dispose();
                f.setVisible(true);
            else {
                mw.exit();
    class MyActionListener implements ActionListener { 
        private Frame     frame;
        private TextField textField1;
        private TextField textField2;
        public MyActionListener(Frame frame, TextField tf1, TextField tf2) {
            this.frame = frame;
            this.textField1 = tf1;
            this.textField2 = tf2;
        public void actionPerformed(ActionEvent e) {
            String s = e.getActionCommand();
            if (s.equals("Button1")) {
                Component[] components = this.frame.getComponents();
                int numOfCheckBoxes = 81;
                int numChecked = 0;
                for (int i = 0; i < components.length; i++)
                    if (components[i] instanceof Checkbox)
                        if (((Checkbox)components).getState())
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    this.textField1.setText(Double.toString(ratio));
    else if (s.equals("Button2")) {
    Component[] components = this.frame.getComponents();
    int numOfCheckBoxes = 81;
    int numChecked = 0;
    for (int i = 0; i < components.length; i++)
    if (components[i] instanceof Checkbox)
    if (((Checkbox)components[i]).getState())
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    this.textField2.setText(Double.toString(ratio));
    class MyItemListener implements ItemListener {
    Frame owner;
    Dialog dialog;
    Label label;
    boolean haveScale1, haveScale2, haveChoice1, haveChoice2;
    public MyItemListener(MW mw) {
    owner = (Frame)mw;
    initializeDialog();
    haveScale1 = true;
    haveScale2 = false;
    haveChoice1 = true;
    haveChoice2 = false;
    public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange() == ItemEvent.SELECTED) {
    String command = (String) e.getItem();
    System.out.println("command = " + command);
    if(command.equals("scale1")) {
    haveScale1 = true;
    haveScale2 = false;
    if(command.equals("scale2")) {
    haveScale2 = true;
    haveScale1 = false;
    if(command.equals("Choice1")) {
    haveChoice1 = true;
    haveChoice2 = false;
    if(command.equals("Choice2")) {
    haveChoice2 = true;
    haveChoice1 = false;
    if(haveScale1 && haveChoice1)
    launchDialog(1);
    if(haveScale2 && haveChoice2)
    launchDialog(2);
    private void launchDialog(int n) {
    label.setText("scale" + n + " and Choice" + n + " chosen");
    dialog.setVisible(true);
    private void initializeDialog() {
    label = new Label();
    label.setAlignment(Label.CENTER);
    dialog = new Dialog(owner, "item listener dialog", false);
    dialog.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    dialog.dispose();
    dialog.add(label); // to center of default BorderLayout
    dialog.setSize(200,100);
    dialog.setLocation(300,300);

  • Height of "Choice" in AWT.

    I have used a choice box in my Application Login Dialog.I mentioned the height of the choice box as '25'.and i was using jdk1.1.8 for my application.Everything was working fine.Now my client wanted me to port the application to jdk1.3.1. when i tried to run the same application in 1.3.1, i was surprised to see that the choice box is appearing smaller by 5 pixcels.where as the size of other components like the text field and panel where the same.Has anyone faced this problem.Does anyone have a solution for this.

    This has been discussed a lot allready. Search the forum!
    Ragnvald Barth
    Software enigneerhttp://blake.prohosting.com/ragnvald

  • Invisible text in choice box!!!! HELP URGENT PLZ!

    after adding additems to a choice in awt, i cannot see the text, but the default option, but the list is white, the elements are there, I can click on them, but i cannot see the text ... can anyone help me ?

    By default it should be black ???
    myChoiceBox.setForeground(Color. ---- );

  • Choice item's value problem

    can I set the value of choice's String items?
    just like those in jsp
    for a jsp example
    <SELECT NAME="selectedfield" style=" WIDTH: 100px" Multiple>
    <OPTION value="ID">my ID</OPTION>
    <OPTION value="date">my birthday</OPTION>
    <OPTION value="money">money in wallet</OPTION>
    I means item's name not equals to item's value!
    can I achieve this in java awt choice?

    No, a Choice in AWT contains only String values and it's those String values that get displayed. There's no distinction between model and view.
    JComboBox in Swing has a more flexible approach whereby you can put an Object into the items and specify how it gets rendered separately.
    Hope this helps.

  • Flash - Java - SerialData Translation issues

    This is for an art installation we're building in a gallery here in NYC. It opens Saturday, and we are in a jam with a problem.
    I'm using Dan O'Sullivan's Serial Server http://itp.nyu.edu/~dbo3/SerialServer/SerialServer.html , which is a little java server that connects to Flash via XML socket and to the serial port. It passes data back and forth since Flash doesn't talk to the serial port.
    The problem is this: Flash won't put the incoming data into the object and trigger the onData handler until it sees a null byte delimiter. On the other side, my serial device -- an encoder & adapter -- has no interest in producing a delimiter and cannot be programmed to do so. The data from the encoder is generally in the form of 4 bytes. You send it 1 byte to poll it.
    Thus the server app has to step in and patch things up, either (a) by indiscriminately appending 0's to everything it sees from the encoder (and figuring it all out in Flash) or better, (b) by having the server do the polling and keeping the current 4 bytes in a string, ready for when Flash requests it.
    This doesn't seem too bad - the server code is relatively simple - but I'm not a Java programmer (sorry, wish I were). I'm inserting the code here - any chance someone can help us out ... immediately?? Thanks very much.
    /Eric Liftin
    MESH Architectures http://mesh-arc.com
    * SerialServer.java
    * Title:               SerialServer
    * Description:          This relays bytes between a serial port and a socket connection.
    * @author               Administrator
    * @version               
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Frame;
    import java.awt.Toolkit;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.StringTokenizer;
    public class SerialServer extends java.applet.Applet  implements SerialListener {
         // IMPORTANT: Source code between BEGIN/END comment pair will be regenerated
         // every time the form is saved. All manual changes will be overwritten.
         // BEGIN GENERATED CODE
         // member declarations
         java.awt.Choice baud = new java.awt.Choice();
         java.awt.Checkbox interpretation = new java.awt.Checkbox();
         java.awt.Choice ports = new java.awt.Choice();
         java.awt.TextField socketPort = new java.awt.TextField();
         java.awt.Label softwarePort = new java.awt.Label();
         java.awt.Button sendToSocket = new java.awt.Button();
         java.awt.Button sendToSerial = new java.awt.Button();
         java.awt.TextField sendText = new java.awt.TextField();
         java.awt.TextArea traffic = new java.awt.TextArea();
         java.awt.Checkbox debugButton = new java.awt.Checkbox();
         java.awt.Label socketStatus = new java.awt.Label();
         java.awt.Label serialStatus = new java.awt.Label();
         java.awt.Label numbersFootnote = new java.awt.Label();
         java.awt.Button clearButton = new java.awt.Button();
         java.awt.Button cover = new java.awt.Button();
         // END GENERATED CODE
         String serialType = null;
         static Socketer mySocket;
         static Serial mySerial;
         boolean isStandalone = false;
         static boolean debug = false;
         public SerialServer() {}
         static HashMap argsHash;
         int substituteChar = 0;
         public void start() {
              //figure out what driver to use
              if (isStandalone == false) {
                        serialType =  getParameter("SerialDriver");
                   } else {
                        serialType = (String) argsHash.get("SerialDriver");
              if( serialType != null){  //if they did not specify
                   serialType= serialType.toLowerCase();
              newSerial();
              if (isStandalone == false) {
                   System.out.println("This is an Applet");
                   useParameters(getParameter("Baud"), getParameter("SerialPort"), getParameter("SocketPort"), getParameter("Sub0ForChar"), getParameter("SerialDriver"));
              } else {
                   useParameters((String) argsHash.get("Baud"), (String) argsHash.get("SerialPort"), (String) argsHash.get("SocketPort"), (String) argsHash.get("Sub0ForChar"),(String) argsHash.get("SerialDriver"));
              newSocket();
              connectSerial();
              //          event handling
              baud.addItemListener(new java.awt.event.ItemListener() {
                   public void itemStateChanged(java.awt.event.ItemEvent e) {
                        baudItemStateChanged(e);
              interpretation.addItemListener(new java.awt.event.ItemListener() {
                   public void itemStateChanged(java.awt.event.ItemEvent e) {
                        interpretationItemStateChanged(e);
              ports.addItemListener(new java.awt.event.ItemListener() {
                   public void itemStateChanged(java.awt.event.ItemEvent e) {
                        portsItemStateChanged(e);
              socketPort.addTextListener(new java.awt.event.TextListener() {
                   public void textValueChanged(java.awt.event.TextEvent e) {
                        socketPortTextValueChanged(e);
              sendToSocket.addActionListener(new java.awt.event.ActionListener() {
                   public void actionPerformed(java.awt.event.ActionEvent e) {
                        sendToSocketPerformed(e);
              sendToSerial.addActionListener(new java.awt.event.ActionListener() {
                   public void actionPerformed(java.awt.event.ActionEvent e) {
                        sendToSerialActionPerformed(e);
              sendText.addTextListener(new java.awt.event.TextListener() {
                   public void textValueChanged(java.awt.event.TextEvent e) {
                        sendTextTextValueChanged(e);
              ////     traffic.addTextListener(new java.awt.event.TextListener() {
              //     public void textValueChanged(java.awt.event.TextEvent e) {
              //          trafficTextValueChanged(e);
              debugButton.addItemListener(new java.awt.event.ItemListener() {
                   public void itemStateChanged(java.awt.event.ItemEvent e) {
                        debugItemStateChanged(e);
              clearButton.addActionListener(new java.awt.event.ActionListener() {
                   public void actionPerformed(java.awt.event.ActionEvent e) {
                        clearActionPerformed(e);
              //populatePorts();
         // Initialize the applet
         public void init() {
              try {
                   initComponents();
              } catch (Exception e) {
                   e.printStackTrace();
         public void useParameters(String _baud, String _serialPort, String _socketPort, String _subChar, String _serialType) {
              System.out.println("Input Parameters Baud:" + _baud + " SerialPort:" + _serialPort + " SocketPort:" + _socketPort + " SubChar:" + _subChar+ " SerialType:" + _serialType);
              if (_baud != null) {
                   baud.select(_baud);
              if (_subChar != null) {
                   substituteChar = Integer.parseInt(_subChar);
              } else {
                   substituteChar = 0;
              //else{
              //          baud.select("9600");
              if (_serialPort != null) {
                   ports.select(_serialPort);
              } //else{
              //     ports.select("COM1") ;
              //if ((_baud != null) || (_serialPort != null)){
              //     newSerial();
              if (_socketPort != null) {
                   socketPort.setText(_socketPort);
                   //     newSocket();
         public void initComponents() throws Exception {
              // IMPORTANT: Source code between BEGIN/END comment pair will be regenerated
              // every time the form is saved. All manual changes will be overwritten.
              // BEGIN GENERATED CODE
              // the following code sets the frame's initial state
              baud.setVisible(true);
              baud.setLocation(new java.awt.Point(222, 31));
              baud.setSize(new java.awt.Dimension(70, 21));
              interpretation.setVisible(true);
              interpretation.setLabel("Use decimals instead of letters.");
              interpretation.setLocation(new java.awt.Point(43, 175));
              interpretation.setSize(new java.awt.Dimension(200, 20));
              ports.setVisible(true);
              ports.setLocation(new java.awt.Point(222, 10));
              ports.setSize(new java.awt.Dimension(108, 21));
              socketPort.setLocation(new java.awt.Point(18, 10));
              socketPort.setVisible(true);
              socketPort.setText("9001");
              socketPort.setSize(new java.awt.Dimension(40, 20));
              softwarePort.setVisible(true);
              softwarePort.setLocation(new java.awt.Point(60, 10));
              softwarePort.setText("<Socket > ------------- <Serial>");
              softwarePort.setSize(new java.awt.Dimension(160, 20));
              sendToSocket.setVisible(true);
              sendToSocket.setLabel("<TestSend");
              sendToSocket.setLocation(new java.awt.Point(1, 60));
              sendToSocket.setSize(new java.awt.Dimension(80, 20));
              sendToSerial.setVisible(true);
              sendToSerial.setLabel("Test Send>");
              sendToSerial.setLocation(new java.awt.Point(279, 60));
              sendToSerial.setSize(new java.awt.Dimension(80, 20));
              sendText.setLocation(new java.awt.Point(82, 60));
              sendText.setVisible(true);
              sendText.setSize(new java.awt.Dimension(197, 20));
              traffic.setLocation(new java.awt.Point(5, 95));
              traffic.setVisible(true);
              traffic.setSize(new java.awt.Dimension(352, 80));
              debugButton.setFont(new java.awt.Font("Serif", 0, 12));
              debugButton.setVisible(true);
              debugButton.setLabel("debug");
              debugButton.setLocation(new java.awt.Point(130, 39));
              debugButton.setSize(new java.awt.Dimension(50, 20));
              socketStatus.setVisible(true);
              socketStatus.setAlignment(java.awt.Label.CENTER);
              socketStatus.setLocation(new java.awt.Point(65, 26));
              socketStatus.setSize(new java.awt.Dimension(45, 20));
              serialStatus.setVisible(true);
              serialStatus.setAlignment(java.awt.Label.CENTER);
              serialStatus.setLocation(new java.awt.Point(177, 26));
              serialStatus.setSize(new java.awt.Dimension(40, 20));
              numbersFootnote.setVisible(false);
              numbersFootnote.setLocation(new java.awt.Point(49, 79));
              numbersFootnote.setText("*Put spaces between numbers for test send.");
              numbersFootnote.setSize(new java.awt.Dimension(280, 20));
              clearButton.setVisible(true);
              clearButton.setLabel("Clear");
              clearButton.setLocation(new java.awt.Point(260, 178));
              clearButton.setSize(new java.awt.Dimension(58, 19));
              setLocation(new java.awt.Point(0, 0));
              setLayout(null);
              add(cover);
              add(baud);
              add(interpretation);
              add(ports);
              add(socketPort);
              add(softwarePort);
              add(sendToSocket);
              add(sendToSerial);
              add(sendText);
              add(traffic);
              add(debugButton);
              add(socketStatus);
              add(serialStatus);
              add(numbersFootnote);
              add(clearButton);
              setSize(new java.awt.Dimension(356, 274));
              // END GENERATED CODE
              baud.add("110");
              baud.add("300");
              baud.add("1200");
              baud.add("2400");
              baud.add("4800");
              baud.add("9600");
              baud.add("19200");
              baud.add("31250"); //   //31250
              baud.add("38400");
              baud.add("57600");
              baud.add("230400");
              baud.add("460800");
              baud.add("921600");
              baud.select("9600");
              cover.setVisible(true);
              //cover.setLayout(null);
              cover.setLocation(new java.awt.Point(-10, 57));
              cover.setSize(new java.awt.Dimension(964, 287));
         public void relayToSerial(int what) {
              boolean ok = mySerial.send(what).equals("OKAY");
              if (debug) {
                   socketStatus.setText("<IN>");
                   if (ok) {
                        serialStatus.setText("<OUT>");
                   showText(what);
         public void gotFromSerial(byte[] _byteArray){
         //public void relayToSocket(int what) {
             for(int i = 0; i < _byteArray.length; i++){
                 int what = (int) (_byteArray[i] & 0xff);
              if ((substituteChar != 0) && (what == substituteChar))
                   what = 0;
              boolean ok = mySocket.send(what).equals("OKAY");
              if (debug) {
                   serialStatus.setText("<IN>");
                   if (ok) {
                        socketStatus.setText("<OUT>");
                   showText(what);
         public void appendDebug(String what) {
              if (traffic.getText().toCharArray().length > 200)
                   traffic.setText(traffic.getText().substring(50));
              traffic.setText(traffic.getText() + "\n" + what + "\n");
              String strMessage = traffic.getText();
              traffic.setCaretPosition(strMessage.length());
         public void showText(int what) {
              if (interpretation.getState()) {
                   traffic.setText(traffic.getText() + " " + what);
              } else {
                   traffic.setText(traffic.getText() + ((char) what));
              String strMessage = traffic.getText();
              traffic.setCaretPosition(strMessage.length());
         // Standard method to start the applet
         //public void start() {
         //     newSocket();
         //     newSerial();
         public void populatePorts() {
              ports.removeAll();
              ArrayList portList = mySerial.getPortsList();
              for (int i = 0; i < portList.size(); i++) {
                   String[] portAndOwner = (String[]) portList.get(i);
                   System.out.println("owner" + portAndOwner[1]);
                   String port = portAndOwner[0];
                   String owner = portAndOwner[1];
                   //System.out.println(owner + " " + port + " number of Ports " + portList.size());
                   if (owner == null || owner.equals("Port currently not owned")) {
                        boolean alreadyThere = false;
                        for (int j = 0; j < ports.getItemCount(); j++) {
                             String alrdy = ports.getItem(j);
                             //System.out.println(port+ " Ports Already " + alrdy );
                             if (alrdy.equals(port)) {
                                  alreadyThere = true;
                                  break;
                        if (alreadyThere == false) {
                             ports.add(port);
                   } else {
                        System.out.println("Can't use it!" + port + owner);
                   //ports.add(((String) portList.get(i)));
         public void newSerial() {
              serialStatus.setText("!");
              mySerial = new Serial(this, serialType);
              if (ports.getItemCount() == 0) {
                   populatePorts();
         public void connectSerial() {
              if (ports.getItemCount() == 0) {
                   appendDebug("No serial ports found.");
              } else {
                   String status = mySerial.connect(ports.getSelectedItem(), Integer.parseInt(baud.getSelectedItem()));
                   if (status.startsWith("Got")) {
                        serialStatus.setText("<OK>");
                        //mySerial.start();
                   } else {
                        serialStatus.setText("Bad");
                        //populatePorts();
                   appendDebug(status);
         public void newSocket() {
              mySocket = new Socketer(Integer.parseInt(socketPort.getText()), this);
         public void socketStatus(String status) {
              if (status.startsWith("Got")) {
                   socketStatus.setText("<OK>");
              } else if (status.startsWith("Waiting")) {
                   socketStatus.setText("Waiting");
              } else {
                   socketStatus.setText("Bad");
              appendDebug(status);
         // Standard method to stop the applet
         public void stop() {
              mySerial.kill();
              mySocket.kill();
         // Standard method to destroy the applet
         public void destroy() {}
         // Main entry point when running standalone
         public static void main(String[] args) {
              SerialServer applet = new SerialServer();
              applet.isStandalone = true;
              Frame frame = new Frame();
              frame.addWindowListener(new java.awt.event.WindowAdapter() {
                   public void windowClosing(java.awt.event.WindowEvent e) {
                        Frame f = (Frame) e.getSource();
                        f.setVisible(false);
                        f.dispose();
                        System.exit(0);
              frame.setTitle("Serial Server");
              frame.add(applet, BorderLayout.CENTER);
              frame.setSize(400, 320);
              Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
              frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
              frame.setVisible(true);
              applet.init();
              argsHash = new HashMap();
              for (int i = 0; i < args.length; i++) {
                   String pair = args;
                   String parts[] = pair.split(":");
                   if (parts.length == 2) {
                        argsHash.put(parts[0], parts[1]);
                   }else{
                        System.out.println("Problem with format of parameter "+ pair);
              applet.start();
              frame.pack();
         public void sendTextTextValueChanged(java.awt.event.TextEvent e) {}
         public void interpretationItemStateChanged(java.awt.event.ItemEvent e) {
              numbersFootnote.setVisible(interpretation.getState());
              System.out.println("Interpretation" + interpretation.isVisible());
         public void socketPortTextValueChanged(java.awt.event.TextEvent e) {
              mySocket.kill();
              mySocket = null;
              newSocket();
              System.out.println("Changed Socket");
         public void sendToSocketPerformed(java.awt.event.ActionEvent e) {
              if (interpretation.getState()) {
                   StringTokenizer st = new StringTokenizer(sendText.getText(), " ");
                   while (st.hasMoreTokens()) {
                        try {
                             int what = Integer.parseInt(st.nextToken());
                             if ((substituteChar != 0) && (what == substituteChar))
                                  what = 0;
                             //System.out.println(substituteChar + "debugSend" + what);
                             mySocket.send(what);
                        } catch (NumberFormatException nfe) {
                             System.out.println("You are are entering letters but you checked the decimal interpretation.");
              } else {
                   byte[] asbytes = sendText.getText().getBytes();
                   for (int i = 0; i < asbytes.length; i++) {
                        int what = asbytes[i];
                        if ((substituteChar != 0) && (what == substituteChar))
                             what = 0;
                        System.out.println(substituteChar + "debugSend" + what);
                        mySocket.send(what);
         public void sendToSerialActionPerformed(java.awt.event.ActionEvent e) {
              if (interpretation.getState()) {
                   StringTokenizer st = new StringTokenizer(sendText.getText(), " ");
                   while (st.hasMoreTokens()) {
                        try {
    mySerial.send(Integer.parseInt(st.nextToken()));
    } catch (NumberFormatException e1) {
         appendDebug("Enter a number.");
              } else {
                   byte[] asbytes = sendText.getText().getBytes();
                   for (int i = 0; i < asbytes.length; i++) {
                        mySerial.send(asbytes[i]);
         public void portsItemStateChanged(java.awt.event.ItemEvent e) {
              mySerial.kill();
              mySerial = null;
              newSerial();
              connectSerial();
              System.out.println("Serial Port Changed");
         public void baudItemStateChanged(java.awt.event.ItemEvent e) {
              mySerial.kill();
              mySerial = null;
              newSerial();
              connectSerial();
              System.out.println("Serial Baud Changed");
         public void debugItemStateChanged(java.awt.event.ItemEvent e) {
              debug = debugButton.getState();
              if (debug) {
                   cover.setLocation(-1000, -1000);
              } else {
                   cover.setLocation(-10, 57);
                   //cover.setVisible(true);
              //System.out.println("debugl Changed" + debug);
         public void clearActionPerformed(java.awt.event.ActionEvent e) {
              traffic.setText("");

    Whoops, I'll fix it up so it's more presentable
    for (int row = 0; row <= 8 ; row++){
        for (int column = 1; column <= row; column++ )
         System.out.print(" ");
        for (int i = 0; i < row; i++){
         System.out.print(" " + (int) Math.pow(2,i));
        for (int j = row-1; j >= 0; j--)
         System.out.print(" " + (int)Math.pow(2, j));
        System.out.println();
    }About the space, I was checking to see what adding and removing them would do, although even after fixing it I seem to get a bunched up mass.
    -edit-
    Another thing I've noted is that pasting the text here seems to throw off the indentations which look normal in eclipse.
    Edited by: Yummyfishman on Oct 5, 2007 8:33 PM
    Edited by: Yummyfishman on Oct 5, 2007 8:35 PM
    Edited by: Yummyfishman on Oct 5, 2007 8:36 PM
    Edited by: Yummyfishman on Oct 5, 2007 8:40 PM

  • How do I set "JComboBox ItemStateChanged function" to do action only once

    I encounter a serious problem with JComboBox.
    When I use "ItemStateChanged" function,
    I found out it does every action TWICE !!!!!
    I need to use "ItemStateChanged" to decrease numbers by 1,
    since it does action twice, the numbers are decrease by 2 everytime, which is not what I intended.
    Are there anyway I could set "ItemStateChanged" to do the action only once?
    Please help me out

    The ItemEvent has a getStateChange() method that tells you what the state change was, "selected" vs "deselected". What you are seeing is that each item changes state twice, from delected to selected and vice versa. Check the result of this method to filter out the unwanted state changes.
    BTW don't use Choice, its AWT not swing.

  • JTabbedPanne and key-arrow

    Hi!!!
    I'm using a JTabbedPane and add a panel with TextField and Choice components... when i'm into a textfield or a choice and press a key-arrow i get moved from one tab to other one. How can i prevent it to happen?
    Thanks

    If focus is on the tab, then yes using the arrow keys will cause focus to switch to a new tab.
    However, is focus is on a text field I have never seen the behaviour you describe.
    Maybe it something to do with the fact that you are using AWT components. TextField and Choice are AWT components and should not be used in a Swing application. Use JTextField and JComboBox.

  • How to improve performance of java.awt.choice

    Hi!
    In my GUI I'm using a java.awt.choice. The problem is, when my app starts this choice is filled with lots of values with add(...).
    This takes quite a long time. I've already set the choice invisible before the filling, so a little bit performance is gained there.
    Is there any other way to tune this choice?
    Thank you!

    Try to fill the choice before adding it to the container

  • How to set the number of rows that an awt.Choice can display

    Dear Sir,
    I want to ask how an awt.Choice  can set the number of rows that it can display, like the method setMaximumRowCount in JCombobox. Since I want to set more row can be displayed, but choice no any method can set. And I have tried to add Jcombobox into awt.frame, then, the handling event function cannot receive event for the right-top cornet button(minimize, maximum, close).
    Best Regards,

    please post a Short, Self Contained, Correct Example showing your problem.
    bye
    TPD

  • Trying to add a java.awt.Choice object to a JTable

    Hi
    I am creating an app. which uses a JTable as the front end, displaying string data in all cells except the first column, which I need to make java.awt.Choice objects (or equivalent) - users will click the cell, a list of job numbers will be displayed and they click the one they want.
    Every time I run the app instead of a drop down list of job numbers I get the text:
    java.awt.Choice[choice0,0,0,0x0,invalid,current=K5000]
    I am unsure how to proceed.
    Any and all help appreciated - for reference my code at present is:
    Choice jobNumber = new Choice();
    jobNumber.addItem("K5000");
    Object[][] dataValues = {
    {jobNumbers[0], "A job title", new Integer(0), new Integer(0), new Integer(8), new Integer(8), new Integer(4), new Integer(0), new Integer(0)},
    where the JTable constructor is passed this array
    regards
    David

    Don't mix Swing and AWT!! Have a look at the JTable tutorial on this site.
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#combobox

  • Logical font special characters in AWT Choice

    Font FONT=new Font("Dialog",Font.PLAIN,12);
    String TEXT="re\u0123istr\u0113tiem";
    TextArea textarea=new TextArea();
    textarea.setFont(FONT);
    textarea.setText(TEXT);
    this.add(textarea);
    Choice choice=new Choice();
    choice.setFont(FONT);
    choice.addItem(TEXT);
    this.add(choice);Result is that text in textarea is correct but dropdown menu constains odd squares. ( http://img215.imageshack.us/img215/668/dialogip6.png )
    Why is this, even the font is exactly same and font contains all needed characters? Same problem occures in multiple systems I've tested. Same squares also occures in MenuItem components. But in most cases all characters are displayed correctly, like in textarea above or when using graphics.drawString(string) directly.

    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);
    }

  • Help Needed with showMessageDialog in awt

    Yes! I'm back with another problem. I'm trying to use showMessageDialog for my about popup. But, all the examples I have looked at use with Swing. I did see in the tutorials that this could work in java.awt.dialog. But, that doesn't seem to be in the docs, though. Anyway, below is my attempt at inserting showMessageDialog in my code. I'm receiving 2 compile errors on the line of code. Can anyone tell me where I have gone wrong?
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    class ChatClient extends Frame
       private TextField tf1 = new TextField("", 50);
        private TextArea ta;
        private Button sendButton;
        private Button quitButton;
        private Button okButton;
        private MenuItem exitMT, aboutMT;
        private Choice choice;
        private String user;
       public ChatClient()
           super("ChatClient");
          addWindowListener(new WindowAdapter()
          {public void windowClosing(WindowEvent e) {System.exit(0); }});
            addWindowListener(new WindowAdapter()
          {public void windowActivated(WindowEvent e)
                    tf1.requestFocusInWindow();
         setTitle("Chat Room");
          Panel p = new Panel(new BorderLayout());
          Panel p1 = new Panel(new BorderLayout());
          Panel p2 = new Panel(new GridLayout(3,1));
          MenuBar mb = new MenuBar ();
          setMenuBar (mb);
          Menu fileMenu = new Menu ("File");
         mb.add(fileMenu);
        Menu helpMenu = new Menu ("Help");
          mb.add(helpMenu);
          fileMenu.add (exitMT= new MenuItem ("Quit"));
         helpMenu.add (aboutMT = new MenuItem ("About"));
         exitMT.addActionListener(new ExitCommand());
          aboutMT.addActionListener(new AboutCommand());
       //creating main panel
       add(p, BorderLayout.NORTH);
          ta = new TextArea(10, 50);
          add(ta, BorderLayout.CENTER);
          ta.setSize(500,300);
          ta.setEditable(false);
        //adding buttons  and choice box to the right side of the window
          sendButton = new Button("Send");
          Panel p3 = new Panel();
          sendButton.setSize(15,15);
          sendButton.setBackground(Color.green);
          p3.add(sendButton);
          p2.add(p3);
         sendButton.addActionListener(new CopyCommand());
        quitButton = new Button("Quit");
         Panel p4 = new Panel();
        quitButton.setSize(15,15);
         quitButton.setBackground(Color.red);
         p4.add(quitButton);
         p2.add(p4);
        quitButton.addActionListener(new ExitCommand());
         Panel p5 = new Panel();
        choice = new Choice();
        choice.addItem("John");
        choice.addItem("Jena");
        choice.addItem("Colleen");
        choice.addItem("Steve");
        user=choice.getItem(0);
        p5.add(choice);
        choice.addItemListener(new CopyCommand());
        p2.add(p5);
         add(p2, BorderLayout.EAST);
       //adding the textfield to the south end of the window
          p1.add(tf1);
          add(p1, BorderLayout.SOUTH);
          tf1.addActionListener(new CopyCommand());
    //handling ActionEvent from buttons and menu items
      class ExitCommand implements ActionListener
        public void actionPerformed (ActionEvent e)
                   tf1.setText("");
                  ta.setText("");
                  choice.select(0);
                  System.exit(0);
      class AboutCommand implements ActionListener
         public void actionPerformed (ActionEvent e)
             OptionPane.showMessageDialog(OptionDialogFrame.this, "ChatClient ver. 1.0 written by Jennifer McAuliffe", "About Screen");
             System.out.print("About menu was pressed!");
      class CopyCommand implements ActionListener, ItemListener
        public void itemStateChanged(ItemEvent e)
        {  if (e.getSource() instanceof Choice)
            user = (String)e.getItem();
        public void actionPerformed (ActionEvent e)
            ta.append(user + ": " + tf1.getText() + "\n");
              tf1.setText("");
                tf1.requestFocus();
         public void keyPressed (KeyEvent ke)
             //figure out if the enter key was pressed
             if (ke.getKeyCode() == KeyEvent.VK_ENTER)
         ta.append(user + ": " + tf1.getText() + "\n");
         tf1.setText("");
         tf1.requestFocus();
         public void keyTyped (KeyEvent ke) { }
         public void keyReleased (KeyEvent ke) { }
       public static void main(String[] args)
       {  Frame f = new ChatClient();
          f.setSize(600, 400);
          f.show();
    }

    they aren't really stupid questions, you need to rely on the api to know exactly what classes you are using and the methods available. here is a really crude example that might give you some ideas. it could use lots of improvements.
    import java.awt.*;
    import java.awt.event.*;
    class DialogExample extends Frame
         public static void main(String[] args)
              Frame f = new Frame();
              //f.setVisible(true);
              Dialog d = new Dialog(f,"About Screen", true);
              d.add(new Label("ChatClient ver. 1.0 written by Jennifer McAuliffe"));
              d.addWindowListener(new WindowAdapter(){
                   public void windowClosing(WindowEvent we) {
                           ((Dialog)(we.getSource())).setVisible(false);
              d.pack();
              d.setLocationRelativeTo(null);
              d.setVisible(true);
              d.dispose();
              System.exit(0);
    }

  • Java AWT/Swing coexisting with native Qt application

    Hi,
    I'm trying to figure out how to get a GUI application written in Java to work in a native Qt application by embedding the Java application in the Qt application.
    Any suggestions on how to do this...

    It requires creating a system window (possibly frameless) using a native API calls (e.g. WinAPI or Xlib). That window might be embedded in your application. You would have to send system messages to that window (WM_XXXXX on Windows and XEvent's on X).
    For more information I suggest you to look at KDE sources where it is implemented for X Window (there is also a Java package, kjas which is a server for running applets in Konqueror.
    Or if you have much time you could write AWT on top of Qt (there is one built with Gtk+) and give the JVM your classes instead of the original JDK.
    The choice is yours. Happy Qt-ing!

  • Help needed for  ItemListeners for Choice Boxes

    Hi,
    I am new to AWT prgramming .
    I have created a menu called " Menu System Test window " and it contains toplevel menu items of "File" and "Help" and File Menu contains subitems "Product Evaluation" and "Exit".
    When i click File->Product Evaluation it displays a new Frame named "ProductEvaluationMeasurementTool" with some check boxes ,radiobuttons, buttons , text fields and choice lists.
    I have added action listeners for Buttons ie Button1 and Button2 and they are working fine.
    My question is how can i add ItemListener for Choice list so that when i select Choice1 in my program it should display a new Frame Window. i am sending the code .
    Thanks in advance
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Component;
    import java.awt.Checkbox;
    import javax.swing.*;
    // Make a main window with two top-level menus: File and Help.
    // Help has a submenu and demonstrates a few interesting menu items.
    public class MainWindow extends Frame {
      public MainWindow() {
        super("Menu System Test Window");
        setSize(500, 500);
        // make a top level File menu
        FileMenu fileMenu = new FileMenu(this);
        // make a top level Help menu
    //    HelpMenu helpMenu = new HelpMenu(this);
        // make a menu bar for this frame 
        // and add top level menus File and Menu
        MenuBar mb = new MenuBar();
        mb.add(fileMenu);
    //    mb.add(helpMenu);
        setMenuBar(mb);
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            exit();
      public void exit() {
        setVisible(false); // hide the Frame
        dispose(); // tell windowing system to free resources
        System.exit(0); // exit
      public static void main(String args[]) {
        MainWindow w = new MainWindow();
        w.setVisible(true);
    // Encapsulate the look and behavior of the File menu
    class FileMenu extends Menu implements ActionListener {
      MainWindow mw;  // who owns us?
      public FileMenu(MainWindow m) {
        super("File");
        mw = m;
        MenuItem mi;
        add(mi = new MenuItem("ProductEvaluation"));
        mi.addActionListener(this);
        add(mi = new MenuItem("Exit"));
        mi.addActionListener(this);
      // respond to the Exit menu choice
      public void actionPerformed(ActionEvent e) { String item = e.getActionCommand();
        if (item.equals("ProductEvaluation"))
         Frame f = new Frame("ProductMeasurementEvaluationTool");
         f.setSize(1290,1290);
         f.setLayout(null);
         TextField t1 = new TextField("textField1");
         t1.setBounds(230, 530, 50, 24);
         f.add(t1);
         TextField t2 = new TextField("textField2");
         t2.setBounds(430, 530, 50, 24);
         f.add(t2);
         ActionListener al = new MyActionListener(f, t1);
         ActionListener a2 = new MyActionListener(f, t2);
         Choice c1 = new Choice();
         c1.addItem("Choice1");
         c1.addItem("Choice2");
         c1.addItem("Choice3");
         f.add(c1);
         c1.setBounds(230, 650, 78, 20);
         Label l1 = new Label("Select the appropriate metrics for Measurement Process Evaluation");
         l1.setBounds(380, 50, 380, 20);
         f.add(l1);
         Label l2 = new Label("Architecture Metrics");
         l2.setBounds(170, 100, 110, 20);
         f.add(l2);
         Label l3 = new Label("RunTime Metrics");
         l3.setBounds(500, 100, 110, 20);
         f.add(l3);
         Label l4 = new Label("Documentation Metrics");
         l4.setBounds(840, 100, 130, 20);
         f.add(l4);
         JRadioButton rb1 = new JRadioButton("Componenent Metrics",false);
         rb1.setBounds(190, 140, 133, 20);
         f.add(rb1);
         JRadioButton rb2 = new JRadioButton("Task Metrics",false);
         rb2.setBounds(540, 140, 95, 20);
         f.add(rb2);
         JRadioButton rb3 = new JRadioButton("Manual Metrics",false);
         rb3.setBounds(870, 140, 108, 20);
         f.add(rb3);
         JRadioButton rb4 = new JRadioButton("Configuration Metrics",false);
         rb4.setBounds(190, 270, 142, 20);
         f.add(rb4);
         JRadioButton rb6 = new JRadioButton("DataHandling Metrics",false);
         rb6.setBounds(540, 270, 142, 20);
         f.add(rb6);
         JRadioButton rb8 = new JRadioButton("Development Metrics",false);
         rb8.setBounds(870, 270, 141, 20);
         f.add(rb8);
         Checkbox  c10 = new Checkbox("Size");
         c10.setBounds(220, 170, 49, 20);
         f.add(c10);
         Checkbox c11 = new Checkbox("Structure");
         c11.setBounds(220, 190, 75, 20);
         f.add(c11);
         Checkbox c12 = new Checkbox("Complexity");
         c12.setBounds(220, 210, 86, 20);
         f.add(c12);
         Checkbox c13 = new Checkbox("Size");
         c13.setBounds(220, 300, 49, 20);
         f.add(c13);
         Checkbox c14 = new Checkbox("Structure");
         c14.setBounds(220, 320, 75, 20);
         f.add(c14);
         Checkbox c15 = new Checkbox("Complexity");
         c15.setBounds(220, 340, 86, 20);
         f.add(c15);
         Checkbox c19 = new Checkbox("Size");
         c19.setBounds(580, 170, 49, 20);
         f.add(c19);
         Checkbox c20 = new Checkbox("Structure");
         c20.setBounds(580, 190, 75, 20);
         f.add(c20);
         Checkbox c21 = new Checkbox("Complexity");
         c21.setBounds(580, 210, 86, 20);
         f.add(c21);
         Checkbox c22 = new Checkbox("Size");
         c22.setBounds(580, 300, 49, 20);
         f.add(c22);
         Checkbox c23 = new Checkbox("Structure");
         c23.setBounds(580, 320, 75, 20);
         f.add(c23);
         Checkbox c24 = new Checkbox("Complexity");
         c24.setBounds(580, 340, 86, 20);
         f.add(c24);
         Checkbox c28 = new Checkbox("Size");
         c28.setBounds(920, 170, 49, 20);
         f.add(c28);
         Checkbox c29 = new Checkbox("Structure");
         c29.setBounds(920, 190, 75, 20);
         f.add(c29);
         Checkbox c30 = new Checkbox("Complexity");
         c30.setBounds(920, 210, 86, 20);
         f.add(c30);
         Checkbox c31 = new Checkbox("Size");
         c31.setBounds(920, 300, 49, 20);
         f.add(c31);
         Checkbox c32 = new Checkbox("Structure");
         c32.setBounds(920, 320, 75, 20);
         f.add(c32);
         Checkbox c33 = new Checkbox("Complexity");
         c33.setBounds(920, 340, 86, 20);
         f.add(c33);
         Button b1  = new Button("Button1");
         b1.setBounds(230, 500, 120, 24);
         f.add(b1);
         b1.addActionListener(al);
         Button b2  = new Button("Button2");
         b2.setBounds(430, 500, 120, 24);
         f.add(b2);
         b2.addActionListener(a2);
         f.show();
        else
       { mw.exit();}
    class MyActionListener implements ActionListener
        Frame f;
        TextField textField1;
        TextField textField2;
        public MyActionListener(Frame f, TextField tf)
            this.f = f;
            textField1 = tf;
            textField2 = tf;
        public void actionPerformed(ActionEvent e)
           String s = e.getActionCommand();
        if (s.equals("Button1"))
            Component[] components = f.getComponents();
      int numOfCheckBoxes = 81;
      int numChecked = 0;
      for ( int i = 0; i < components.length; i++ )
       if ( components[i] instanceof Checkbox )
        //numOfCheckBoxes++;
        Checkbox checkBox = (Checkbox) components;
    if ( checkBox.getState() )
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    textField1.setText( Double.toString( ratio ) );
    else
    if (s.equals("Button2"))
    Component[] components = f.getComponents();
    int numOfCheckBoxes = 81;
    int numChecked = 0;
    for ( int i = 0; i < components.length; i++ )
    if ( components[i] instanceof Checkbox )
    //numOfCheckBoxes++;
    Checkbox checkBox = (Checkbox) components[i];
    if ( checkBox.getState() )
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    textField2.setText( Double.toString( ratio ) );
    // Encapsulate the look and behavior of the Help menu
    class HelpMenu extends Menu implements ActionListener {
    MainWindow mw; // who owns us?
    public HelpMenu(MainWindow m) {
    super("Help");
    mw = m;
    MenuItem mi;
    add(mi = new MenuItem("Description"));
    mi.addActionListener(this);
    // respond to a few menu items
    public void actionPerformed(ActionEvent e) {
    String item = e.getActionCommand();
    if (item.equals("Description"))
    System.out.println("You can get description at our website");

    Hi,
    I am new to AWT prgramming .
    I have created a menu called " Menu System Test window " and it contains toplevel menu items of "File" and "Help" and File Menu contains subitems "Product Evaluation" and "Exit".
    When i click File->Product Evaluation it displays a new Frame named "ProductEvaluationMeasurementTool" with some check boxes ,radiobuttons, buttons , text fields and choice lists.
    I have added action listeners for Buttons ie Button1 and Button2 and they are working fine.
    My question is how can i add ItemListener for Choice list so that when i select Choice1 in my program it should display a new Frame Window. i am sending the code .
    Thanks in advance
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Component;
    import java.awt.Checkbox;
    import javax.swing.*;
    // Make a main window with two top-level menus: File and Help.
    // Help has a submenu and demonstrates a few interesting menu items.
    public class MainWindow extends Frame {
      public MainWindow() {
        super("Menu System Test Window");
        setSize(500, 500);
        // make a top level File menu
        FileMenu fileMenu = new FileMenu(this);
        // make a top level Help menu
    //    HelpMenu helpMenu = new HelpMenu(this);
        // make a menu bar for this frame 
        // and add top level menus File and Menu
        MenuBar mb = new MenuBar();
        mb.add(fileMenu);
    //    mb.add(helpMenu);
        setMenuBar(mb);
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            exit();
      public void exit() {
        setVisible(false); // hide the Frame
        dispose(); // tell windowing system to free resources
        System.exit(0); // exit
      public static void main(String args[]) {
        MainWindow w = new MainWindow();
        w.setVisible(true);
    // Encapsulate the look and behavior of the File menu
    class FileMenu extends Menu implements ActionListener {
      MainWindow mw;  // who owns us?
      public FileMenu(MainWindow m) {
        super("File");
        mw = m;
        MenuItem mi;
        add(mi = new MenuItem("ProductEvaluation"));
        mi.addActionListener(this);
        add(mi = new MenuItem("Exit"));
        mi.addActionListener(this);
      // respond to the Exit menu choice
      public void actionPerformed(ActionEvent e) { String item = e.getActionCommand();
        if (item.equals("ProductEvaluation"))
         Frame f = new Frame("ProductMeasurementEvaluationTool");
         f.setSize(1290,1290);
         f.setLayout(null);
         TextField t1 = new TextField("textField1");
         t1.setBounds(230, 530, 50, 24);
         f.add(t1);
         TextField t2 = new TextField("textField2");
         t2.setBounds(430, 530, 50, 24);
         f.add(t2);
         ActionListener al = new MyActionListener(f, t1);
         ActionListener a2 = new MyActionListener(f, t2);
         Choice c1 = new Choice();
         c1.addItem("Choice1");
         c1.addItem("Choice2");
         c1.addItem("Choice3");
         f.add(c1);
         c1.setBounds(230, 650, 78, 20);
         Label l1 = new Label("Select the appropriate metrics for Measurement Process Evaluation");
         l1.setBounds(380, 50, 380, 20);
         f.add(l1);
         Label l2 = new Label("Architecture Metrics");
         l2.setBounds(170, 100, 110, 20);
         f.add(l2);
         Label l3 = new Label("RunTime Metrics");
         l3.setBounds(500, 100, 110, 20);
         f.add(l3);
         Label l4 = new Label("Documentation Metrics");
         l4.setBounds(840, 100, 130, 20);
         f.add(l4);
         JRadioButton rb1 = new JRadioButton("Componenent Metrics",false);
         rb1.setBounds(190, 140, 133, 20);
         f.add(rb1);
         JRadioButton rb2 = new JRadioButton("Task Metrics",false);
         rb2.setBounds(540, 140, 95, 20);
         f.add(rb2);
         JRadioButton rb3 = new JRadioButton("Manual Metrics",false);
         rb3.setBounds(870, 140, 108, 20);
         f.add(rb3);
         JRadioButton rb4 = new JRadioButton("Configuration Metrics",false);
         rb4.setBounds(190, 270, 142, 20);
         f.add(rb4);
         JRadioButton rb6 = new JRadioButton("DataHandling Metrics",false);
         rb6.setBounds(540, 270, 142, 20);
         f.add(rb6);
         JRadioButton rb8 = new JRadioButton("Development Metrics",false);
         rb8.setBounds(870, 270, 141, 20);
         f.add(rb8);
         Checkbox  c10 = new Checkbox("Size");
         c10.setBounds(220, 170, 49, 20);
         f.add(c10);
         Checkbox c11 = new Checkbox("Structure");
         c11.setBounds(220, 190, 75, 20);
         f.add(c11);
         Checkbox c12 = new Checkbox("Complexity");
         c12.setBounds(220, 210, 86, 20);
         f.add(c12);
         Checkbox c13 = new Checkbox("Size");
         c13.setBounds(220, 300, 49, 20);
         f.add(c13);
         Checkbox c14 = new Checkbox("Structure");
         c14.setBounds(220, 320, 75, 20);
         f.add(c14);
         Checkbox c15 = new Checkbox("Complexity");
         c15.setBounds(220, 340, 86, 20);
         f.add(c15);
         Checkbox c19 = new Checkbox("Size");
         c19.setBounds(580, 170, 49, 20);
         f.add(c19);
         Checkbox c20 = new Checkbox("Structure");
         c20.setBounds(580, 190, 75, 20);
         f.add(c20);
         Checkbox c21 = new Checkbox("Complexity");
         c21.setBounds(580, 210, 86, 20);
         f.add(c21);
         Checkbox c22 = new Checkbox("Size");
         c22.setBounds(580, 300, 49, 20);
         f.add(c22);
         Checkbox c23 = new Checkbox("Structure");
         c23.setBounds(580, 320, 75, 20);
         f.add(c23);
         Checkbox c24 = new Checkbox("Complexity");
         c24.setBounds(580, 340, 86, 20);
         f.add(c24);
         Checkbox c28 = new Checkbox("Size");
         c28.setBounds(920, 170, 49, 20);
         f.add(c28);
         Checkbox c29 = new Checkbox("Structure");
         c29.setBounds(920, 190, 75, 20);
         f.add(c29);
         Checkbox c30 = new Checkbox("Complexity");
         c30.setBounds(920, 210, 86, 20);
         f.add(c30);
         Checkbox c31 = new Checkbox("Size");
         c31.setBounds(920, 300, 49, 20);
         f.add(c31);
         Checkbox c32 = new Checkbox("Structure");
         c32.setBounds(920, 320, 75, 20);
         f.add(c32);
         Checkbox c33 = new Checkbox("Complexity");
         c33.setBounds(920, 340, 86, 20);
         f.add(c33);
         Button b1  = new Button("Button1");
         b1.setBounds(230, 500, 120, 24);
         f.add(b1);
         b1.addActionListener(al);
         Button b2  = new Button("Button2");
         b2.setBounds(430, 500, 120, 24);
         f.add(b2);
         b2.addActionListener(a2);
         f.show();
        else
       { mw.exit();}
    class MyActionListener implements ActionListener
        Frame f;
        TextField textField1;
        TextField textField2;
        public MyActionListener(Frame f, TextField tf)
            this.f = f;
            textField1 = tf;
            textField2 = tf;
        public void actionPerformed(ActionEvent e)
           String s = e.getActionCommand();
        if (s.equals("Button1"))
            Component[] components = f.getComponents();
      int numOfCheckBoxes = 81;
      int numChecked = 0;
      for ( int i = 0; i < components.length; i++ )
       if ( components[i] instanceof Checkbox )
        //numOfCheckBoxes++;
        Checkbox checkBox = (Checkbox) components;
    if ( checkBox.getState() )
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    textField1.setText( Double.toString( ratio ) );
    else
    if (s.equals("Button2"))
    Component[] components = f.getComponents();
    int numOfCheckBoxes = 81;
    int numChecked = 0;
    for ( int i = 0; i < components.length; i++ )
    if ( components[i] instanceof Checkbox )
    //numOfCheckBoxes++;
    Checkbox checkBox = (Checkbox) components[i];
    if ( checkBox.getState() )
    numChecked++;
    double ratio = (double) numChecked / (double) numOfCheckBoxes;
    textField2.setText( Double.toString( ratio ) );
    // Encapsulate the look and behavior of the Help menu
    class HelpMenu extends Menu implements ActionListener {
    MainWindow mw; // who owns us?
    public HelpMenu(MainWindow m) {
    super("Help");
    mw = m;
    MenuItem mi;
    add(mi = new MenuItem("Description"));
    mi.addActionListener(this);
    // respond to a few menu items
    public void actionPerformed(ActionEvent e) {
    String item = e.getActionCommand();
    if (item.equals("Description"))
    System.out.println("You can get description at our website");

Maybe you are looking for

  • Oracle GL error during journal import- ICM DOWN

    Hi all, I hit an error regarding the Concurrent Manager - Oracle GL during the journal import from GL_INTERFACE table. I've already populate the table with a set of dummy values. i've run the journal import to import the value from the table. i've cl

  • Brazil - Vendor Issued Boleto

    hello, I have a question regarding Vendor issued Boletos. The process we have is 1. we recieved NF/invoice from third party [ creates IR and uses payment method 'Vendor issued Boleto' ] 2. We recieve physical Boleto from vendor 3. IR is completed usi

  • System won't boot when eSata drive connected

    I have an eSata SSD drive connected for fast foto-editing. The system halts at the bios/Toshiba screen. Disconnecting and rebooting and recoonection the SSD drive after the bios screen has passed works. How can I make the bios to continue, with the e

  • WPA with local MAC address authentication

    Can Cisco Aironet AP support WPA encryptions with local MAC address authentications as the current setup i have do not have a radius server to authenticate my users. i tried configuring with local MAC address but no success.

  • Budgeting and Planning periods in Comensation Management

    Hi guys, Do budgeting periods and Planning periods have to be the same in compensation management? Based on the SAP documentation, I understand that planning period can be different from budgeting period? What if my client has budget period from 01/0