我无法点击此JTextField进行编辑,为什么不呢?

时间:2013-11-22 17:42:17

标签: java swing java-7 jtextfield declarative-services

更新

仅在OS X上运行时才会发生这种情况.Windows上不存在此问题。我正在使用Eclipse Kepler和Oracle 1.7.0_45.jdk


以下代码是从OSGI声明性服务组件运行的基本GUI。问题是当GUI启动时,我无法与JTextField loginField进行交互。我可以与JButton loginButton进行互动。如果我运行与Java应用程序相同的代码,我可以交互/编辑JTextField。所以这个问题必须归功于它作为OSGi包发布。任何人都可以对这个问题有所了解吗?

DoctorDevice.java

package ie.ucd.sh.doctor;

import org.osgi.service.component.ComponentContext;
import ie.ucd.sh.db.context.DbContext;
import java.awt.EventQueue;

/**
 * This class runs the GUI for the Doctor Device.  It connects to the database using the abstraction
 * provided by the ie.ucd.sh.db.context bundle and displays information on the doctor that has logged in
 * into the device.
 * @author Conor Griffin
 */
public class DoctorDevice {

    protected DbContext dbContext;
    DoctorGUI window;

    public void activate(ComponentContext ctxt) {

        /**
         * Launch the swing GUI and set its DbContext
         */
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    window = new DoctorGUI();
                    window.frame.setVisible(true);
                    window.dbContext = dbContext;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    protected void deactivate(ComponentContext ctxt) {
        window.terminate();
    }

    public DbContext getContext() {
        return dbContext;
    }

    public void setContext(DbContext dbContext) {
        this.dbContext = dbContext;
        System.out.println("DbContext was set to " + dbContext);
    }

}

DoctorGUI.java

package ie.ucd.sh.doctor;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import java.awt.GridLayout;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import ie.ucd.sh.db.context.DbContext;
import ie.ucd.sh.db.context.Doctor;
import ie.ucd.sh.db.context.Entity.Type;
import ie.ucd.sh.db.context.Nurse;
import ie.ucd.sh.db.context.Patient;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTabbedPane;

public class DoctorGUI {

    protected JFrame frame;
    protected DbContext dbContext;
    private static DateFormat df = new SimpleDateFormat("HH:mm:ss");
    private Doctor currentUser;
    private JTextField loginField;

    /**
     * Create the application.
     */
    public DoctorGUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    public void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridLayout(0, 2, 0, 0));

        JPanel inputPanel = new JPanel();
        frame.getContentPane().add(inputPanel);
        inputPanel.setLayout(null);

        JButton loginButton = new JButton("Login");
        loginButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                currentUser = login();
            }
        });
        loginButton.setBounds(137, 6, 82, 29);
        inputPanel.add(loginButton);

        loginField = new JTextField();
        loginField.setBounds(6, 5, 134, 28);
        inputPanel.add(loginField);
        loginField.setToolTipText("Enter your ID");
        loginField.setColumns(10);
        loginField.setEditable(true);

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        frame.getContentPane().add(tabbedPane);
        loginField.requestFocusInWindow();
    }


    /**
     * Login the doctor
     * @return
     */
    private Doctor login() {
        int id = 0;
        Doctor doctor = null;
        try {
            id = Integer.parseInt(loginField.getText());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            String title = "Invalid Value";
            String message = "ID must be numeric";
            JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
            return doctor;
        }

        doctor = (Doctor)dbContext.get(Type.Doctor, id);
        System.out.println(doctor.getFirstName() + " " + doctor.getLastName());

        return doctor;
    }

    /**
     * Set the JFrame to invisible and throw it away
     */
    public void terminate() {
        frame.setVisible(false);
        frame.dispose();
    }
}

DbContextClient.xml - OSGi组件定义

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0" activate="activate" deactivate="deactivate" name="DbContextClient">
   <implementation class="ie.ucd.sh.doctor.DoctorDevice"/>
   <reference bind="setContext" cardinality="1..1" interface="ie.ucd.sh.db.context.DbContext" name="DbContext" policy="static" unbind="getContext"/>
</scr:component>

2 个答案:

答案 0 :(得分:2)

经过几个小时的研究后,添加到运行配置参数的参数修复了我的问题:-noSplash

我的日食设置:enter image description here

答案 1 :(得分:0)

  

loginField.requestFocusInWindow();

在窗口可见后尝试请求焦点。 (在具有此字段的窗口上调用setVisible(true)后调用此方法。)