Wednesday, November 22, 2017

[Solved] google blog title and comment do not show angle brackets and the content within

You are trying to compose a google blog, and you set your title to "The animals mentioned in <Harry Potter>". After you finished and published your blog, you are surprised to see that the title of your blog becomes "The animals mentioned in", the <Harry Potter> part disappeared.

A set of angle brackets denotes a tag in HTML, therefore is not displayed. The google blog automatically translates angle brackets in the blog content into &lt; and &gt;. For example, <Harry Potter> is translated to &lt;Harry Potter&gt;

The conclusion is that if you want to display angle brackets in your blog title, use &lt; for < and &gt; for >.

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

                        
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.


NetBeans: The <classpath> for <junit> must include junit.jar if not in Ant's own classpath

This error happens because the JUnit jar is missing from your class path. When this error occurs while you are trying to run your project:

1. Right-click on your project, and select Properties.

2. In the popup window, click on the Libraries under the Categories and check if the JUnit jar or library presents.

3. Add the JUnit-x.xx.jar to your library or build it as a library and add the library. You can get the JUnit jar from here.

-----------------------------------------------------------------------------------------------------------------
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.

Use ComponentListener to resize font when the size of a JPanel / JFrame is changed

After a container is maximized or stretched to a very large size, the initial font may be too small to look nice in the big container. One way to resize the font according to the size of the container is through a ComponentListener. Below is a sample code.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

public class PanelFontResize extends JFrame {

    private JPanel thePanel;
    private JLabel theLabel;
    private JTextField theText;
    private JTable theTable;

    public PanelFontResize() {

        theLabel = new JLabel("The Topic: ");
        theText = new JTextField("Does the Sun rise the same time each day?");
        JPanel northPane = new JPanel(new GridBagLayout());
        northPane.add(theLabel, new GridBagConstraints(0,0,1,1,1.0,1.0,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0,0,0,5),0,0));
        northPane.add(theText, new GridBagConstraints(1,0,2,1,2.0,1.0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0),0,0));
     
        String[] colnames = {"Season", "Sun Rise"};
        String[][] data = {{"Spring", "6:00"},
                    {"Summer", "5:30"},
                    {"Autum", "6:30"},
                    {"Winter", "7:00"}};
        theTable = new JTable(data, colnames);
        JScrollPane sp = new JScrollPane(theTable);
     
        thePanel = new JPanel(new BorderLayout());
        thePanel.add(northPane, BorderLayout.NORTH);
        thePanel.add(sp, BorderLayout.CENTER);
        thePanel.addComponentListener(new PanelListener());
     
        setContentPane(thePanel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 200);
        validate();
        setVisible(true);
    }
 
 
    private class PanelListener extends ComponentAdapter {

        @Override
        public void componentResized(ComponentEvent e) {
            Dimension psize = thePanel.getSize();
            if (psize.width <= 761 && psize.height <= 551) {
                setFonts(11);
            } else if (psize.width <= 984 && psize.height <= 617
                    && psize.width > 761 && psize.height > 551) {
                setFonts(13);
            } else if (psize.width <= 1046 && psize.height <= 762
                     && psize.width > 984 && psize.height > 617) {
                setFonts(15);
            } else if (psize.width <= 1245 && psize.height <= 867
                    && psize.width > 1046 && psize.height > 762) {
                setFonts(18);
            } else if (psize.width > 1245 && psize.height > 867){
                setFonts(20);
            }
        }
    }
 
    public void setFonts(int fontSize) {

        Font plainFont = new Font("Dialog", Font.PLAIN, fontSize);
        Font boldFont = new Font("Dialog", Font.BOLD, fontSize + 2);

        UIManager.put("Text.font", new FontUIResource(plainFont));
        UIManager.put("TextField.font", new FontUIResource(plainFont));
        UIManager.put("Table.font", new FontUIResource(plainFont));
        UIManager.put("Label.font", new FontUIResource(boldFont));
        UIManager.put("TableHeader.font", new FontUIResource(boldFont));
        UIManager.put("Panel.font", new FontUIResource(plainFont));
        UIManager.put("ScrollPane.font", new FontUIResource(plainFont));
     
        setTableSize(fontSize);
        SwingUtilities.updateComponentTreeUI(thePanel);
    }
 
    private void setTableSize(int fontSize){
            Font f = getFont().deriveFont((float)fontSize);
            theTable.setRowHeight(new Float(f.getSize2D() * 1.5).intValue());
    }
 
    public static void main(String[] args){
        new PanelFontResize();
    }
}

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

                        
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.


Tuesday, November 21, 2017

Use UIManager to set fonts, foreground color, background color, select color, border for Swing components

Once you set the properties in the UIManager, all your Swing components will have the corresponding look and feel.

public void setLookAndFeel () {

        Font plainFont = new Font("Serif", Font.PLAIN, 12);
        Font boldFont = new Font("Serif", Font.BOLD, 12);

        Color focusColor = Color.BLUE;
        Color backgroundColor = Color.YELLOW;
        Color disabledForgroundColor = Color.GREY;
        Color selectColor = Color.LIGHT_GRAY;

        UIManager.put("Text.font", new FontUIResource(plainFont));
        UIManager.put("TextField.font", new FontUIResource(plainFont));
        UIManager.put("FormattedTextField.font", new FontUIResource(plainFont));
        UIManager.put("PasswordField.font", new FontUIResource(boldFont));
        UIManager.put("TextArea.font", new FontUIResource(plainFont));
        UIManager.put("TextPane.font", new FontUIResource(plainFont));
        UIManager.put("List.font", new FontUIResource(plainFont));
        UIManager.put("Table.font", new FontUIResource(plainFont));
        UIManager.put("TableHeader.font", new FontUIResource(plainFont));
                   
        UIManager.put("CheckBox.font", new FontUIResource(plainFont));
        UIManager.put("RadioButton.font", new FontUIResource(plainFont));
        UIManager.put("Button.font",new FontUIResource(boldFont));
        UIManager.put("Button.focus", new ColorUIResource(focusColor));
        UIManager.put("Button.border", new LineBorder(Color.ORANGE, 5, true));
        UIManager.put("ComboBox.font", new FontUIResource(plainFont));
        UIManager.put("ComboBox.TableHeader.font", new FontUIResource(plainFont)); 
        UIManager.put("Label.font", new FontUIResource(plainFont));
        UIManager.put("ToolTip.font", new FontUIResource(boldFont));
        UIManager.put("ToggleButton.font", new FontUIResource(plainFont));
        UIManager.put("TitledBorder.font", new FontUIResource(boldFont));
     
        UIManager.put("Menu.font", new FontUIResource(boldFont));
        UIManager.put("MenuItem.font", new FontUIResource(boldFont));
        UIManager.put("MenuBar.font", new FontUIResource(boldFont));
        UIManager.put("PopupMenu.font", new FontUIResource(boldFont));
        UIManager.put("RadioButtonMenuItem.font", new FontUIResource(boldFont));
        UIManager.put("CheckBoxMenuItem.font", new FontUIResource(boldFont));
        UIManager.put("MenuItem.acceleratorFont", new FontUIResource(boldFont));
        UIManager.put("CheckBoxMenuItem.acceleratorFont", new FontUIResource(boldFont));
        UIManager.put("Menu.acceleratorFont", new FontUIResource(boldFont));
        UIManager.put("RadioButtonMenuItem.acceleratorFont", new FontUIResource(boldFont));
     
        UIManager.put("ToolBar.font", new FontUIResource(boldFont));
        UIManager.put("MenuBar.font", new FontUIResource(boldFont));
        UIManager.put("Panel.font", new FontUIResource(boldFont));
        UIManager.put("ProgressBar.font", new FontUIResource(boldFont));
        UIManager.put("OptionPane.font", new FontUIResource(boldFont));
        UIManager.put("OptionPane.buttonFont", new FontUIResource(boldFont));
        UIManager.put("OptionPane.messageFont", new FontUIResource(plainFont));
        UIManager.put("ScrollPane.font", new FontUIResource(boldFont));
        UIManager.put("EditorPane.font", new FontUIResource(plainFont));
        UIManager.put("ColorChooser.font", new FontUIResource(boldFont));
        UIManager.put("InternalFrame.titleFont", new FontUIResource(boldFont));
        UIManager.put("TabbedPane.font", new FontUIResource(boldFont));

        UIManager.put("Panel.background", backgroundColor);
        UIManager.put("CheckBox.background", backgroundColor);
        UIManager.put("RadioButton.background", backgroundColor);
        UIManager.put ("Button.select", selectColor);
UIManager.put ("ToggleButton.select", selectColor);
        UIManager.put("ComboBox.disabledBackground", backgroundColor);
        UIManager.put("ComboBox.fieldBackground", backgroundColor);
        UIManager.put("ComboBox.disabledForeground", disabledForgroundColor);
        UIManager.put("OptionPane.background", backgroundColor);
        UIManager.put("OptionPane.warningDialog.border.background", backgroundColor);
        UIManager.put("Label.background", backgroundColor);
        UIManager.put("TextField.shadow", backgroundColor);
        UIManager.put("TextField.background", backgroundColor); 
    }

Reference:

1. Swing UIManager Keys
2. Nimbus Defaults

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

                        
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.

Friday, November 3, 2017

JLabel: setHorizontalTextPosition vs setHorizontalAlignment

A JLabel can display a text string or an image or both.

The setHorizontalTextPosition(int textPosition) method is used when a JLabel displays both an image and a text. This method specifies whether the text should be displayed on the left or right side or center of the image. It takes an argument which is one of these SwingConstants values: LEFT, CENTER, RIGHT, LEADING or TRAILING. TRAILING is the default position.

The setHorizontalAlignment(int alignment) method is used to specify the alignment of the whole contents on the JLable. It also takes an argument which is one of these SwingConstants values: LEFT, CENTER, RIGHT, LEADING or TRAILING.

For example, you have a JLabel which only displays the text "User Name", and you want the text to be at the right side of the label. You have two ways to achieve this.

1. JLabel userLabel = new JLabel ("User Name", SwingConstants.RIGHT");

2. JLabel userLabel = new JLabel();

      userLabel.setText("User Name"); //by default, JLabel displays text at the left edge.
      userLabel.setHorizontalAlignment(SwingConstants.RIGHT);

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

                        
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.