Showing posts with label Background. Show all posts
Showing posts with label Background. Show all posts

Thursday, February 22, 2018

Setting all JPanel, JCheckbox, JRadioButton in your project non-opaque / transparent through UIManager

To set the transparency of an individual JComponent, we use the JComponent.setOpaque(boolean opaque) to achieve it.

By default, a JPanel is opaque. To set all the JPanel objects in your whole project to be non-opaque, you can achieve it through modifying the UIManager once instead of setting each JPanel object to be non-opaque by calling setOpaque(false).

The odd thing is that the code UIManager.put("Panel.opaque", false) does not always work. In fact, it has never worked for me.

The way it works for me is by setting the background color of panels through the UIManager. There are two Color constructors that take four parameters.

Color(int r, int g, int b, int alpha)
Color(float r, float g, float b, float alpha)

The last parameter alpha specifies the transparency of the color.

For the int argument, alpha is a value between 0 and 255; 0 is completely transparent and 255 completely non-transparent.

For the float argument, alpha is a value between 0.0 and 1.0; 0.0 is complete transparent and 1.0 completely non-transparent.

To set all your JPanel objects transparent (non-opaque), use the following code before constructing any of the JPanel objects:

UIManager.put("Panel.background", new Color(110, 110, 110, 0);
or
UIManager.put("Panel.background", new Color(0.5, 0.5, 0.5, 0.0);

To set all your JPanel objects non-transparent(opaque):
UIManager.put("Panel.background", new Color(110, 110, 110, 255);
or
UIManager.put("Panel.background", new Color(0.5, 0.5, 0.5, 1.0);

Of course, you can set the transparency to any value in between.

Similarly, you can set the transparency of your JCheckbox and JRadioButton objects by setting the values for the CheckBox.background and RadioButton.foreground keys in the UIManager.

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

                        
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, May 30, 2014

Setting text styles, colors, font and alignment in JTextPane (2): html

You can also setting the styles, colors, font and other character of the text in JTextPane using html. Following is an example code.

import java.awt.Color;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class HtmlTest extends JTextPane {
 
    public static void main(String[] args){
        //create the JTextPane and set the ContentType to text/html
        HtmlTest test = new HtmlTest();
        test.setContentType("text/html");
        test.setBackground(Color.yellow);
     
        String str = "<p align='center'><font size='14' color='blue' style='background-color: gray;'>Style Test Result</font></p><br/>"+
                    "<font size='8' color='red' style='background-color:green;'>the first line</font><br/>"+
                    "the second line<br/>";
     
        //get the HTMLEditorKit
        HTMLDocument doc = (HTMLDocument)test.getDocument();
        HTMLEditorKit editorKit = (HTMLEditorKit)test.getEditorKit();
        try{
            //insert the text using the HTMLEditorKit
            editorKit.insertHTML(doc, doc.getLength(), str,0,0, null);
        }catch(BadLocationException ble){
            ble.printStackTrace(System.out);
        } catch(IOException ioe){
            ioe.printStackTrace(System.out);
        }
     
        JFrame f = new JFrame("Style Test");
        f.setContentPane(new JScrollPane(test));
        f.setSize(500, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

           Previous <

References

1. Setting text styles, colors, font and alignment in JTextPane (1): attributes

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

                        
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.

Thursday, May 29, 2014

Setting text styles, colors, font and alignment in JTextPane (1): attributes

JTextPane allows to set font, styles, foreground color, background color, and text alignment for each segment of  the text, which is very difficult to do with JTextArea. Following is a sample code for setting some of this characters in JTextPane.

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class AttributeTest extends JTextPane {
    SimpleAttributeSet attributeSet;
 
    public AttributeTest() {
        //create the SimpleAttributeSet
        attributeSet = new SimpleAttributeSet();
    }
 
    public void addString(int fontSize,
            Color foregroundColor,
            Color backgroundColor,
            boolean isBold,
            boolean isItalic,
            int alignment,
            String str) {
        //add the attributes to the SimpleAttributeSet
        StyleConstants.setFontSize(attributeSet, fontSize);
        StyleConstants.setForeground(attributeSet, foregroundColor);
        StyleConstants.setBackground(attributeSet, backgroundColor);
        StyleConstants.setBold(attributeSet, isBold);
        StyleConstants.setItalic(attributeSet, isItalic);
        StyleConstants.setAlignment(attributeSet, alignment);
     
        //set the attribute to the StyledDocument
        int len = this.getText().length();    
        StyledDocument sDoc = this.getStyledDocument();
        sDoc.setCharacterAttributes(len, str.length(), attributeSet, false);

        // Or using the following two lines of code to replace the above two lines of code
       // this.setCaretPosition(len);
      //  this.setCharacterAttributes(attributeSet, false);
   
       //insert the string to the JTextPane
        this.replaceSelection(str);
    }
 
    public static void main(String[] args){
        String[] strs = {"The Third Line\n",
                         "The Second Line\n",
                         "The First Line\n",
                         "Style Test Result\n"
                         };
        AttributeTest test = new AttributeTest();
        for (int i=0; i<strs.length; i++) {
            if (i == 3){
                test.addString(14, Color.blue, Color.LIGHT_GRAY, true, false, StyleConstants.ALIGN_CENTER, strs[i]);
            } else if (i == 2){
                test.addString(11, Color.red, Color.GREEN, false, true, StyleConstants.ALIGN_LEFT, strs[i]);
            } else if (i == 1) {
                test.addString(11, Color.orange, Color.LIGHT_GRAY, false, false, StyleConstants.ALIGN_LEFT, strs[i]);
            } else {
                test.addString(11, Color.black, Color.CYAN, false, false, StyleConstants.ALIGN_LEFT, strs[i]);
            }
        }
        test.setBackground(Color.yellow);
        JFrame f = new JFrame("Style Test");
        f.setContentPane(new JScrollPane(test));
        f.setSize(500, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

                                                    > next

References

1. Setting text styles, colors, font and alignment in JTextPane (2): html

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

                        
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? Order Here

Monday, January 6, 2014

Background of JRadioButton is blue after it is unselected

If the background of a JRadioButton turns blue after it is unselected, do the following to change it to white.

button.getModel().setArmed(false);
button.getModel().setPressed(false);

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

                        
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.