I have to make a Java Program using Swing and I want to show some text, but not using JLabel
(including text component)! I have to extends JFrame
. What other options do I have?
Asked
Active
Viewed 5,062 times
4

Ian Roberts
- 120,891
- 16
- 170
- 183

lolllol
- 61
- 1
- 4
-
5That's a rather strange restriction. Do you have any other restrictions we should know about before suggesting answers? Why can you not use a JLabel? – Duncan Jones Oct 23 '12 at 17:35
-
1I don't think this question is too localized—there may be a reason to render text directly—but more context would help. – trashgod Oct 23 '12 at 17:43
-
1Other restrictions could be if the text must wrap the words before show. – Roman C Oct 23 '12 at 17:46
-
Dont rewrite/delete question text with nonsense, either delete it, leave it for now and come back later when you know exactly or mark as solved if it is. But editing a question to now say: *I have to think about it more...* is not good idea it will be removed. – David Kroukamp Oct 23 '12 at 18:24
1 Answers
4
You should not:
- Extend
JFrame
unnecessarily - Draw directly to
JFrame
viapaint(..)
You should:
- override
JComponent
spaintComponent(...)
- do custom painting in
paintComponent(...)
, - add
JComponent
instance toJFrame
Here is an example for you:
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DrawSimpleText {
public DrawSimpleText() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new DrawSimpleText();
}
});
}
private void initComponents() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JPanel(true) {
Font font = new Font("Serif", Font.PLAIN, 20);
String s = "Java Source and Support";
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(font);
FontMetrics fm = g.getFontMetrics(font);
int width = fm.stringWidth(s);
Dimension d = getSize();
//center String/text
int cx = (d.width - width) / 2;
int cy = (d.height - fm.getHeight()) / 2 + fm.getAscent();
g2.drawString(s, cx, cy);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 100);
}
});
f.pack();
f.setVisible(true);
}
}
UPDATE:
As per your comment though its going against all Swing rules and my own morals:
However if you still need to do this (which I cannot fathom why this is just bad practice) here is how:
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class DrawSimpleText extends JFrame {
public DrawSimpleText() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new DrawSimpleText();
}
});
}
private void initComponents() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
Font font = new Font("Serif", Font.PLAIN, 20);
String s = "Java Source and Support";
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(font);
FontMetrics fm = g.getFontMetrics(font);
int width = fm.stringWidth(s);
Dimension d = getSize();
//center String/text
int cx = (d.width - width) / 2;
int cy = (d.height - fm.getHeight()) / 2 + fm.getAscent();
g2.drawString(s, cx, cy);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 100);
}
}

David Kroukamp
- 36,155
- 13
- 81
- 138
-
-
2Also consider `TextLayout` for better metrics, as shown [here](http://stackoverflow.com/a/4287269/230513). – trashgod Oct 23 '12 at 17:40
-
@user1769093 that is not considered good practice, but see my updated post – David Kroukamp Oct 23 '12 at 17:42
-
-
1
-
@RomanC +1 yes good one added double buffer. What hard coded values the text co-ordinates? – David Kroukamp Oct 23 '12 at 17:54
-
-
1@RomanC while I'm all for flexible APIs, this is just an example & demonstrates the basic requirements of the OP. +1 – MadProgrammer Oct 23 '12 at 18:05
-
1
-
-
@MadProgrammer Showing a bad practices in the example doesn't fill the requirements of the OP. – Roman C Oct 23 '12 at 18:10
-
@MadProgrammer +1 yup I do agree, although we should give good examples whats the use of giving an example that is near perfect OP wont learn... – David Kroukamp Oct 23 '12 at 18:10
-
1@david teach em to fish, sometimes pointing them in the right direction is more important then giving then the right answer – MadProgrammer Oct 23 '12 at 18:14
-
2@romanc I don't think this is a bad example. There are no requirements from the OP with regards to layout, they wanted to draw text. If we start second guessing the requirements we could not only be wasting our time but also introducing irrelevancy into the answer. I agree, we should correct bad practice when we see it and encourage good practice, but there's only so much we can guess at. I don't think in this case going all out with providing properties to set the text position is required to answer the OPs question ... IMHO – MadProgrammer Oct 23 '12 at 18:17