JFrame focus when used as a popup

  • Follow


I know this is probably poor implementation on my part but I've got a
GUI application that needs to popup a window with several controls on
it that allow some options to be set. I implemented this popup as a
JFrame and now, of course, when you touch the underlying GUI, the popup
looses focus and you can't get back to the popup (this is an embedded
app on a fixed screen).

Is there some way I can restrict focus to the popup JFrame (I know, it
should have been a dialog box) so that button presses on the underlying
GUI are ignored until the JFrame is disposed of?

Here's the code for the popup app, feel free to critique, any help
appreciated:

================================================================
package gui;

/*
 *  ModeSelect.java is a 1.4 application
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import turbine.DataConst;
import turbine.GMethods;


public class ModeSelect implements DataConst
{
    private  JFrame frame;
    private  JComboBox cb1;
    private  JComboBox cb2;
    private  int mode;
    private  String type;
    private  String [] platform_list;
    private  String [] mode_list;


    public  void addComponents(Container pane)
    {
        int BTN_WD = 100;
        int BTN_HT = 50;
        int CB_WD  = 135;
        int CB_HT  = 60;

        int B_POS = javax.swing.border.TitledBorder.DEFAULT_POSITION;
        int B_JUST =
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION;

        Font df_14 = new Font("Dialog",1,14);
        Font df_16 = new Font("Dialog",1,16);

        pane.setLayout(null);
        pane.setBackground (new Color (225,225,225));


        // Discription label

        JLabel l1 = new JLabel ( "Please Select Device and Mode" );
        Dimension size = l1.getPreferredSize();
        l1.setFont(df_16);
        l1.setBounds(65, 0, size.width + 75, size.height + 20);


        // Platform type (Exciter, Exporter, Split) combo box
        cb1 = new JComboBox (platform_list);
        cb1.setBorder (BorderFactory.createTitledBorder
                (null,"Device Type", B_JUST, B_POS, df_14, null));
        cb1.setBounds(40, 40, CB_WD, CB_HT);
cb1.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                cbTypeActionPerformed(e);
            }
        });


        // Operating mode (AM, FM, etc.) combo box
        cb2 = new JComboBox (mode_list);
        cb2.setBorder (BorderFactory.createTitledBorder
                (null,"Operation Mode", B_JUST, B_POS, df_14, null));
        cb2.setBounds(215, 40, CB_WD, CB_HT);

        cb2.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                cbModeActionPerformed(e);
            }
        });


        // OK button

        JButton b1 = new JButton ("OK");
        b1.setBounds(60, 115, BTN_WD, BTN_HT);
        b1.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                // Get currently selected values from platform and mode
boxes
                savePlatform();
                saveMode();

                // Save new platform and mode
                GMethods.setPlatform (type);
                GMethods.setMode (mode);

                // Run cmtx_set.sh to set new platform and mode
                GMethods.setModeSelect();

                frame.dispose();
            }
        });


        // Cancel button

        JButton b2 = new JButton ("CANCEL");
        b2.setBounds(230, 115, BTN_WD, BTN_HT);
        b2.addActionListener( new ActionListener()



        {
            public void actionPerformed(ActionEvent e)
            {
                frame.dispose();
            }
        });


        // Add components to viewing pane of frame

        pane.add(l1);
        pane.add(cb1);
        pane.add(cb2);
        pane.add(b1);
        pane.add(b2);
    }


    /**
     * Create the GUI and show it.  For thread safety, this method
     * should be invoked from the event-dispatching thread.
     */
    private void createAndShowGUI()
    {
        int FRM_WD = 390;
        int FRM_HT = 200;

        // Create and set up the window.
        frame = new JFrame("Authorized Mode Selection");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.requestFocus();

        // Center on the screen
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) Math.round((double)(d.getWidth()-FRM_WD)/2);
        int y = (int) Math.round((double)(d.getHeight()-FRM_HT)/2);
        frame.setLocation(x, y);
        frame.setSize(FRM_WD, FRM_HT);

        // Set up the content pane.
        addComponents(frame.getContentPane());

        // Size and display the window.
        frame.setResizable(false);
        frame.setVisible(true);
    }

    protected void cbTypeActionPerformed(ActionEvent e)
    {
        savePlatform();}
    }

    public void savePlatform ()
    {
        type = (String) cb1.getSelectedItem();
    }

    protected void cbModeActionPerformed(ActionEvent e)
    {
        saveMode();
    }

    public void saveMode()
    {
        String mode_str;
        char [] mode_tmp = new char [2];

        // Retrieve selected item sting and slice out the mode number
in
        // brackets (ie. [01] AM MA1) and save it as an integer in mode
        mode_str = (String) cb2.getSelectedItem();
        mode_str.getChars(1,3,mode_tmp,0);

        mode = Integer.parseInt(new String(mode_tmp),10);
    }

    public void show ( String [] pf, String [] md )
    {

        platform_list = pf;
        mode_list = md;

        // Schedule a job for the event-dispatching thread: Creating
        // and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

0
Reply jkimble (57) 11/22/2005 6:23:53 PM

"James Kimble" <jkimble@one.net> wrote in message 
news:1132683833.656068.24670@g44g2000cwa.googlegroups.com...
>
> I know this is probably poor implementation on my part but I've got a
> GUI application that needs to popup a window with several controls on
> it that allow some options to be set. I implemented this popup as a
> JFrame and now, of course, when you touch the underlying GUI, the popup
> looses focus and you can't get back to the popup (this is an embedded
> app on a fixed screen).
>
> Is there some way I can restrict focus to the popup JFrame (I know, it
> should have been a dialog box) so that button presses on the underlying
> GUI are ignored until the JFrame is disposed of?

    Since you're going to be making changes to the code anyway, what's wrong 
with the solution of changing it so that you use a JDialog instead of a 
JFrame?

    - Oliver 


0
Reply Oliver 11/22/2005 6:46:50 PM


Oliver Wong wrote:
> "James Kimble" <jkimble@one.net> wrote in message 
> news:1132683833.656068.24670@g44g2000cwa.googlegroups.com...
> 
>>I know this is probably poor implementation on my part but I've got a
>>GUI application that needs to popup a window with several controls on
>>it that allow some options to be set. I implemented this popup as a
>>JFrame and now, of course, when you touch the underlying GUI, the popup
>>looses focus and you can't get back to the popup (this is an embedded
>>app on a fixed screen).
>>
>>Is there some way I can restrict focus to the popup JFrame (I know, it
>>should have been a dialog box) so that button presses on the underlying
>>GUI are ignored until the JFrame is disposed of?
> 
> 
>     Since you're going to be making changes to the code anyway, what's wrong 
> with the solution of changing it so that you use a JDialog instead of a 
> JFrame?
> 


And make that JDialog modal
>     - Oliver 
> 
> 
0
Reply Vova 11/22/2005 7:09:22 PM

2 Replies
114 Views

(page loaded in 0.072 seconds)

Similiar Articles:









7/11/2012 1:09:28 AM


Reply: