Results 1 to 7 of 7
  1. #1
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default [SOLVED] JButton selected

    Hi everyone:

    I have a program which creates a GUI. When I start the program, all the buttons in my GUI are disabled (I do that with setEnabled(false)). After I click some Option in the menu, I enable some buttons (again with setEnabled). My problem is that the first one of those buttons has some strange rectangle around the name, and I have to click it twice to make it work. I know it is only a small detail but I don't know what could be the reason. Any hints are appreciated.

    Here some screenshots:

    Before enable the buttons

    After:


    Edit: Ok, after I upload the screenshots I just realize that the second one does not show what I wanted. Messing around a little bit, I discovered a new thing about the problem. When I change the window (for instance to take the screenshot), that little rectangle around the name of the button disappears, and if I click one in the button it works.
    Last edited by sky; 12-05-2009 at 09:35 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    My problem is that the first one of those buttons has some strange rectangle around the name
    That indicates which button currently has focus. This moves from button to button as you tab.

    It seems like there is some kind of focus issue since you are creating a frame withouth any focusable components. Make sure you create and show the frame on the Event Dispatch Thread. All the examples in the Swing tutorial show the proper way to do this as demonstrated by their usage of the way they invoke the "createAndShowGUI" method.

    If you need more help post your SSCCE that demonstrate the problem.

  3. #3
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    I try to create the frame on the Event Dispatch Thread, as you suggested, but this did not solve the problem. Therefore, I created the SSCCE. While doing this, I also identify the problem, and I corrected it. Well, sort of... I will explain mysefl with the example:

    Java Code:
    public class Test
    {
        public static void main(String[] args) {
    
            GUI app = new GUI();
        }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.ArrayList;
    
    /**
     * ImageViewer is the main class of the image viewer application. It builds 
     * and displays the application GUI.
     * 
     * To start the application, create an object of this class.
     * 
     * @author Michael Kolling and David J Barnes 
     * @version 0.3
     */
    public class GUI
    {
        private JFrame frame;
        private JButton addCinemaButton;
        private JButton listButton;
        private JMenu mainMenu;
        private JMenu modeMenu;
    
        /**
         * Create an ImageViewer show it on screen.
         */
        public GUI() {
            
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    makeFrame();
                }
            });
        }
      
        private void mode(ActionEvent e) {
            
            String cmd=null;
            
            if (e != null) {
                cmd = e.getActionCommand();
                
                if (cmd.equals("admin")) {
                    addCinemaButton.setEnabled(true);
                    
                } else if (cmd.equals("custom")) {
                    addCinemaButton.setEnabled(false);
                }
                
                //mainMenu.setSelected(false); 
                //mainMenu.setPopupMenuVisible(false);
                //modeMenu.setPopupMenuVisible(false);
            }
            
        }
    
        private void addCinema() {
            System.out.println("Test");
        }
          
        
        /**
         * Quit function: quit the application.
         */
        private void quit()
        {
            System.exit(0);
        }
        
        // ---- swing stuff to build the frame and all its components ----
        
        /**
         * Create the Swing frame and its content.
         */
        private void makeFrame()
        {
            frame = new JFrame("CinemaSystem");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            makeMenuBar(frame);
            
            // We cast the frame in order to use the method setBorder()
            JPanel contentPane = (JPanel) frame.getContentPane();
            contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
            
            contentPane.setLayout(new BorderLayout(6, 6));
            
            
            JLabel firstLabel = new JLabel("Commands:");
            contentPane.add(firstLabel, BorderLayout.NORTH);
            
            JLabel statusLabel = new JLabel("Version 1.0");
            contentPane.add(statusLabel, BorderLayout.SOUTH);
            
            JPanel buttonbar = new JPanel();
            //buttonbar.setBorder(new EmptyBorder(6, 6, 6, 6));
            buttonbar.setLayout(new GridLayout(0,1));
            
            addCinemaButton = new JButton("Add cinema");
            buttonbar.add(addCinemaButton);
            
            addCinemaButton.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) { addCinema(); }
                               });
            
        
                               
            JPanel leftbar = new JPanel();
            leftbar.setLayout(new FlowLayout()); //Not neccessary cause it's the default layout
            leftbar.add(buttonbar);
            
            // Disable the buttons until a mode is selected
            addCinemaButton.setEnabled(false);
            
            contentPane.add(leftbar, BorderLayout.WEST);
    
            // building is done - arrange the components and show        
            frame.pack();
            frame.setVisible(true);
        }
        
        /**
         * Create the main frame's menu bar.
         * @param frame   The frame that the menu bar should be added to.
         */
        private void makeMenuBar(JFrame frame)
        {
            JMenuBar menubar = new JMenuBar();
            frame.setJMenuBar(menubar);
            
            // create the File menu
            mainMenu = new JMenu("Menu");
            menubar.add(mainMenu);
        
            
            modeMenu = new JMenu("Mode");
            mainMenu.add(modeMenu);
            
            ButtonGroup group = new ButtonGroup();
            
            JRadioButton adminItem = new JRadioButton("Admin");
            adminItem.setActionCommand("admin");
            adminItem.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) { mode(e); }
                               });
            group.add(adminItem);
            modeMenu.add(adminItem);
            
            JRadioButton customItem = new JRadioButton("Custom");
            customItem.setActionCommand("custom");
            customItem.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) { mode(e); }
                               });
            group.add(customItem);
            modeMenu.add(customItem);
            
        }
    }
    As you can see there are three commented lines. If you descomment thos lines, the problem appears. But if I don't, then I have another problem: when I select one of the choices of the radio button, the menu does not close by itself, as it happens in all the examples from Java, and I want to do that (but in those examples I didn't find the code that close the menus, it's like if they close by default or something)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Camickr appears to be right on the money (big surprise there, huh?) that it's likely a focus problem. It goes away for instance if you give the contentPane the initial focus:

    Java Code:
        // We cast the frame in order to use the method setBorder()
        JPanel contentPane = (JPanel) frame.getContentPane();
        //....
        
        // !! added:
        contentPane.setFocusable(true);
        contentPane.requestFocusInWindow();
    but this smells of a kludge, and I'm not sure if there is a canonical answer to this problem.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Camickr appears to be right on the money (big surprise there, huh?) that it's likely a focus problem.
    But not this time and worse yet I got you thinking along the wrong lines. Thats what I get for trying to answer without a SSCCE :(

    The problem is the code is using JRadioButtons. It should be using JRadioButtonMenuItem.

    Also, you may want to use:

    addCinemaButton.setEnabled(true);
    addCinemaButton.requestFocusInWindow();
    So the user knows which button has focus.

    Yet again a SSCCE proves it weight in gold :)
    Last edited by camickr; 12-05-2009 at 09:27 PM.

  6. #6
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Thank you both, it works perfect now. I don't know how I could miss the fact of using JRadioButton, since I follow the example from Java Tutorials.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. SELECTED select menu??
    By Samurai Coder in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 06:09 PM
  2. JTable Gridline in selected row
    By arubin in forum AWT / Swing
    Replies: 11
    Last Post: 11-24-2009, 07:05 PM
  3. JTable problem with getting ROW selected
    By nadia in forum Advanced Java
    Replies: 2
    Last Post: 01-13-2009, 05:57 PM
  4. Replies: 0
    Last Post: 07-11-2008, 04:29 PM
  5. Replies: 0
    Last Post: 07-07-2008, 04:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •