Solve this if you can!

I have posted this three times now, and I have still not got an answer to the problem. The problem is as follows:
I have a problem with the focus of a JTextField in a JApplet. Here is my code:
class MyPanel extends JPanel {
    ImageIcon imageIcon;
    Image image;
    public MyPanel(Image tiledBackgroundImage) {
        setOpaque(false);
        setLayout(new BorderLayout());
        imageIcon = new ImageIcon(tiledBackgroundImage);
        image = imageIcon.getImage();
    public void paintComponent(Graphics g) {
        for (int x = 0; x < getWidth(); x += image.getWidth(this)) {
            for (int y = 0; y < getHeight(); y += image.getHeight(this))
                g.drawImage(image, x, y, this);
        super.paintComponent(g);
}and in the init() method of my applet I have the following call
setContentPane(new MyPanel(getAppletContext().getImage(new URL(getDocumentBase(), getParameter("backgroundImage")))));However, before I added this code for rendering the background image everything was working well. But after I added it, the JTextFields started to reject focus; when I click on them with the mouse I see a fast blink and then focus is lost again. Why is this so?

I don't know why some people have make theirself exceptionally clear by writing seven question marks... well, if you can solve it with this code I will give you the dollars. Here is the whole relevant code:
import layout.TableLayout;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;
import java.net.URL;
import java.net.MalformedURLException;
class MyPanel extends JPanel {
    ImageIcon imageIcon;
    Image image;
    public MyPanel(Image tiledBackgroundImage) {
        setOpaque(false);
        setLayout(new BorderLayout());
        imageIcon = new ImageIcon(tiledBackgroundImage);
        image = imageIcon.getImage();
    public void paintComponent(Graphics g) {
         setOpaque(false);
        for (int x = 0; x < getWidth(); x += image.getWidth(this)) {
            for (int y = 0; y < getHeight(); y += image.getHeight(this))
                g.drawImage(image, x, y, this);
         setOpaque(true);
        super.paintComponent(g);
public class AppletGUI extends JApplet {
    private Communicator databaseCommunicator;
    private JRadioButton localhostRB, otherHostRB;
    private ButtonGroup hostBG;
    private JTextField hostTF, userNameTF, fieldTypeTF, fieldKeyTF, fieldDefaultTF, fieldExtraTF;
    private JCheckBox fieldNullAllowedChB, fieldUnsignedChB, fieldZerofillChB;
    private JPasswordField passwordPF;
    private JTextArea messagesTA;
    private JScrollPane messagesSP, databasesSP, tablesSP, tableDataSP, tableStructureSP;
    private JButton loginBtn, newDBBtn, deleteDBBtn, newTableBtn, deleteTableBtn,
        newColumnBtn, deleteColumnBtn, modifyColumnBtn, newTupleBtn, deleteTupleBtn, modifyTupleBtn,
        disconnectBtn;
    private SortedJList databasesL, tablesL, tableDataL, tableFieldNamesL;
    //showColumnUI elements
    JTextField columnNameTF, columnDefaultTF;
    JComboBox columnTypeCB, columnSizeCB, columnDecimalsCB, columnKeyCB, columnReferencesTableCB,
        columnReferencesColumnCB;
    JCheckBox columnNullAllowedChB, columnZerofillChB, columnUnsignedChB, columnAutoIncrChB;
    JButton addFieldBtn, cancelBtn;
    String tableName;
    //addTableUI elements
    private JTextField tableNameTF, fieldNameTF;
    private DefaultTableModel fieldsTM;
    private JTable columnsT;
    private JScrollPane columnsSP;
    private JButton addTableBtn;
    //addDatabaseUI elements
    private JTextField nameTF;
    private String host;
    public void init() {
        try {
            getRootPane().setContentPane(new MyPanel(getAppletContext().getImage(new URL(getDocumentBase(),
                    getParameter("backgroundImage")))));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        host = getParameter("host");
    public void paint(Graphics g) {
        super.paint(g);
        showLoginUI();
    private void showLoginUI() {
        getContentPane().removeAll();
        //--- Create and initialize labels ---//
        JLabel loginLbl = new JLabel("Login to the MySQL server");
        loginLbl.setFont(new Font("Arial", Font.ITALIC, 12));
        JLabel hostLbl = new JLabel("Host address:");
        hostLbl.setFont(new Font("Times New Roman", Font.PLAIN, 12));
        JLabel usernameLbl = new JLabel("Username:");
        usernameLbl.setFont(new Font("Times New Roman", Font.PLAIN, 12));
        JLabel passwordLbl = new JLabel("Password:");
        passwordLbl.setFont(new Font("Times New Roman", Font.PLAIN, 12));
        JLabel messagesLbl = new JLabel("Messages:");
        messagesLbl.setFont(new Font("Arial", Font.ITALIC, 12));
        //--- Create and initialize text fields and areas ---//
        hostTF = new JTextField(15);
        hostTF.setEditable(false);
        hostTF.setText(host);
        hostTF.setOpaque((true));
        userNameTF = new JTextField(10);
        passwordPF = new JPasswordField(10);
        messagesTA = new JTextArea(5, 20);
        messagesTA.setLineWrap(true);
        messagesTA.setWrapStyleWord(true);
        messagesTA.setEnabled(false);
        messagesTA.setBackground(hostTF.getBackground());
        messagesSP = new JScrollPane(messagesTA);
        //--- Create and initialize buttons ---//
        localhostRB = new JRadioButton("localhost");
        localhostRB.setOpaque(false);
        otherHostRB = new JRadioButton();
        otherHostRB.setSelected(true);
        otherHostRB.setOpaque(false);
        localhostRB.setEnabled(false);
        hostBG = new ButtonGroup();
        hostBG.add(localhostRB);
        hostBG.add(otherHostRB);
        loginBtn = new JButton("Login");
        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (userNameTF.getText().length() == 0 || passwordPF.getPassword().length == 0)
                    JOptionPane.showMessageDialog(AppletGUI.this, "You must specify both username and password.",
                            "Compulsory field!", JOptionPane.ERROR_MESSAGE);
                else if (otherHostRB.isSelected() && hostTF.getText().length() == 0)
                    JOptionPane.showMessageDialog(AppletGUI.this, "Specify the host name.",
                            "Compulsory field!", JOptionPane.ERROR_MESSAGE);
                else {
                    try {
                        if (localhostRB.isSelected())
                            databaseCommunicator = new Communicator("mysql", userNameTF.getText(),
                                    String.valueOf(passwordPF.getPassword()));
                        else
                            databaseCommunicator = new Communicator("mysql", userNameTF.getText(),
                                    String.valueOf(passwordPF.getPassword()), hostTF.getText());
                        showMainUI();
                    } catch (DatabaseException e1) {
                        e1.printStackTrace();
                        messagesTA.append(e1.getMessage() + "\n");
/*        JButton exitBtn = new JButton("Exit");
        exitBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
        final double margin = 10;
        final double pref = TableLayout.PREFERRED;
        final double textareaHeight = 100;
        final double size[][] = {
            { margin, pref, pref, pref, margin },   //Widths
            { margin, pref, pref, pref, pref, pref,
              pref, pref, pref, pref, pref,
              textareaHeight, margin }    //Heights
        getContentPane().setLayout(new TableLayout(size));
        //--- Layout column 1 ---//
//        getContentPane().add(welcomeLbl, "1, 1, 3, 1");
        getContentPane().add(Box.createVerticalStrut(10), "1, 2");
        getContentPane().add(loginLbl, "1, 3, 2, 3");
        getContentPane().add(Box.createVerticalStrut(10), "1, 4");
        getContentPane().add(hostLbl, "1, 5");
        getContentPane().add(localhostRB, "1, 6");
        JPanel panel = new JPanel();
        panel.add(otherHostRB);
        panel.add(hostTF);
        panel.setOpaque(true);
        getContentPane().add(panel, "2, 6");
        getContentPane().add(Box.createVerticalStrut(10), "1, 7");
        getContentPane().add(usernameLbl, "1, 8");
        getContentPane().add(passwordLbl, "1, 9");
        getContentPane().add(Box.createVerticalStrut(10), "1, 10");
        getContentPane().add(messagesLbl, "1, 11, l, t");
        //--- Layout column 2 ---//
        getContentPane().add(userNameTF, "2, 8");
        getContentPane().add(passwordPF, "2, 9");
        getContentPane().add(messagesSP, "2, 11");
        //--- Layout column 3 ---//
        getContentPane().add(loginBtn, "3, 8, 3, 9");
//        getContentPane().add(exitBtn, "3, 11, l, b");
//        fitSizeAndCenter(this);
//        setVisible(true);
        getContentPane().validate();
        getContentPane().repaint();
    }

Similar Messages

Maybe you are looking for