Probleminha com JInternalFrame (Label com image)

5 respostas
nelsonnetto

Falae Galera... dei uma vasculhada e não encontrei nada...
O lance eh o seguinte...
Criei um JDesktopPanel com Jmenu, Ao clicar num JmenuItem ele abre os internalFrames.
Dentre dos internalframes carrego 3 label com image...
Os Labels aparecem... mas as imagens não...

Alguem pode me dar uma luz?

Segue o código:

public class Service extends JInternalFrame {
    static int openFrameCount = 0;
    static final int xOffset = 30, yOffset = 30;
    private JLabel lblHTTP, lblSSH, lblFTP; // Labels para os serviços monitorados
    private Status status = new Status(); //instancia a classe Status
    private ImageIcon imgHTTP, imgSSH, imgFTP; //instancia as imagens
    private String checkStatus = new String(); //cria variavel para conferir Status
    private String green = new String("imgs/green.gif");
    private String red = new String("imgs/red.gif");
    private String yellow = new String("imgs/yellow.gif");

    public Service() {
        super("Monitoramento " + (++openFrameCount), 
              true, //resizable
              true, //closable
              true, //maximizable
              true);//iconifiable
        setLayout  (new GridLayout(3,1));

        //...Create the GUI and put it in the window...
        
        checkStatus = status.getStatus();

        //Add the labels.
                
        if ( checkStatus == "off" ) {
            imgHTTP = new ImageIcon(red);
            imgSSH = new ImageIcon(red);
            imgFTP = new ImageIcon(red);
        }
        else if ( checkStatus == "on" ){
            imgHTTP = new ImageIcon(green);
            imgSSH = new ImageIcon(green);
            imgFTP = new ImageIcon(green);
        }
        else{
            imgHTTP = new ImageIcon(yellow);
            imgSSH = new ImageIcon(yellow);
            imgFTP = new ImageIcon(yellow);
        }
        
        lblHTTP = new JLabel();
        lblHTTP.setText("HTTP");
        lblHTTP.setIcon(imgHTTP);
        
        lblSSH = new JLabel();
        lblSSH.setText("SSH");
        lblSSH.setIcon(imgSSH);
        
        lblFTP = new JLabel();
        lblFTP.setText("FTP");
        lblFTP.setIcon(imgFTP);
        
        add (lblHTTP);
        add (lblSSH);
        add (lblFTP);

        //...Then set the window size or call pack...
        setSize(300,300);

        //Set the window's location.
        setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
    }
}

5 Respostas

T
if ( checkStatus == "off" ) {

deveria ser (já que “==” não funciona para Strings)

if ( "off".equals (checkStatus)) {

ou se você puder usar variáveis booleanas em vez de status (estou supondo que seu status é “on” ou “off” fica mais rápido ainda.)

nelsonnetto

thingol:
if ( checkStatus == "off" ) {

deveria ser (já que “==” não funciona para Strings)

if ( "off".equals (checkStatus)) {

ou se você puder usar variáveis booleanas em vez de status (estou supondo que seu status é “on” ou “off” fica mais rápido ainda.)

Valeu pela dica Thingol…
Vou ver isso…

Quanto ao problema de aparecer a imagem dentro do JInternalFrame…
Vc teria alguma idéia?

davidbuzatto

O caminho dessas imagens estão corretos?
Esse “img” é um pacote do teu projeto?

Tenta assim:

imgHTTP = new ImageIcon( getClass().getResource( "caminhoDaSuaImagem" ));

Onde caminho pode ser o caminho completo, ou relativo aos pacotes de seu projeto.

Falow!

renatoes

Acho que vc tentou adicionar cores correto?
Utilize a forma dada pelo colega davidbuzatto
[]'s

nelsonnetto

davidbuzatto:
O caminho dessas imagens estão corretos?
Esse “img” é um pacote do teu projeto?

Tenta assim:

imgHTTP = new ImageIcon( getClass().getResource( "caminhoDaSuaImagem" ));

Onde caminho pode ser o caminho completo, ou relativo aos pacotes de seu projeto.

Falow!

Falaei davidbuzatto ,
implementei como vc comentou…

if ( checkStatus == "off" ) { imgHTTP = new ImageIcon(getClass().getResource("imgs/red.png"));

Mas deu um erro muito do cabuloso…

<blockquote>Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException

at javax.swing.ImageIcon.(ImageIcon.java:138)

at Client.Service.(Service.java:42)

at Client.Cliente.createFrame(Cliente.java:321)

at Client.Cliente.jMEscolheServidorActionPerformed(Cliente.java:243)

at Client.Cliente.access$600(Cliente.java:25)

at Client.Cliente$10.actionPerformed(Cliente.java:187)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)

at javax.swing.AbstractButton.doClick(AbstractButton.java:302)

at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1000)

at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1041)

at java.awt.Component.processMouseEvent(Component.java:5488)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)

at java.awt.Component.processEvent(Component.java:5253)

at java.awt.Container.processEvent(Container.java:1966)

at java.awt.Component.dispatchEventImpl(Component.java:3955)

at java.awt.Container.dispatchEventImpl(Container.java:2024)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)

at java.awt.Container.dispatchEventImpl(Container.java:2010)

at java.awt.Window.dispatchEventImpl(Window.java:1778)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

</blockquote>

E eu na minha pequenes… ahhaha… não faço a menor noção do que seja isso…
Tem como vc me dar uma idéia???

Valeu pela força…

Criado 1 de dezembro de 2006
Ultima resposta 4 de dez. de 2006
Respostas 5
Participantes 4