Monday, January 10, 2011

KeyListener in Java

If you want to write a code in Java which is based on keys being pressed on keyboard, then KeyListener is your way a head. This link at Oracle.com has a very nice description of the whole class and a very good example :

http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Friday, January 07, 2011

HOWTO : add/remove a String Item to jComboBox, Java

jComboBox :
// Add an item to the start of the list
cb.insertItemAt("item0", 0);

// Add an item after the first item
cb.insertItemAt("item0.5", 1);

// Add an item to the end of the list
cb.addItem("item3");

// Remove first item
cb.removeItemAt(0);

// Remove the last item
cb.removeItemAt(cb.getItemCount()-1);

// Remove all items
cb.removeAllItems();

HOWTO : get a list of available ports, Java

If you want to ahve a list over all ports, the code down here can help alot
protected void list() {
// get list of ports available on this particular computer,
// by calling static method in CommPortIdentifier.
Enumeration portList = CommPortIdentifier.getPortIdentifiers();

// Process the list.
while (portList.hasMoreElements()) {
CommPortIdentifier cpi = (CommPortIdentifier) portList.nextElement();
System.out.print("Port " + cpi.getName() + " ");
if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("is a Serial Port: " + cpi);
} else if (cpi.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
System.out.println("is a Parallel Port: " + cpi);
} else {
System.out.println("is an Unknown Port: " + cpi);
}
}
}

Source : java2s.com, modified as javax.comm does not exist anymore on Windows and RxTx should be used

Thursday, January 06, 2011

HOWTO : The code which lets you share a link on Facebook on your website

If you want to share the link of a current page on your website or a permanent link on Facebook, you just need to do the following :

Facebook has a very good documentation on it which you can read here.

</SCRIPT> <a name="fb_share" type="button"></a> <script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share"
type="text/javascript">
</script>

Result :

Wednesday, January 05, 2011

HOWTO : change the location of Desktop folder at Windwos 7

If like me you would also like to change the location of your Desktop folder(security reasons or willing to share the same Desktop on all of your computers), keep reading.


The solution is pretty simple :-) Simply go to following path : 
C:/users/name/Desktop
Then right click on the Desktop folder and then choose properties. After it click on "Location tab" and there you can change the location of the Desktop folder.


in XP, it is the same story but a little bit more complicated as you should go to registery(Start->Run->regedit) and change the value of this entry in registry : 


HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop

HOWTO : insert a code in blogspot weblogs


<"border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%>

Some code here

Monday, January 03, 2011

HOWTO : Add a program to Applications Menu

In Gnome(edgy) : System -> Preferences there is an item called "Main Menu". Here you to edit the application menu.

HOWTO : Find out if you have a 64 bit Ubuntu or a 32 bit one ?

Simply open a terminal and write the following command :

uname -m
x86_64 = 64 bit
i686 = 32 bit

Sunday, January 02, 2011

Setting limitation on uploading file, PHP

You can always set limitations on the uploading process as it is a big security risk to let users upload whatever they like ...
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
?>

More detailed types of files : 
"application/pdf" - PDF Files
"application/msword" - MS Word Files
"application/powerpoint" - MS Powerpoint Files
"application/excel" - MS Excel Files
"text/plain" - Text Files


Source : W3Schools

Uploading a file, PHP

With PHP you have the power to upload files to the server.


An example code(The client side) :
<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>


In the HTML code above :

  • The enctype attribute of the tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded
  • The type="file" attribute of the input tag specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field

****
An example for the "server side" script

The "upload_file.php" file contains the code for uploading a file:

<?php
 if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>



By using the global PHP $_FILES array you can upload files from a client computer to the remote server.

The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this:


  • $_FILES["file"]["name"] - the name of the uploaded file
  • $_FILES["file"]["type"] - the type of the uploaded file
  • $_FILES["file"]["size"] - the size in bytes of the uploaded file
  • $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
  • $_FILES["file"]["error"] - the error code resulting from the file upload

This is a very simple way of uploading files. You better add restrictions on what the user is allowed to upload to your sever. Down here you can also see a code which saves the uploaded file to a physical place on server. If you do not save the file, the temporary file will be deleted after the script ends. It also checks if the file already exist.


<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>



Source : W3Schools

Thursday, December 30, 2010

Set a selected item for select tag in HTML, web design

In this example Green will be the selected item(pay attention to where the selected statement is placed) :

colors:

<select name="colors">

<option>White</option>

<option>Red</option>

<option selected>Green</option>

<option>Blue</option>

</select>

Wednesday, December 29, 2010

Configuration about PHP server, HOWTO

If you are wondering about the configuration of the PHP server you have, you can simply make a PHP file with following command :


<?php





phpinfo();





?>


and simply place the file in the localhost folder and browse it in an Internet Browser. You would simply have all the information you need. The result will be something like the picture down here(Take from Wiki)

Start X from shell, Ubuntu

First method, Use init script
sudo /etc/init.d/gdm start
sudo /etc/init.d/kdm start

Second method, type startx command :
startx

If nothing works, type xinit to star X server without KDE:
xinit

Now start kde with following :
startkde

Tuesday, December 28, 2010

add a user to Ubuntu from Shell/terminal

useradd -m -s /bin/bash userName
passwd userName
cd /home
ls -l
mkdir scheiber ----> if it already does not exist
chmod 0700 scheiber
chown -R scheiber:scheiber scheiber

Switch between X login and shell, Ubuntu

Use the Ctrl-Alt-F1 shortcut keys to switch to the first console.

To switch back to Desktop mode, use the Ctrl-Alt-F7 shortcut keys.

Monday, December 27, 2010

How to Group your Radio Buttons

First add your Radio Buttons to the panel and then select all radio buttons you want to group and set their buttonGroup property (choose the group name from the combo box).

How to add tabbed panel to Java Frame in Netbeans

Select tabbed pane, right click on it and then go to Add From Palette, and selecting another Panel(not tabbed panel again) from there to add another tab.

For renaming the tab righ click on it and choose Edit Text.
To change the tabs order you can simply right click on the panel and click on Change Order and then re-order the tabs order as you wish.

Saturday, December 25, 2010

AVG detects Chrome as a Trojan threat

The AVG 8.5 seems to have problem. Either update to AVG 9.0 or 2011 free versions (this error is mainly being observed in AVG 8.5) OR revert back virus definition of Avg to previous state.
Revert update to previous version : 
Tools -> Advance setting -> Update 
and use the Manage to revert ! After that try installing it !

Tuesday, December 14, 2010

Live Writer, a Multi weblog solution

You got several websites and you want to publish something with not much differences in all of them. Then Live Writer is the right tool for you. It lets you add different weblogs(from Wordpress, blogger, etc) and update them with couple of clicks. There are other solutions that can publish one post(exactly the same thing) on several weblogs, but they lack some WYSIWYG feature or they simply do not work with some of your weblogs(Wordpress in my case). I can recommend this MS tool for bloggers

Download Live Writer from here.

List of people available in one IRC chat channel

  • List of people available in one IRC chat channel
    /who
  • Gives the real name of the given nickname/username
    /who nickname