Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, March 6, 2016

How to "Select All" or Highlight textfield or password field in Java

There are cases when you want to select all or highlight the characters inside a textfield or password field in your Java desktop app. Here's a sample code to do that:

int i = textfield.getText().length();
textfield.setSelectionStart(0);
textfield.setSelectionEnd(i);


Put this in the action or wherever you want it. Replace textfield variable to the name of your textfield. I'm not sure but this might work for JTextArea and JTextPane too.

How to maximize frame or window in Java (JFrame)

There are multiple ways but I recommend these lines of code:

GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); Frame fr= new Frame(); fr.setMaximizedBounds(e.getMaximumWindowBounds()); fr.setExtendedState(main.getExtendedState() | JFrame.MAXIMIZED_BOTH); fr.setVisible(true); 

The code above will maximize your frame/window without hiding the taskbar (at the bottom part) of your screen except if you used undecorated frame. Any maximizing will hide your taskbar. This is according to my own experience but if you found ways to against it, please post it as a comment. Thanks :)