Barra de Status [RESOLVIDO]

7 respostas
M

Alguem sabe como inserir barra de statu no java??

por exemplo… quando eue stiver fazendo um cadastro de pessoa por exeplo… ao inves de eu colocar um JOptionPane eu queria colocar
em baixo na tela… Salvo com sucesso e ficar por uns 5 segundos essa mesnagem no status pro usuario saber que foi cadastrado com sucesso,
pq se eu colocar um JOptionPane seria desagradavel para o usuario ficar recebendo toda hora essa mensagem, já com uma barra de status ele iria
poder começar um novo cadastro sem perder tempo…

sera que tem como fazer isso??

Obrigado!!!

7 Respostas

D

marcos.stuchi:
Alguem sabe como inserir barra de statu no java??

por exemplo… quando eue stiver fazendo um cadastro de pessoa por exeplo… ao inves de eu colocar um JOptionPane eu queria colocar
em baixo na tela… Salvo com sucesso e ficar por uns 5 segundos essa mesnagem no status pro usuario saber que foi cadastrado com sucesso,
pq se eu colocar um JOptionPane seria desagradavel para o usuario ficar recebendo toda hora essa mensagem, já com uma barra de status ele iria
poder começar um novo cadastro sem perder tempo…

sera que tem como fazer isso??

Obrigado!!!


Não sei se tem um jeito melhor, mas você pode fazer isso com um label que inicialmente fica invisível depois quando for exibir a mensagem tu seta o texto nele, para fazer com que o label fique na tela apenas por 5 segundos dê um sleep na thread, segue o exemplo:

public class Janela extends JFrame {	
	/**
	 * A constante serialVersionUID
	 */
	private static final long serialVersionUID = -6391611250034404253L;

	private JLabel label = new JLabel("");
	private JButton botao = new JButton("Clique");

	public Janela() {
		this.setSize(300, 150);
		this.setTitle("Exemplo");
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setVisible(true);
		this.label.setVisible(false);
		this.label.setHorizontalAlignment(JLabel.CENTER);
		this.botao.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				dormir();
			}
		});
		this.setLayout(new BorderLayout());
		this.add(botao, BorderLayout.CENTER);
		this.add(label, BorderLayout.SOUTH);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new Janela();
			}
		});
	}

	public void dormir() {
		try {
			Thread.sleep(5000);
			this.label.setText("Cadastro realizado com sucesso.");			
			this.label.setVisible(true);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}	  
	}
}
M

dessa maneira deu certo, só nao consegui deixar a mensagem por 5 segundos…

vc teria como me explicar melhor como eu deixo a mensagem durante 5 segundos?

F

Tente assim:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;


//VS4E -- DO NOT REMOVE THIS LINE!
public class Barra extends JFrame {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel0;
	private JButton jButton0;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public Barra() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getJLabel0(), new Constraints(new Leading(47, 10, 10), new Leading(190, 10, 10)));
		add(getJButton0(), new Constraints(new Leading(53, 10, 10), new Leading(60, 10, 10)));
		setSize(320, 240);
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("jButton0");
			jButton0.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					jButton0ActionActionPerformed(event);
				}
			});
		}
		return jButton0;
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			jLabel0.setText("Bem-Vindo");
		}
		return jLabel0;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Barra frame = new Barra();
				frame.setDefaultCloseOperation(Barra.EXIT_ON_CLOSE);
				frame.setTitle("Barra");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private void jButton0ActionActionPerformed(ActionEvent event) {
		jLabel0.setText("Cadastrado com sucesso");
		threadR(jLabel0);
		
		
	}
	
	
	public void threadR(final JLabel label){
		Thread tThread = new Thread(new Runnable() {
			
			@Override
			public void run() {

					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
	
						e.printStackTrace();
					}
					repaint();
					label.setText("");
				
			}
		});
		tThread.start();
	}
	

}
M

pra esses 3 IMPORTS

import org.dyno.visual.swing.layouts.Constraints;

import org.dyno.visual.swing.layouts.GroupLayout;

import org.dyno.visual.swing.layouts.Leading;

eu preciso de alguma biblioteca??

teria como me passar o link pra baixar essa biblioteca pra eu fazer testes?

F

Use apenas isso, o resto vc inseri em sua aplicação.

private void jButton0ActionActionPerformed(ActionEvent event) {  //Aqui é o evento de quando clicar em um botão
        jLabel0.setText("Cadastrado com sucesso");  
        threadR(jLabel0);  
          
          
    }  
      
      
    public void threadR(final JLabel label){  
        Thread tThread = new Thread(new Runnable() {  
              
            @Override  
            public void run() {  
  
                    try {  
                        Thread.sleep(5000);  
                    } catch (InterruptedException e) {  
      
                        e.printStackTrace();  
                    }  
                    repaint();  
                    label.setText("");  
                  
            }  
        });  
        tThread.start();  
    }  
      
  
}
F

Implementei usando o código do cara acima, veja:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class Janela3 extends JFrame {      
    /** 
     * A constante serialVersionUID 
     */  
    private static final long serialVersionUID = -6391611250034404253L;  
  
    private JLabel label;  
    private JButton botao = new JButton("Clique");  
  
    public Janela3() {  
    	label = new JLabel("");
        this.setSize(300, 150);  
        this.setTitle("Exemplo");  
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);  
        this.setVisible(true);  
        this.label.setVisible(true); 

        
        this.label.setHorizontalAlignment(JLabel.CENTER);  
        this.botao.addActionListener(new ActionListener() {  
            @Override  
            public void actionPerformed(ActionEvent ae) {  
            	label.setText("Cadastrado com sucesso");
        		threadR(label);
        		  
            }  
        });  
        this.setLayout(new BorderLayout());  
        this.add(botao, BorderLayout.CENTER);  
        this.add(label, BorderLayout.SOUTH);  
    }  
  
    public static void main(String[] args) {  
        SwingUtilities.invokeLater(new Runnable() {  
            public void run() {  
                new Janela3();  
            }  
        });  
    }  
  
    public void threadR(final JLabel label){
		Thread tThread = new Thread(new Runnable() {
			
			@Override
			public void run() {

					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
	
						e.printStackTrace();
					}
					repaint();
					label.setText("");
				
			}
		});
		tThread.start();
	}
}

Boa sorte.

M

deu certo pessoal…

com esse codigo

private void jButton0ActionActionPerformed(ActionEvent event) {  //Aqui é o evento de quando clicar em um botão  
       jLabel0.setText("Cadastrado com sucesso");    
       threadR(jLabel0);    
           
           
   }    
       
       
   public void threadR(final JLabel label){    
       Thread tThread = new Thread(new Runnable() {    
               
           @Override    
           public void run() {    
  
                   try {    
                       Thread.sleep(5000);    
                   } catch (InterruptedException e) {    
       
                       e.printStackTrace();    
                   }    
                   repaint();    
                   label.setText("");    
                   
           }    
       });    
       tThread.start();    
   }

mto Obrigado… vou colocar resolvido no meu post

Criado 24 de fevereiro de 2012
Ultima resposta 24 de fev. de 2012
Respostas 7
Participantes 3