Question

I'm learning Java using Art and Science of Java (using Java SE 6u45). Trying to Change the font of text by entering the new font type in a JTextField. But the problem is that I can't enter any text in JTextField. The problem is common to other swing components i've used like JButton, JCheckBox. But in the latter components I could see the effect of selection, even though the visual selection stays the same, meaning that the check box remains checked even after clicking but the code shows the result of an unchecked box.

But in case of JTextField, not even the effect is showing. Not also could i delete a test text i put in JTextField. Tried to use isEditable() , grabFocus() and isFocusable(). Could it be a Java bug ?

/**
* Example 10.9
 * 
 * This program prints the given text in the font inputted by the user in JTextField
 */
package ASJ_Examples;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JTextField;

import acm.graphics.GLabel;
import acm.program.GraphicsProgram;

public class FontSampler extends GraphicsProgram implements ActionListener{
/**
 * Eclispe Generated
 */
private static final long serialVersionUID = -5734136235409079420L;
private static final String TEST_STRING = "This is a test";
private static final double LEFT_MARGIN = 3;
private static final int MAX_FONT_NAME = 10;

public void init(){
    addJFontLabel();
    addJFontTextField();
    lastY = 0;
    addGLabel(); 
}
/**
 * Adds a text field to enter the required font
 */
private void addJFontTextField() {
    String test = "new";
    fontField = new JTextField(test, MAX_FONT_NAME);    //added to see if Jtextfiled is responding 
//      fontField.setEnabled(true);
//      fontField.setEditable(true);
    fontField.addActionListener(this);
    //added these to give focus to jtextfield but no effect
    fontField.isEditable();
    fontField.grabFocus();
    fontField.isFocusable();
    //add to window
    add(fontField, SOUTH);

}
/**
 * Adds JFontLAbel to denote the text input field
 */
private void addJFontLabel() {
    add(new JLabel("Font"), SOUTH);

}

/**
 * Adds the test label to canvas
 */
private void addGLabel() {
    lastLabel = new GLabel(TEST_STRING);
    add(lastLabel, 20, 20);

}

public void ActionPerformed(ActionEvent e){
    if(e.getSource() == fontField){
        GLabel label = new GLabel(TEST_STRING);
        label.setFont(lastLabel.getFont()); //to display the text even if the suer entered a non-allowed font
        label.setFont(fontField.getText()); //change the font to u4ser demanded font
        addGlabel(label);
        lastLabel = label;
    }
}
/**
 *adds a Glabel on the next line adjusting for heights
 * @param label
 */
private void addGlabel(GLabel label) {
    lastY += label.getHeight();
    lastY += lastLabel.getDescent() - label.getDescent();
    add(label, LEFT_MARGIN, lastY);

}
/**
 * JTextField to enter font
 */
private JTextField fontField;
/**
 * GLabel which is being worked on
 */
private GLabel lastLabel;
/**
 * 
 */
private double lastY;
}
Was it helpful?

Solution 2

As @andrew-thompson pointed out, the issue was mix of awt and swing. I suppose somehow the GComponent was overlaying Swing component JTextField which made it inaccessible. So the workaround is to create JPanel and a KeyListener to the JTextField as noted by @marco . This is the code that works : SSCCE Code

 /**
 * 
 * This program prints the given text in the font inputted by the user in JTextField
 */

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.JTextField;

import acm.graphics.GLabel;
import acm.program.GraphicsProgram;

public class FontSampler extends GraphicsProgram implements KeyListener{
private static final String TEST_STRING = "This is a test";
private static final double LEFT_MARGIN = 3;
private static final int MAX_FONT_NAME = 10;

public void init(){
    initPanel();    //init panel
    addJFontTextField();    //adds fontField to enter the font type
    addGLabel();    //adds default label to GCanvas
    lastY = 0;  //default y offset for post-processed label
    add(panel); //adds panel to canvas
}

/**
 * initialises panel 
 */
private void initPanel() {
    panel = new JPanel();
    panel.setLayout(new BorderLayout());

}

/**
 * Adds a text field to enter the required font
 */
private void addJFontTextField() {
    fontField = new JTextField( MAX_FONT_NAME);//added to see if Jtextfiled is responding
    panel.add(fontField, BorderLayout.SOUTH);
    fontField.addKeyListener(this); //adds key listener
}

/**
 * Adds the test label to canvas
 */
private void addGLabel() {
    lastLabel = new GLabel(TEST_STRING);
    add(lastLabel, 20, 20);

}

    /**
     * Called when any key is pressed
     */
public void keyPressed(KeyEvent e){
    if(e.getKeyCode() == KeyEvent.VK_ENTER){//check for enter key pressed
        GLabel label = new GLabel(TEST_STRING);
        label.setFont(lastLabel.getFont()); 
        label.setFont(fontField.getText());
        addGlabel(label);
        lastLabel = label;
    }
}

/**
 *adds a Glabel on the next line adjusting for heights
 * @param label
 */
private void addGlabel(GLabel label) {
    lastY += label.getHeight();
    lastY += lastLabel.getDescent() - label.getDescent();
    add(label, LEFT_MARGIN, lastY);
}

/**
 * JTextField to enter font
 */
private JTextField fontField;
/**
 * GLabel which is being worked on
 */
private GLabel lastLabel;
/**
 * 
 */
private double lastY;

private JPanel panel;

}

Thanks :)

OTHER TIPS

try using fontField.requestFocus(); instead of fontField.grabFocus(); and fontField.setEditable(true); instead of fontField.isEditable(); fontField.setFocusable(true); instead of fontField.isFocusable();

btw fontField.setEditable(true); and fontField.setFocusable(true); are not necessary by default they are set to true.

But in the latter components I could see the effect of selection, even though the visual selection stays the same, meaning that the check box remains checked even after clicking but the code shows the result of an unchecked box.

That sounds very strange. Maybe the GraphicsProgram class is doing something naughty. Cannot tell w/o its code though.

fontField.isEditable();
fontField.grabFocus();
fontField.isFocusable();

The first returns a boolean if the field is editable (it is by default). The second should not be used by client programs, use fontField.requestFocusInWindow() instead. The third returns a boolean if the field is focusable (it is by default).

fontField.addActionListener(this);

An ActionListener on a JTextField does nothing. Try doing this:

fontfield.getDocument().addDocumentListener(new DocumentListener() {

    @Override
    public void removeUpdate(DocumentEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        // TODO Auto-generated method stub  
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        // TODO Auto-generated method stub
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top