Let's think you designed a Text field and a Button, When you write something on text filed, button will visible or if you clear the data on text field button will hide again.
First create 2 components (Text field and Button).
You have to choose the correct event to write your code. Button will visible when a key pressed, avoid ActionPerformed event (Action Performed will get both click and key pressed and etc.)
Think before code:
if (key pressed) Text field equals(=) empty
Button won't enable.
else (key pressed) Button will enable. (Text field is not empty)
Now let's convert it to code:
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
if (jTextField1.getText().equals("")){
jButton1.setEnabled(false);
}else{
jButton1.setEnabled(true);
}
}
First we need to get the text from the text field and check whether it's empty or not. Now you can enable the button or hide.
Happy coding guys!
Tuesday, 10 June 2014
Monday, 9 June 2014
Generate values form jList to jTextfield
First create a text field and list using your IDE.
Your have to select the proper event to code. Normally we select values from lists, so choose ListSelection - valueChanged event.
Convert list values to the String and get the string values from the jList.
String text = jList1.getSelectedValue().toString();
Now set above String values to the Text field.
jTextField1.setText(text);
code:
private void jList1(javax.swing.event.ListSelectionEvent evt) { String text = jList1.getSelectedValue().toString();
jTextField1.setText(text);
}
Enjoy coding!...
Your have to select the proper event to code. Normally we select values from lists, so choose ListSelection - valueChanged event.
Convert list values to the String and get the string values from the jList.
String text = jList1.getSelectedValue().toString();
Now set above String values to the Text field.
jTextField1.setText(text);
code:
private void jList1(javax.swing.event.ListSelectionEvent evt) { String text = jList1.getSelectedValue().toString();
jTextField1.setText(text);
}
Enjoy coding!...
How to center a jFrame on Screen - NetBeans
If you run your application it will display on upper-left corner on your screen(Default). You can set the center positions on source code, but here I'll show you the simple method to center it.
First go to the properties window of your jFrame.
Select Code view.
In code view Form size policy settings default value is Generate pack().
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0Q1YPJe-DJRoDSbzmcbQG1ku_EuMy8mVLsbhWaJc9vfr78KGjXdIMie7ZMXHKFM7iZJtRRk9ManA_dYFduBMve2abbem8EtHp73V6oIYrdPpTYeNBu51VgFzBI5ily9tY9lBZCNjPX_8/s1600/frame_properties_code.gif)
Select Generate Resize Code.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPDPyAcKtlXl4fWDb3UuhY5UI2PlIwJFeOxDWcb2X4tMoqAVL_UmSc0wNia_Njm5_EkH5yul5txQt0ivlotkvv7bDLlJ4mKHxia_7aF1R9Zpc5R-Nuj4fzJtjdMHvB5bvp8Rml1akCXVw/s1600/frame_properties_code_resize.gif)
Now bellow settings will be changed.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOOYfmHIv-2CuNQ_Kh4Ie6-e0Qu1Fe6FJXTEIwhIgAhyphenhyphenWNmKGLYwpA_0Xsjiss1rSSAnb96Hz6OcqgRoK4fNRNtrt_0vCtyk4UnQ6feJBjoQuu0d2kcbNswK3ylDPHYyy9uGJgqOcI-Ms/s1600/frame_properties_code_resize_tick.gif)
Tick the Generate Center option, it will unchecked the Generate Position option.
Close the properties window and run the application.
First go to the properties window of your jFrame.
Select Code view.
In code view Form size policy settings default value is Generate pack().
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0Q1YPJe-DJRoDSbzmcbQG1ku_EuMy8mVLsbhWaJc9vfr78KGjXdIMie7ZMXHKFM7iZJtRRk9ManA_dYFduBMve2abbem8EtHp73V6oIYrdPpTYeNBu51VgFzBI5ily9tY9lBZCNjPX_8/s1600/frame_properties_code.gif)
Select Generate Resize Code.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPDPyAcKtlXl4fWDb3UuhY5UI2PlIwJFeOxDWcb2X4tMoqAVL_UmSc0wNia_Njm5_EkH5yul5txQt0ivlotkvv7bDLlJ4mKHxia_7aF1R9Zpc5R-Nuj4fzJtjdMHvB5bvp8Rml1akCXVw/s1600/frame_properties_code_resize.gif)
Now bellow settings will be changed.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOOYfmHIv-2CuNQ_Kh4Ie6-e0Qu1Fe6FJXTEIwhIgAhyphenhyphenWNmKGLYwpA_0Xsjiss1rSSAnb96Hz6OcqgRoK4fNRNtrt_0vCtyk4UnQ6feJBjoQuu0d2kcbNswK3ylDPHYyy9uGJgqOcI-Ms/s1600/frame_properties_code_resize_tick.gif)
Tick the Generate Center option, it will unchecked the Generate Position option.
Close the properties window and run the application.
Enjoy coding!...
If you have any questions pls feel free to ask :)
If you have any questions pls feel free to ask :)
Sunday, 8 June 2014
Set Icon Image in Java application (Remove default Icon) - NetBeans
For demonstrating purpose I'll create a Project name BloggerEx and in that I have Blogger.java (jFrame)
First of all in your jFrame,
1. In Navigator bar select jFrame, double click it and go to properties or properties tab will show on right side of the NetBeans application.
(You have to select design view, otherwise navigator bar won't show.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRLGgGmU6L3urBgH2kt4po73Qm4H1kfqyrCj-mklCSlOOXKILzp0DQUbTKaaeUqrYwKv6PvNBe2vSpG8krQs92ojrIIzIp0nAvdhMPmzLNQw7_TQQ8rSNQZtN7sYHYcdZOHJFlcUHedeo/s1600/frame.gif)
If you run the application it will show the default java icon on the upper-left corner.
Now select iconImage property from the properties window.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP9yWEg8AvhF5r6PBf80ZNIeGdvGqNmyujDM1wZwdIbuCx_LI3_gGef_O7AEs8tBN62socP5SuxnrxTfVxbFk_7V8PHXJvRP7SVL-9-X1vGC4HdD0grt3Ymp8GSz0Tw0GlgjJ3RttIt6Y/s1600/frame_properties.gif)
2. Select the 'iconImage' property and it will show the separate window to change Image icon.
Select Custom code.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFkyKlQwjpz5HkUZGeCRGPrqEIc2GhftSHfRMisdbmNfLDG4rORd36AnxhG4VVMEZUzdHwKg983JFeABOYGrFjCadyLfDHzRv1jCrMgkNz4O-v3ModXIMw6k3Fs09fWmasXwEOfxcAxUo/s1600/frame_setIcon_pro.gif)
(If you don't have specific icon to set, choose now and copy that icon to project folder,
go to src folder in the project,
select the project name folder, (If you create project name 'My' this folder will be 'my')
Paste the icon in to the folder)
3. Now type the Code as below.
Note: when you typing the code it may not give the code suggestion sometimes
new javax.swing.ImageIcon(getClass().getResource("/bloggerex/cowboy.png")).getImage()
Note: ("/bloggerex/cowboy.png") = bloggerex is my project name and cowboy.png is the icon name.
You can change those with your project name and icon name.
4. Click ok and run the project or jFrame.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjACAwNj3yybnc1cWqmGYpzd8QjVlDb6IpLHfqH8aQTZh6EvWDm1For4m05bSyYs9qSlGhbgxc4gvLs12aVxHtpYGuBZaIG0nubZ03Hh3aP_Pnk3Msz21q1Tr4_SjyfXbNLPiyoPvEWAls/s1600/Finale.gif)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuJAxfyzHIESZptDhmkloNZ1ear7zm_oaDQqYhHk5wI3wlQ610tVRQamWgWB4v6lXcQWACiWp8zyhbfSxl4E66uReTB0Dwa6ezNUVjUH2rKUtpTKowsDx3Zv2OOkmYjg-2yPvz67OKwvQ/s1600/Cowboy.png)
Enjoy coding!...
If you have any questions pls feel free to ask :)
First of all in your jFrame,
1. In Navigator bar select jFrame, double click it and go to properties or properties tab will show on right side of the NetBeans application.
(You have to select design view, otherwise navigator bar won't show.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRLGgGmU6L3urBgH2kt4po73Qm4H1kfqyrCj-mklCSlOOXKILzp0DQUbTKaaeUqrYwKv6PvNBe2vSpG8krQs92ojrIIzIp0nAvdhMPmzLNQw7_TQQ8rSNQZtN7sYHYcdZOHJFlcUHedeo/s1600/frame.gif)
If you run the application it will show the default java icon on the upper-left corner.
Now select iconImage property from the properties window.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP9yWEg8AvhF5r6PBf80ZNIeGdvGqNmyujDM1wZwdIbuCx_LI3_gGef_O7AEs8tBN62socP5SuxnrxTfVxbFk_7V8PHXJvRP7SVL-9-X1vGC4HdD0grt3Ymp8GSz0Tw0GlgjJ3RttIt6Y/s1600/frame_properties.gif)
2. Select the 'iconImage' property and it will show the separate window to change Image icon.
Select Custom code.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFkyKlQwjpz5HkUZGeCRGPrqEIc2GhftSHfRMisdbmNfLDG4rORd36AnxhG4VVMEZUzdHwKg983JFeABOYGrFjCadyLfDHzRv1jCrMgkNz4O-v3ModXIMw6k3Fs09fWmasXwEOfxcAxUo/s1600/frame_setIcon_pro.gif)
(If you don't have specific icon to set, choose now and copy that icon to project folder,
go to src folder in the project,
select the project name folder, (If you create project name 'My' this folder will be 'my')
Paste the icon in to the folder)
3. Now type the Code as below.
Note: when you typing the code it may not give the code suggestion sometimes
new javax.swing.ImageIcon(getClass().getResource("/bloggerex/cowboy.png")).getImage()
Note: ("/bloggerex/cowboy.png") = bloggerex is my project name and cowboy.png is the icon name.
You can change those with your project name and icon name.
4. Click ok and run the project or jFrame.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjACAwNj3yybnc1cWqmGYpzd8QjVlDb6IpLHfqH8aQTZh6EvWDm1For4m05bSyYs9qSlGhbgxc4gvLs12aVxHtpYGuBZaIG0nubZ03Hh3aP_Pnk3Msz21q1Tr4_SjyfXbNLPiyoPvEWAls/s1600/Finale.gif)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuJAxfyzHIESZptDhmkloNZ1ear7zm_oaDQqYhHk5wI3wlQ610tVRQamWgWB4v6lXcQWACiWp8zyhbfSxl4E66uReTB0Dwa6ezNUVjUH2rKUtpTKowsDx3Zv2OOkmYjg-2yPvz67OKwvQ/s1600/Cowboy.png)
Enjoy coding!...
If you have any questions pls feel free to ask :)
Subscribe to:
Posts (Atom)