0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jcarlos
  • 135
  • 1
  • 2
  • 10
  • Have a look on this [How to style a text field so it doesnt have a border?][1] [1]: http://stackoverflow.com/questions/2281937/swing-jtextfield-how-to-remove-the-border – junior developper Dec 26 '13 at 14:52
  • I think you can override paint method and paint it on component.Background will be transparent and no border there i think thats what you are looking for – Tomas Bisciak Dec 26 '13 at 14:53
  • 1) `if there's any way to print text without using JLable or JtextField.` Take a look at all components that extends `JTextComponent` 2)`Can a border in the TextField be removed?` Yes – nachokk Dec 26 '13 at 14:56

3 Answers3

1

You seem to be looking for JComponent.setBorder(null). However there are a number of ways to achieve the effect you seem to want..

  1. Opaque JLabel with a BG color.
  2. A custom painted component..

Here is an example of the two standard components.

enter image description here

import java.awt.*;
import javax.swing.*;

class TextFieldNoBorder {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                String s = "Text without borders..";
                JPanel gui = new JPanel(new GridLayout(0,1,5,5));

                JTextField tf = new JTextField(s, 20);
                tf.setBorder(null);

                gui.add(tf);

                JLabel l = new JLabel(s);
                l.setOpaque(true);
                l.setBackground(Color.YELLOW);
                gui.add(l);

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

"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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 2
    This is probably some kind of illness of 90% of Swing Programmers: When they make their own component, they always extend `JPanel` instead of `JComponent`. Why? – Martijn Courteaux Dec 26 '13 at 15:10
  • 1
    Seems a lot of 'heavy lifting' for something that can be done in just a couple of lines of code using either a label or text field.. – Andrew Thompson Dec 26 '13 at 15:10
  • Well, OP didn't really describe what they were trying to do. I was simply answering the question in the above quote :) – Paul Samsotha Dec 26 '13 at 15:12
  • Well, I'm new to Swing so a friend of mine gave me a kinda mission to complete. I think he's trying to teach me how to 'Draw' a text instead of just 'print'. Thanks guys anyway :) – jcarlos Dec 26 '13 at 15:16
0

You can use this!

JTextField textField = new JTextField();
textField.setBorder(javax.swing.BorderFactory.createEmptyBorder());

This will set the empty border to your TextField

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53