[RESOLVIDO] Iniciar uma tela com um JTextField com um valor Default

1 resposta Resolvido
jtablejava
Diego_Gomes_Dias

Ola galera, gostaria de que ao abrir minha JPanel o meu JTextField (textNum) esteja ja com o valor 0 preenchido.
Como fazer?

public class CadFornecedor extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textRSocial;
	private JTextField textCnpj;
	private JTextField textEnd;
	private JTextField textNum;
	private JTextField textUF ;
	private JTextField textCidade;
	private JTextField textCEP;
	private JTextField textBairro;
	private JTextField textTelefone;
	private JTextField textNomeFantasia;
	private JTextField textIncSocial;
	private JTextField textComp;
	private JTextField textEmail;
	conexao con = new conexao();
	int type = 0;
	
		
	private void onClickSalvar(){
		FornecedorController cc = new FornecedorController();
		try{
			cc.salvar(	
				textNomeFantasia.getText(), 
				textRSocial.getText(), 
				Integer.parseInt(textCnpj.getText()),
				Integer.parseInt(textIncSocial.getText()),
				type,//tipo de empresa
				textEnd.getText(),
				Integer.parseInt(textNum.getText()),
				Integer.parseInt(textComp.getText()),
				textBairro.getText(),
				textCEP.getText(),
				textTelefone.getText(),
				textCidade.getText(),
				textEmail.getText(),
				textUF.getText());
			JOptionPane.showMessageDialog(this, "Contato salvo com sucesso!");
            clearFields();
		}catch(SQLException e){
			JOptionPane.showMessageDialog(this, 
					"Nao foi possivel salvar contato!n" + 
					e.getLocalizedMessage());
	        } catch (ParseException e) {
	            JOptionPane.showMessageDialog(this, 
					"Data possui formato inválido!n" + 
					e.getLocalizedMessage());
	        }
	}
	
	public void clearFields(){
		textNomeFantasia.setText(""); 
		textRSocial.setText(""); 
		textCnpj.setText("");
		textIncSocial.setText("");
		textEnd.setText("");
		textNum.setText("0");
		textComp.setText("0");
		textBairro.setText("");
		textCEP.setText("");
		textTelefone.setText("");
		textCidade.setText("");
		textEmail.setText("");
		textUF.setText("");
	}
	
		
			public void run() {
				try {
					CadFornecedor frame = new CadFornecedor();
					frame.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

	public CadFornecedor() {
		setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\diego\\Downloads\\shadow\\businessman_add.png"));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 564, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblRazaoSocial = new JLabel("Raz\u00E2o Social:");
		lblRazaoSocial.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblRazaoSocial.setBounds(288, 126, 75, 14);
		contentPane.add(lblRazaoSocial);
		
		textRSocial = new JTextField();
		textRSocial.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textRSocial.setColumns(10);
		textRSocial.setBounds(288, 140, 245, 20);
		contentPane.add(textRSocial);
		
		JLabel lblCnpj = new JLabel("CNPJ:");
		lblCnpj.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblCnpj.setBounds(10, 171, 46, 14);
		contentPane.add(lblCnpj);
		
		textCnpj = new JTextField();
		textCnpj.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textCnpj.setColumns(10);
		textCnpj.setBounds(10, 185, 115, 20);
		contentPane.add(textCnpj);
		
		JLabel lblEndereo = new JLabel("Endere\u00E7o:");
		lblEndereo.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblEndereo.setBounds(11, 225, 75, 14);
		contentPane.add(lblEndereo);
		
		textEnd = new JTextField();
		textEnd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textEnd.setColumns(10);
		textEnd.setBounds(10, 237, 245, 20);
		contentPane.add(textEnd);
		
		JLabel lblNumero = new JLabel("Numero:");
		lblNumero.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNumero.setBounds(288, 225, 59, 14);
		contentPane.add(lblNumero);
		
		textNum = new JTextField();
		textNum.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textNum.setColumns(10);
		textNum.setBounds(288, 237, 86, 20);
		contentPane.add(textNum);
		
		JLabel label_4 = new JLabel("U.F.:");
		label_4.setFont(new Font("Tahoma", Font.PLAIN, 11));
		label_4.setBounds(424, 311, 46, 14);
		contentPane.add(label_4);
		
		textUF = new JTextField();
		textUF.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textUF.setColumns(10);
		textUF.setBounds(424, 323, 66, 20);
		contentPane.add(textUF);
		
		JLabel label_5 = new JLabel("Cidade:");
		label_5.setFont(new Font("Tahoma", Font.PLAIN, 11));
		label_5.setBounds(11, 311, 62, 14);
		contentPane.add(label_5);
		
		textCidade = new JTextField();
		textCidade.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textCidade.setColumns(10);
		textCidade.setBounds(10, 323, 143, 20);
		contentPane.add(textCidade);
		
		JLabel label_6 = new JLabel("C.E.P.:");
		label_6.setFont(new Font("Tahoma", Font.PLAIN, 11));
		label_6.setBounds(288, 268, 46, 14);
		contentPane.add(label_6);
		
		textCEP = new JTextField();
		textCEP.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textCEP.setColumns(10);
		textCEP.setBounds(288, 280, 86, 20);
		contentPane.add(textCEP);
		
		JLabel label_7 = new JLabel("Bairro:");
		label_7.setFont(new Font("Tahoma", Font.PLAIN, 11));
		label_7.setBounds(10, 268, 46, 14);
		contentPane.add(label_7);
		
		textBairro = new JTextField();
		textBairro.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textBairro.setColumns(10);
		textBairro.setBounds(10, 280, 245, 20);
		contentPane.add(textBairro);
		
		JLabel label_8 = new JLabel("Telefone:");
		label_8.setFont(new Font("Tahoma", Font.PLAIN, 11));
		label_8.setBounds(424, 268, 62, 14);
		contentPane.add(label_8);
		
		textTelefone = new JTextField();
		textTelefone.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textTelefone.setColumns(10);
		textTelefone.setBounds(424, 280, 86, 20);
		contentPane.add(textTelefone);
		
		JButton btnProcurar = new JButton("Procurar");
		btnProcurar.setIcon(new ImageIcon("C:\\Users\\diego\\Downloads\\shadow\\businessman_view.png"));
		btnProcurar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		btnProcurar.setBounds(342, 11, 89, 70);
		contentPane.add(btnProcurar);
		btnProcurar.setVerticalTextPosition(SwingConstants.EAST);
		btnProcurar.setHorizontalTextPosition(SwingConstants.CENTER);
		
		JButton btnInserir = new JButton("Inserir");
		btnInserir.setIcon(new ImageIcon("C:\\Users\\diego\\Downloads\\shadow\\businessman_add.png"));
		btnInserir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onClickSalvar();
			}
		});
		btnInserir.setBounds(116, 11, 89, 70);
		contentPane.add(btnInserir);
		btnInserir.setVerticalTextPosition(SwingConstants.EAST);
		btnInserir.setHorizontalTextPosition(SwingConstants.CENTER);
		
		JButton buttonNovo = new JButton("Novo");
		buttonNovo.setIcon(new ImageIcon("C:\\Users\\diego\\Downloads\\shadow\\businessman_preferences.png"));
		buttonNovo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearFields();
			}
		});
		buttonNovo.setBounds(10, 11, 89, 70);
		contentPane.add(buttonNovo);
		buttonNovo.setVerticalTextPosition(SwingConstants.EAST);
		buttonNovo.setHorizontalTextPosition(SwingConstants.CENTER);
		
		textNomeFantasia = new JTextField();
		textNomeFantasia.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textNomeFantasia.setColumns(10);
		textNomeFantasia.setBounds(10, 140, 245, 20);
		contentPane.add(textNomeFantasia);
		
		JLabel lblNomeFantasia = new JLabel("Nome Fantasia:");
		lblNomeFantasia.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNomeFantasia.setBounds(10, 126, 79, 14);
		contentPane.add(lblNomeFantasia);
		
		JButton btnFechar = new JButton("Fechar");
		btnFechar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnFechar.setIcon(new ImageIcon("C:\\Users\\diego\\Downloads\\shadow\\delete.png"));
		btnFechar.setBounds(446, 11, 89, 70);
		contentPane.add(btnFechar);
		btnFechar.setVerticalTextPosition(SwingConstants.EAST);
		btnFechar.setHorizontalTextPosition(SwingConstants.CENTER);
		
		JLabel lblInscSoc = new JLabel("Inscri\u00E7\u00E3o Social:");
		lblInscSoc.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblInscSoc.setBounds(140, 171, 86, 14);
		contentPane.add(lblInscSoc);
		
		textIncSocial = new JTextField();
		textIncSocial.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textIncSocial.setColumns(10);
		textIncSocial.setBounds(140, 185, 115, 20);
		contentPane.add(textIncSocial);
		
		JComboBox box = new JComboBox();
		box.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(box.getSelectedItem().equals("Sociedade limitada"))	type = 1;
				if(box.getSelectedItem().equals("Empresário individual")) type = 2;
				if(box.getSelectedItem().equals("Microempreendedor individual")) type = 3;
				if(box.getSelectedItem().equals("Sociedade SOCIALISTA")) type = 4;
				if(box.getSelectedItem().equals("Sociedade anônima")) type = 5;
				if(box.getSelectedItem().equals("Sociedade em comandita simples")) type = 6;
				if(box.getSelectedItem().equals("Sociedade em comandita por ações")) type = 7;
				if(box.getSelectedItem().equals("Sociedade simples")) type = 8;
				if(box.getSelectedItem().equals("Sem fins lucrativos"))	type = 9;				
			}
		});
		box.setFont(new Font("Tahoma", Font.PLAIN, 13));
		box.setBounds(288, 185, 245, 20);
		contentPane.add(box);
		box.addItem("Sociedade limitada");
		box.addItem("Empresário individual");
		box.addItem("Microempreendedor individual");
		box.addItem("Sociedade SOCIALISTA");
		box.addItem("Sociedade anônima");
		box.addItem("Sociedade em comandita simples");
		box.addItem("Sociedade em comandita por ações");
		box.addItem("Sociedade simples");
		box.addItem("Sem fins lucrativos");
		
		JLabel lblTiposDeEmpresas = new JLabel("Tipos de Empresas:");
		lblTiposDeEmpresas.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblTiposDeEmpresas.setBounds(288, 171, 99, 14);
		contentPane.add(lblTiposDeEmpresas);
		
		JLabel lblComplemento = new JLabel("Complemento:");
		lblComplemento.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblComplemento.setBounds(424, 225, 75, 14);
		contentPane.add(lblComplemento);
		
		textComp = new JTextField();
		textComp.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textComp.setColumns(10);
		textComp.setBounds(424, 237, 86, 20);
		contentPane.add(textComp);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblEmail.setBounds(202, 311, 46, 14);
		contentPane.add(lblEmail);
		
		textEmail = new JTextField();
		textEmail.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textEmail.setColumns(10);
		textEmail.setBounds(202, 323, 172, 20);
		contentPane.add(textEmail);
	}
}

1 Resposta

Jeanderson_Silva
Solucao aceita

coloca na parte onde está sendo instanciado:

textNum.setText("0");
Criado 12 de maio de 2017
Ultima resposta 12 de mai. de 2017
Respostas 1
Participantes 2