How to eliminate the gap between large Java Swing Tags
In my application, I have a label with a font size of more than 200 This label contains large up and down (irregular) gaps How do I delete it?
This is my code:
package Core;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class LabelDemo extends JPanel {
public LabelDemo() {
super(new GridBagLayout());
JLabel label2;
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
// Create the other labels.
label2 = new JLabel("Text-Only Label");
label2.setBorder(BorderFactory.createTitledBorder("aaaaaaaa"));
label2.setFont(new Font("Verdana",Font.PLAIN,(int) 220));
// label2.setBorder(new EmptyBorder(-50,0));
// Add the labels.
add(label2,c);
}
/**
* Create the GUI and show it. For thread safety,this method should be invoked from the event dispatch thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("LabelDemo");
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
// Add content to the window.
frame.add(new LabelDemo());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
SwingUtilities.invokelater(new Runnable() {
public void run() {
// Turn off Metal's use of bold fonts
UIManager.put("swing.boldMetal",Boolean.FALSE);
createAndShowGUI();
}
});
}
}
I also tried my last article: how to change gap in swing label and tried to use insets, but this looks different in Linux and windows
Is there a better way to close this gap?
Solution
Jdigit may give you some ideas:
>It overrides paintcomponent() to downsample high-resolution bufferedimage and control geometry. > It uses setborderpainted (false) to set the borderpainted property. > It uses focushandler for custom highlighting
Appendix: as mentioned here, the fundamental problem is the leading of the font, which is defined in fontmetrics as included in the height of the font As suggested in @ Guillaume polet's comments, you can render text anywhere in your JComponent You can use the textlayout discussed here to calculate the boundary, as shown below
advantage:
>Absolute control placement. > The geometry of textelayout boundary based on fontmetrics
Disadvantages:
>No icon support. > No HTML support
Note that the JComponent author "recommends that you put components in JPanel and set borders on JPanel."
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* @see https://stackoverflow.com/a/16014525/230513
*/
public class UnleadedTest {
private static class Unleaded extends JComponent {
private Font font = new Font("Verdana",144);
private FontRenderContext frc = new FontRenderContext(null,true,true);
private String text;
private TextLayout layout;
private Rectangle r;
public Unleaded(String text) {
this.text = text;
calcBounds();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(r.width,r.height);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
calcBounds();
layout.draw(g2d,-r.x,-r.y);
}
private void calcBounds() {
layout = new TextLayout(text,font,frc);
r = layout.getPixelBounds(null,0);
}
}
private void display() {
JFrame f = new JFrame("Unleaded");
f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
Unleaded label = new Unleaded("Unleaded");
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Title"));
panel.add(label);
f.add(panel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokelater(new Runnable() {
@Override
public void run() {
new Unleadedtest().display();
}
});
}
}
