Search code examples
javaswingjtextfield

Is there any way to display text without JLabel or JTextField in java swing?


I'm creating Swing project, but I'm wondering if there's any way to print text without using JLabel or JTextField.

I found out text field could be the one but it isn't as tidy as JLabel. (there's a border around the text).

Can a border in the text field be removed? Or are there other methods I can use?


Solution

  • "I'm wondering if there's any way to print text without using JLable or JtextField"

    You could always draw the text in a JPanel. Takes a lot more work, but you can do a lot more with your text.

    public class TextPanel extends JPanel{
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawString("Hello, Stackoverflow!", xLocation, yLocation);
        }
    }
    

    See Drawing Text with Graphics