UIError after extending JTextComponent

33 views Asked by At

I got this error after I extended JTextComponent

UIDefaults.getUI() failed: no ComponentUI class for: com.mypackage.components.FlowComponent[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=,caretColor=,disabledTextColor=,editable=true,margin=,selectedTextColor=,selectionColor=]
java.lang.Error

I am know this has something to do with the UI however I have no idea how UI works and because of that I am using the default lookandFeel

The full text of the class that throws the exception every time it is called:

public class FlowComponent extends JTextComponent {

/////// Constructor ///////

FlowType type;
String data = null;
FontMetrics fm;
PlainDocument doc = new PlainDocument();

public FlowComponent(FlowType type) {
    this.type = type;
    setDocument(doc);
}


/////// Main Methods ////////

@Override
public Dimension getPreferredSize() {
    switch(type) {
    case START:
        return new Dimension(200, 60);
    case END:
        return new Dimension(200, 60);
    case PROCESS:
        return new Dimension(200, 60);
    case INPUT_OUTPUT:
        return new Dimension(200, 60);
    case DECISION:
        return new Dimension(180, 100);
    default:
        return null;
    }
}


/////// Paint Methods ////////

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    super.paintComponent(g2);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    try {
        paintChoice(g2);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

public void paintChoice(Graphics2D g2) throws BadLocationException {
    fm = g2.getFontMetrics();
    switch (type) {
    case START:
        data = "Start";
        doc.insertString(0, data, null);
        paintStartEnd(g2);
        break;
    case END:
        data = "End";
        paintStartEnd(g2);
        break;
    case PROCESS:
        data = "";
        paintProcess(g2);
        break;
    case INPUT_OUTPUT:
        data = "";
        paintInputOutput(g2);
        break;
    case DECISION:
        data = "";
        paintDecision(g2);
        break;
    }
}

private void paintStartEnd(Graphics2D g2) {
    int stringx = (getWidth() - fm.stringWidth(data)) / 2;
    int stringy = (getHeight() - (int) fm.getHeight()) / 2 + fm.getAscent();
    g2.setColor(Color.white);
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);
    g2.setColor(Color.black);
    g2.drawOval(0, 0, getWidth() - 1, getHeight() - 1);
    g2.drawString(data, stringx, stringy);
}

private void paintProcess(Graphics2D g2) {
    int stringx = (getWidth() - fm.stringWidth(data)) / 2;
    int stringy = (getHeight() - (int) fm.getHeight()) / 2 + fm.getAscent();
    g2.setColor(Color.white);
    g2.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
    g2.setColor(Color.black);
    g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
    g2.drawString(data, stringx, stringy);
}

private void paintInputOutput(Graphics2D g2) {
    Polygon parragram = makeParrallelogram();
    int stringx = (getWidth() - fm.stringWidth(data)) / 2;
    int stringy = (getHeight() - (int) fm.getHeight()) / 2 + fm.getAscent();
    g2.setColor(Color.white);
    g2.fillPolygon(parragram);
    g2.setColor(Color.black);
    g2.drawPolygon(parragram);
    g2.drawString(data, stringx, stringy);
}

private void paintDecision(Graphics2D g2) {
    Polygon diamond = makeDiamond();
    int stringx = (getWidth() - fm.stringWidth(data)) / 2;
    int stringy = (getHeight() - (int) fm.getHeight()) / 2 + fm.getAscent();
    g2.setColor(Color.white);
    g2.fillPolygon(diamond);
    g2.setColor(Color.black);
    g2.drawPolygon(diamond);
    g2.drawString(data, stringx, stringy);
}


/////// Make Polygons ///////

private Polygon makeParrallelogram() {
    Polygon parragram = new Polygon();
    parragram.addPoint(0, 0);
    parragram.addPoint(30, 59);
    parragram.addPoint(199, 59);
    parragram.addPoint(169, 0);
    return parragram;
}

private Polygon makeDiamond() {
    Polygon diamond = new Polygon();
    diamond.addPoint(getWidth() / 2, getHeight() - 1);
    diamond.addPoint(getWidth() - 1, getHeight() / 2);
    diamond.addPoint(getWidth() / 2, 0);
    diamond.addPoint(0, getHeight() / 2);
    return diamond;
}
}

If I take out the things specific to JTextComponents like the Document and make this class extend JComponent then I do not get the same error. I need help figuring out what is wrong.

(I know the document doesn't do anything yet but I working on making that work as well, so don't say I should delete it and make it extend JComponent because this will be a textComponent when I am done.)

Thanks in advance.

0

There are 0 answers