A
A
Alexander Smolin2015-09-08 16:57:51
Java
Alexander Smolin, 2015-09-08 16:57:51

How to properly style a JTextField?

There is a transparent window.

package ru.alexp.tools;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JFrame;

public class InputFrame extends JFrame {

    private static InputFrame instance;

    private final ConsoleInputPanel inputPanel = new ConsoleInputPanel();

    public InputFrame() {
        final Dimension dim = getToolkit().getScreenSize();
        setSize(500, 80);
        setLocation(dim.width - getWidth() - 50, dim.height - getHeight() - 200);
        setUndecorated(true);
        setBackground(new Color(255, 255, 255, 0));
        setType(Type.UTILITY);
        add(inputPanel);

        addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                repaint();
            }

            @Override
            public void focusLost(FocusEvent e) {
                repaint();
            }
        });
    }

    public static InputFrame getInputFrame() {
        if (instance == null) {
            instance = new InputFrame();
        }
        return instance;
    }
}


It contains a panel on which a stylized JTextField is drawn.
To redraw the contents of the JTextField, I overwrite paintComponent(Graphics g).
protected void paintComponent(Graphics g) {
    Graphics2D gg = (Graphics2D) g;
    gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    gg.setColor(Color.WHITE);
    gg.setFont(font);
    gg.drawString(getText(), 50, 50);
}

At the first rendering of the text, everything works fine. But when I draw another text, I see an overlay of these drawn lines on the screen.
222cdb05c6b444b6a0d070d1dfec7984.png
(here it was written first "12345" then "5432111")

Possible solutions were immediately found:
  • Before rendering, clear the area with clearRect() -> got a white area below the element
  • Redraw content completely using repaint() in paintComponent() -> got infinite recursion (yeah, I know. stupid. but that's how InputFrame.getInputFrame().repaint() somehow worked.)
  • implement via super.paintComponent() -> white background; to remove the white background setBackground(new Color(255, 255, 255, 0)) -> overlay rendered lines again


Help solve this problem and tell me how to correctly implement the rest of the visual functions of JTextField such as blinking the cursor and highlighting characters.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Smolin, 2015-09-09
@Alex_P97

protected void paintComponent(Graphics g) {
    g.setColor(new Color(255, 255, 255, 0));
    g.clearRect(0, 0, getWidth(), getHeight());
    Graphics2D gg = (Graphics2D) g;
    gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    gg.setColor(Color.WHITE);
    gg.setFont(font);
    gg.drawString(getText(), 15, 50);
}

Didn't help much.
If in JavaFX I find a worthy replacement for Swing's GroupLayout, not GridPanel. I'll move on right away.
But thanks anyway. Let's see what can be done with JavaFX.
(So ​​while the question remains open. I need a Swing solution.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question