Tuesday, October 31, 2017

Java: Resize font size automatically when the size of a JLabel is changed

You have JLabels in your Swing GUI interface and are set to fill the empty spaces when the GUI interface is resized. You want the size of the text on the label to change accordingly when the size of the label changes.

One way to do it is to set a range of the text font size, add a ComponentListener to the label and reset the size of the text font. Below is a sample code.

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class LabelFontResizeTest extends JLabel {
    //The range of the font size
    private static final int MIN_FONT_SIZE = 3;
    private static final int MAX_FONT_SIZE = 240;
    private Graphics g;

    public LabelFontResizeTest(String text) {
        super(text);
        addComponentListener(new LabelListener());
    }

    //The ComponentListener that sets the font size
    private class LabelListener extends ComponentAdapter {

        @Override
        public void componentResized(ComponentEvent e) {
            if (g == null) {
                return;
            }
            Rectangle r = LabelFontResizeTest.this.getBounds();
            int fontSize = MIN_FONT_SIZE;
            Font f = LabelFontResizeTest.this.getFont();

            Rectangle r1 = new Rectangle();
            Rectangle r2 = new Rectangle();
            while (fontSize < MAX_FONT_SIZE) {
                r1.setSize(getTextSize(LabelFontResizeTest.this, f.deriveFont(f.getStyle(), fontSize)));
                r2.setSize(getTextSize(LabelFontResizeTest.this, f.deriveFont(f.getStyle(), fontSize + 1)));
                if (r.contains(r1) && !r.contains(r2)) {
                    break;
                }
                fontSize++;
            }

            setFont(f.deriveFont(f.getStyle(), fontSize));
            repaint();
        }
    }

    private Dimension getTextSize(JLabel l, Font f) {
        Dimension size = new Dimension();
        g.setFont(f);
        FontMetrics fm = g.getFontMetrics(f);
        size.width = fm.stringWidth(l.getText());
        size.height = fm.getHeight();

        return size;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.g = g;
    }

    public static void main(String[] args) throws Exception {
        LabelFontResizeTest label = new LabelFontResizeTest("Magic Text expand and shrinks automatically!!!");
        JFrame frame = new JFrame("Resize label font");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(label);

        frame.setSize(300, 100);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}



Reference

1. How to change JLabel font size to fill JPanel free space while resizing?

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

Wednesday, October 25, 2017

Java: Conversions between boxed number Integer[] array and unboxed primitive int[] array

int [] unboxed = {3, 6, 9, 12, 15, 18, 21, 24, 27};

Integer [] boxed = IntStream.of(unboxed).boxed().toArray(Integer[]::new);

int [] unboxed2 = Stream.of(boxed).mapToInt(Integer::intValue).toArray();

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Java: Convert List to primitive Array long[] ; and primitive Array long[] to List

For instance, you have a list of Long object.

        List<Long> list = <your list>;

You want to convert it to a long[].

1. Java 8
 
        Convert the list to a Stream<Long>, map each Long object to long, and then convert the result to an array.

        long[] array = list.stream().mapToLong(Long::longValue).toArray();

2. Before Java 8 versions

        Created an empty array with the size of your list, loop through the list and add each element to the array.

         long [] array = new long[list.size()];
         int i = 0;
         for (long element : list) {
                 array[i++] = element;
         }

Now you have a long[] array2 object and you want to convert it to a List<Long> object.

3. Java 8

        List<Long> list2 = Arrays.stream(array2).boxed().collect(Collectors.toList());

4. Before Java 8 versions.

        List<Long> list2 = new ArrayList<>();
        for (long value : array2) {
                list2.add(new Long(value));
        }

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.