java etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
java etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

6 Aralık 2016 Salı

MacOs Friendly Intellij IDEA Shortcuts

Shortcuts help developers to be more productive and prevent distraction caused by mouse. Unfortunately, there are subtle shortcut differences between different platforms. Following Intellij IDEA shortcuts will increase productivity in MacOs environment:

VCS
Intellij IDEA does not provide preconfigured VCS shortcuts. To make it easy to remember, all shortcuts are assigned to ALT button and all shortcuts denote the first letter of assigned action (except Commit/Check In).

Alt+A                        Annotate
Alt+U                        Update
Alt+R                        Rollback
Alt+H                        History

Alt+C                        Commit/Check In
Alt+P                        Push

The reason why 'I' character is selected as shortcut of "Commit/Check In" is, because Intellij IDEA already used this character in "Commit Changes" dialog for "Commit/Check In" action .



Intellij IDEA already provides a shortcut for Push action which is SHIFT+COMMAND+K. This shortcut is not easy to remember. It is suggested to use ALT+P for Push action.

1 Haziran 2013 Cumartesi

How to Download Sun Jars in Maven Projects

Sun jars cannot be hosted in Maven central repository due to Sun's Binary License, e.g:

  • javax.sql:jdbc-stdex
  • javax.transaction:jta
  • javax.activation:activation

java.net provides a Maven repository to access Sun jars. It is a good practice to add following repository in your settings.xml file:
<repository>
      <id>maven2-repository.dev.java.net</id>
      <name>Java.net Repository for Maven</name>
      <url>http://download.java.net/maven/2/</url>
      <layout>default</layout>
</repository>

More details can be found at:
http://maven.apache.org/guides/mini/guide-coping-with-sun-jars.html

31 Mart 2009 Salı

Intellij Knows How To "Extract Method"

Intellij has a great refactoring feature called "Extract Method". You can convert a code piece into a method using this feature. In this process, Intellij can handle all method parameters and return values. You can use this feature first by selecting the code piece you wish to convert to method, and press CTRL+ALT+M.

I will explain what impressed me most about this feature using following example. In the following code, I am not comfortable with the first try/catch block and I want to hide this dirty code inside a method.
 1     public static void main(String[] args) {
2 Connection conn = null;
3
4
//I am not comfortable with following try/catch block
5 try {
6 Class.forName("com.mysql.jdbc.Driver");
7 } catch (ClassNotFoundException e) {
8 e.printStackTrace();
9 System.out.println("Driver not found");
10 return;
11 }
12
13
try {
14 conn = DriverManager.getConnection(
15 "jdbc:mysql://localhost/petshop", "root", "changeme");
16 Statement statement = conn.createStatement();
17 } catch (SQLException e) {
18 e.printStackTrace();
19 }
20 }
21
To achieve this, position the cursor over 5th line and press CTRL+W. By the way, this is another nice Intellij feature called "Incremental Select" :).



Then, press CTRL+ALT+M. Notice that, the return type of the method is boolean. Click OK and see what we'll get:


Following is the refactored version of our code. Let's see what happened. Intellij put the code inside a method which returns boolean and wrapped the method call inside an if statement. This was the only possible solution which wouldn't violate the flow of program:).

 1     public static void main(String[] args) {
2 Connection conn = null;
3
4
//I am comfortable now
5 if (lookupClass()) return;
6
7
try {
8 conn = DriverManager.getConnection(
9 "jdbc:mysql://localhost/petshop", "root", "changeme");
10 Statement statement = conn.createStatement();
11 } catch (SQLException e) {
12 e.printStackTrace();
13 }
14 }
15
16
private static boolean lookupClass() {
17 try {
18 Class.forName("com.mysql.jdbc.Driver");
19 } catch (ClassNotFoundException e) {
20 e.printStackTrace();
21 System.out.println("Driver not found");
22 return true;
23 }
24 return false;
25 }
26

25 Ocak 2009 Pazar

The Easiest Java Tab Implementation With Close Button

Unfortunately, the current implementation of JTabbedPane class in Swing API does not contain a close button. You have to implement this feature by yourself. Here, I have a simple JTabbedPane implementation with a close button. I used MetalIconFactory.InternalFrameCloseIcon class in the close button. Because this is a private inner class, you need to use MetalIconFactory.getInternalFrameCloseIcon(int iconSize). Check the following screenshots to see how a JTabbedPane and a CloseButtonTabbedPane looks like:

A tabbed pane with default JTabbedPane implementation, shows up as below:



A tabbed pane with CloseButtonTabbedPane implementation, shows up as below:





Click here to download the source code of CloseButtonTabbedPane here.

The advantages of this implementation is:
  • You don't need an image for close button. Instead you use an image provided by Java API.
  • Everything you need is packaged inside just 1 class.

4 Ağustos 2008 Pazartesi

How To Sort Collections Using commons-beanutils?

Hi everybody,
Following is another simple way of sorting collections using commons-beanutils package:
Assume that we have an object as below:

public class User {
private long userId;
private String userName;
private String password;
}

And we want to sort based on userName field. We can use BeanComparator class of commons-beanutils package as below to sort the list:

ArrayList list = new ArrayList();
//...
//add User object inside the list.
//...
Collections.sort(list, new BeanComparator("userName"));

Notice that, we passed the name of the field to be sorted, as the parameter, to BeanComparator class constructor. We accomplished sorting of a collection using just one line of code:).

Download commons-beanutils from http://commons.apache.org/beanutils.

5 Nisan 2008 Cumartesi

Why Doesn't PrintWriter Work?

Even though you may call method PrintWriter.println(), you may not get any output. This is because PrintWriter is auto-flush off by default. Check the code below:

 1 public class Test {
2 public static void main(String[] args) {
3 OutputStream out1 = System.out;
4 PrintWriter out = new PrintWriter(out1);
5 out.println("hello");
6 System.out.println("text is about to be flushed.");
7 out.flush();
8 }
9 }

The output is as follows:
text is about to be flushed.
hello

To enable auto-flush, you must initialize PrintWriter as follows:
PrintWriter out = new PrintWriter(out1, true);

26 Mart 2008 Çarşamba

Java Tabanlı Oyun Motorları

Bu aralar bol bol boş vaktim var. Ben de, çok merak edip de şimdiye kadar ilgilenemediğim, değişik değişik konularla zamanımı değerlendiriyorum. Şu anda java tabanlı açık kaynak kodlu oyun motorlarını araştırıyorum. Bir yandan da OpenGL ve DirectX'i araştırıyorum. Google'da "java game engine" araması sonucu bulduğum bazı sonuçları aşağıda yayınlıyorum.

Java Monkey Engine:
http://www.jmonkeyengine.com/

EasyWay:
http://easyway.sourceforge.net/joomla/index.php
Adı üstünde kullanımı ve öğrenimi kolay bir oyun motoru. Şimdilik sadece 2 boyutlu oyunlar yapılabiliyor. 3 boyut desteği yok. Tam ekran ve pencere desteği var. Applet desteği sıradaki versiyonla beraber geliyor. Bir an önce oyun programlamaya başlamak istiyorum diyorsanız, seçiminiz bu olsun. Sitedeki tutorial'leri okuduktan sonra kendi oyununuzu yazmaya başlayabilirsiniz.

OctLight:
https://jge.dev.java.net/
OctLight öncelikle online oyun geliştirilmesi amacıyla tasarlanmış. Fakat normal oyunlar da geliştirilebiliyor. Proje'nin şimdilik çok eksiği var. Yaklaşık bir senedir güncelleme görmemiş.

Golden T Game Engine:
http://goldenstudios.or.id/products/GTGE/
GTGE ile de 2 boyutlu oyunlar hazırlanabiliyor. Tam ekran, pencere ve applet desteği var. Sitesinde onlarca örnek oyunu kaynak koduyla beraber indirebilirsiniz.
Bu proje, EasyWay'den daha aktif bir proje. Sadece forumlarında bile, EasyWay'den çok daha fazla post mevcut.

JGame:
http://www.13thmonkey.org/~boris/jgame/

Şimdilik hangisi daha kullanışlı ben de bilmiyorum. Zaman içinde bu gönderiyi güncelleyeceğim.

23 Mart 2008 Pazar

Online JavaOne Seminerleri

Aşağıdaki adresten, şimdiye kadar yapılmış olan JavaOne seminerlerini izleyebilir ve sunum dosyalarını indirebilirsiniz.

http://developers.sun.com/learning/javaoneonline/index.jsp

Sitede, aklınıza gelebilecek her türlü konuda görsel, işitsel içerik mevcut:).

20 Mart 2008 Perşembe

ServletContext ile ServletConfig Arasındaki Fark

ServletContext, uygulamaya ait parametrelere erişmek için kullanılır. 1 uygulamada, 1 ServletContext objesi bulunur. Bu parametreler, web.xml dosyasında, aşağıdaki gibi saklanır:
<context-param>
<param-name>logfile_path</param-name>
<param-value>\logs\log.txt</param-value>
</context-param>


ServletConfig, ait olduğu servlet'e ait parametreleri barındırır. Uygulama içindeki her servlet'in 1 ServletConfig objesi vardır. Bu parametreler, web.xml'de aşağıdaki gibi saklanır.
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
<init-param>
<param-name>About the Servlet</param-name>
<param-value>This is my servlet.</param-value>
</init-param>
</servlet>