Search code examples
javaswingjlabel

What on this JLabel is Invalid?


Ok, I have the following that is printed out when I do a MyBox.toString()

com.swa.fin.esaa.sgb.myclasses.mydatatypes.MyAlphaForLabel[,0,0,0x0,invalid,hidden,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.MatteBorder@1082823,flags=25165824,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=0,height=0],defaultIcon=,disabledIcon=,horizontalAlignment=LEFT,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=TOP,verticalTextPosition=CENTER]

MyBox is setup by:

public void MySetMyBox( Rectangle bounds, Color color, Color bcolor, int bwidth ) {
    MyBox = new MyAlphaForLabel();
    MyBox.setComponentOrientation( ComponentOrientation.LEFT_TO_RIGHT );
    MyBox.setVerticalAlignment( JLabel.TOP );
    MyBox.setHorizontalAlignment( JLabel.LEFT );
    MyBox.setFont( new Font(Font.MONOSPACED, Font.PLAIN, 12 ) );
    MyBox.setBackground( color );
    MyBox.setEnabled( true );
    MyBox.setVisible( MyVisible );
    MyBox.setFocusable( true );
    MyBox.setOpaque( MyOpaque );
    MyBox.setBounds( bounds );
    MyBox.setSize( new Dimension( bounds.width, bounds.height ) );
    MyBox.setPreferredSize( new Dimension( bounds.width, bounds.height ) );
    if ( ( MyDashed ) || ( MySelected ) && ( MyCellType != MYCELLS.ZOOM ) ) {
        MyBox.setBorder( new MyDashedBorder( bcolor, bwidth, bwidth ) );
    } else {
        MyBox.setBorder( new MatteBorder( bwidth, bwidth, bwidth, bwidth, bcolor ) );
    }
}

Where all the MyXxxxx variables are declared as necessary (IE MyVisible is a boolean that is true at this point).

MyBox is a MyAlphaForLabel:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.io.Serializable;
import javax.swing.JLabel;
import com.swa.fin.esaa.sgb.MyData;
import com.swa.fin.esaa.sgb.myclasses.myutils.MyDebug;

public class MyAlphaForLabel extends JLabel implements MyData, Serializable {

    private static final long serialVersionUID = 5522598090700895821L;

    private MyDebug           dbug                    = new MyDebug( MyData.MYDEBUGCHECK.MYALPHAFORLABEL.getOn() );

    public String MyTextString = "";

    public MyAlphaForLabel() {
        super();
        this.setOpaque( false );
    }

    /**
     *  Paint the background using the background Color of the
     *  contained component
     */
    @Override
    public void paintComponent( Graphics graphics ) {

        // Set color for the text label
        int red   = ( this.getBackground().getRed()   >= 50 ) ? ( this.getBackground().getRed()   - 50 ) : ( this.getBackground().getRed()   + 50 ); 
        int green = ( this.getBackground().getGreen() >= 50 ) ? ( this.getBackground().getGreen() - 50 ) : ( this.getBackground().getGreen() + 50 ); 
        int blue  = ( this.getBackground().getBlue()  >= 50 ) ? ( this.getBackground().getBlue()  - 50 ) : ( this.getBackground().getBlue()  + 50 ); 
        int alpha = 128; 

        // Make sure we are displaying something
        if ( this.getBackground().getAlpha() != 0 ) {
            // First draw rectangle
            graphics.setColor( this.getBackground() );
            graphics.fillRect(0, 0, this.getWidth(), this.getHeight());
            // Then draw text label
            graphics.setColor( new Color( red, green, blue, alpha ) );
            graphics.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 10 ) );
            graphics.drawString(MyTextString, 5, 15);
            dbug.Message( "MYALPHACONTAINER", "paintComponent set text to %s ", MyTextString );

        }
    }
}

My Question is what is wrong? Why does it say it is invalid.

Ok, I saw the one answer below and thought I would add some more info:

Ok, but the problem that I have is I add it to a JScrollPane and it doesn't show up.

I have a large data class that has all the important information for the "box" that is represented by my drawn JLabel. For example, I need scaled coordinates that translate from the drawn JLabel to an engineering drawing it represents. Anyway, I used to have the data class be an extension of JLabel (or the MyAlphaForLabel) and it worked fine and showed up on the JScrollPane. I ran into problems when I wanted to export my data to XML because it was picking up the parent class no matter what I did with Accessor being NONE. So, I moved the "JLabel" into a piece of the data record that was not exported by XML. Instead of adding the whole data record to the JScrollPane I now just add the JLabel. Exact same code only change was selected the classes public variable as opposed to the whole class before in the pane.add() statement. But I do not get the JLabel to show on the JScrollPane anymore. And I don't know why.

Any thoughts?

I realize that code is not here but it does amount to a large amount of code.


Solution

  • that just means that your component has not been validated by a layout manager yet