Heya all,

I'm working on a small 'file browser', where the list of files is printed in a console (for now), but where the user inserts the file's path in a GUI.

My gui is rather simple: one frame and one panel containing a JTextField and a JButton.
I want the JButton to be activated when the user presses enter in the JTextArea (like in any other file browser) to increase useability.

In order to do so, i made the button focusable and the default focus, and that works on windows, but not on linux.
I know the default look and feel for my distro (mint 16), metal, does not activate a button on pressing enter, so i changed that to Motif without any results.

//class responsible for the JPanel and it's content
private JLabel addressLabel;
    private JTextField addressTextField;
    private JButton goButton;
    private JXplorer data;
    //private JLabel iconLabel; // not yet implemented
    private JPanel viewPanel;

    public JXAddressView() {
        viewPanel = new JPanel();

        addressLabel = new JLabel("Address");
        addressTextField = new JTextField(10);
        goButton = new JButton("Go");

        goButton.addActionListener(this);
        goButton.setFocusable(true);
        goButton.requestFocusInWindow();

        viewPanel.add(addressLabel);
        viewPanel.add(addressTextField);
        viewPanel.add(goButton);



    }



//function that builds the JFrame and places the JPanel in it
private void buildGUI() {
        jxAddressView.setData(this);

        try {               
            if(UIManager.getLookAndFeel().getID().equals("Metal"))
            {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            }
        } //**snip** some catches

        JFrame frame = new JFrame("JXplorer, The java file exploration utility");

        frame.setContentPane(jxAddressView.getViewPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFrame.setDefaultLookAndFeelDecorated(true);

        frame.pack();
        frame.setVisible(true);
    }

Did you try
frame.getRootPane().setDefaultButton(goButton);
?

That works, but it's not a super neat solution, since the panels and their content are being assembled in one class, and the frame is defined in another one (a main() function).

I added a getter for the button so the main class can get it, although this means giving a GUI element to a non-GUI class.
Still, this'll do just fine ;) thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.