3

I Need to fetch the username from windows and i need to assign that username for my swing application and how can i do that. Here is my code

import javax.swing.*;    
import java.awt.event.*;  
public class PasswordFieldExample {  
    public static void main(String[] args) {    
    JFrame f=new JFrame("User-ID");    
     final JLabel label = new JLabel();            
     label.setBounds(20,150, 200,50);  

     JLabel l1=new JLabel("Username:");    
        l1.setBounds(20,20, 80,30);    
        JButton b = new JButton("Login");  
        b.setBounds(100,120, 80,30);    
        final JTextField text = new JTextField();  
        text.setBounds(100,20, 100,30);    
               f.add(l1); f.add(label); f.add(b); f.add(text);  
                f.setSize(300,300);    
                f.setLayout(null);    
                f.setVisible(true);     
                b.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e) {       
                   String data = "Username " + text.getText();
                   label.setText(data);          
                }  
             });   
}  
}  
Vasanth
  • 53
  • 9

2 Answers2

4
System.getProperty("user.name");

This might help

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Nandha
  • 752
  • 1
  • 12
  • 37
1
final JTextField text = new JTextField(); 
text.setText(System.getProperty("user.name")); 
text.setBounds(100, 20, 100, 30);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vasanth
  • 53
  • 9
  • 1) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Nov 11 '17 at 07:36
  • .. 3) In this case, the size of a text field should be suggested by the number of columns of text it is meant to display, as well as the details of the font of the field (the larger the font, that larger the field. E.G. `JTextField text = new JTextField();` should be `JTextField text = new JTextField(12);` for a field meant to display 12 characters. Meanwhile the **location** of the field is best arranged logically (as opposed to hard coded values) using the layout, layout constraints and borders and margins applied to the field. – Andrew Thompson Nov 11 '17 at 07:39
  • @AndrewThompson.. thanks for your suggestion – Vasanth Nov 11 '17 at 07:49
  • @Vasanth Please mark your answer as accepted, since it solves the problem completely. You can find a tick mark below the down arrow button – Nandha Nov 11 '17 at 08:23
  • I cant find that tick mark under your answer – Vasanth Nov 11 '17 at 08:28
  • actually u made the answer in comment section i think so.. just check it tsk – Vasanth Nov 11 '17 at 08:30
  • while i m trying to click the tick and i m getting the reply as " u cant accept ur own answer for two days" – Vasanth Nov 11 '17 at 08:33