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

Monday, May 02, 2016

copy a text to clipboard in Java

Easily done via Java Toolkit

StringSelection selection = new StringSelection(theString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);

commenting a line in Netbeans

commenting a line in Netbeans ? Try this

Ctrl + Shift + C

Source : https://netbeans.org/project_downloads/usersguide/shortcuts-80.pdf

Tuesday, April 19, 2011

start jFrame in the center of the screen


// Get the size of the screen
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    
    // Determine the new location of the window
    int w = window.getSize().width;
    int h = window.getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;
    
    // Move the window
    window.setLocation(x, y);

Monday, April 18, 2011

Integrating power of Java & Processing :-)

In the following link you can find how to import processing libraries to a Java class where you can combine the power of Java with the flexibility of Processing and make miracles ;-)

http://processing.org/learning/eclipse/

Source : Processing.org

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

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.

Tuesday, October 19, 2010

Python for Java Programmers

If you are a Java programmer and need to learn python, try this link. It is pretty useful.

Thursday, April 08, 2010

Foreach statement C# & Java

Java : 


double[] ar = {1.2, 3.0, 0.8};
int sum = 0;
for (double d : ar) {  // d gets successively each value in ar.
    sum += d;
}



C# :
using System;
class ForEachLoop
{
    public static void Main()
    {
        string[] names = {"Cheryl", "Joe", "Matt", "Robert"};
        foreach
 (string person in names)
        {
            Console.WriteLine("{0} ", person);
        }
    }
}

Monday, November 02, 2009

instanceof example in Java

public class MainClass {
public static void main(String[] a) {

String s = "Hello";
if (s instanceof java.lang.String) {
System.out.println("is a String");
}
}

}

Tuesday, September 29, 2009

Special characters, Java ...

If you are wondering how to check special characters in Java with a char variable,you can look @ the meaningless program down here.This program just shows you how to compare special characters with a char variable.
/********************************************
*********************************************
********** Special Char check ***************
*********************************************
********************************************/
public void checkSpecialChar(String aText){
Char character = aText.charAt(0);

if (character == '<') {
System.out.println(aText);
}
else if (character == '>') {
System.out.println(aText);
}
else if (character == '&') {
System.out.println(aText);
}
else if (character == '\"') {
System.out.println(aText);
}
else if (character == '\t') {
System.out.println(aText);
}
else if (character == '!') {
System.out.println(aText);
}
else if (character == '#') {
System.out.println(aText);
}
else if (character == '$') {
System.out.println(aText);
}
else if (character == '%') {
System.out.println(aText);
}
else if (character == '\'') {
System.out.println(aText);
}
else if (character == '(') {
System.out.println(aText);
}
else if (character == ')') {
System.out.println(aText);
}
else if (character == '*') {
System.out.println(aText);
}
else if (character == '+') {
System.out.println(aText);
}
else if (character == ',') {
System.out.println(aText);
}
else if (character == '-') {
System.out.println(aText);
}
else if (character == '.') {
System.out.println(aText);
}
else if (character == '/') {
System.out.println(aText);
}
else if (character == ':') {
System.out.println(aText);
}
else if (character == ';') {
System.out.println(aText);
}
else if (character == '=') {
System.out.println(aText);
}
else if (character == '?') {
System.out.println(aText);
}
else if (character == '@') {
System.out.println(aText);
}
else if (character == '[') {
System.out.println(aText);
}
else if (character == '\\') {
System.out.println(aText);
}
else if (character == ']') {
System.out.println(aText);
}
else if (character == '^') {
System.out.println(aText);
}
else if (character == '_') {
System.out.println(aText);
}
else if (character == '`') {
System.out.println(aText);
}
else if (character == '{') {
System.out.println(aText);
}
else if (character == '|') {
System.out.println(aText);
}
else if (character == '}') {
System.out.println(aText);
}
else if (character == '~') {
System.out.println(aText);
}
else {
//the char is not a special one
//add it to the result as is
System.out.println(aText);
}
}//end of checkSpecialChar(String )
/********************************************
*********************************************
******* End of Special Char check **********
*********************************************
********************************************/
Edited from this source

Monday, July 27, 2009

Remove whitespaces or whatever letter from a string in Java

String str = "Helloo , Welcome to the land of FRACK !!!".replaceAll(" ", "!"));

str will be : Helloo!,!Welcome!to!the!land!of!FRACK!!!!

Wednesday, April 08, 2009

a beautiful implementation of iterator for binary search tree

This is a beautiful implementation of an iterator for a Binary Search Tree.

private class treeIterator implements Iterator {
private Stack pose; // Java stack

//Constructor
private treeIterator() {
// Declare the stack in the constructor for each copy of our iterator
pose = new Stack();
slideLeft(root);
}

/**
* Traverse the Binary Search tree and place all the elements in the right order in the tree
* @param node
*/
private void slideLeft(Node node) {
while (node != null) {
pose.push(node);
node = node.left;
}
}

/**
* Returns true if the stack has elements and False if it is empty
*/
public boolean hasNext() {
return !pose.isEmpty();
}

/**
* Here we use peep to get the element from the stack and pop to remove it
* and in this way we go through the whole BST IF we need it !!!
*/
public E next() {
if (pose.isEmpty())
throw new NoSuchElementException(
"No more elements left in the iterator.");
E element = pose.peek().element;
slideLeft(pose.pop().right);
return element;
}

public void remove() {
throw new UnsupportedOperationException("n");
}
}//end of Iterator

Tuesday, April 07, 2009

make a frame NOT resizable in JAVA ...

This program illustrates you how to make a frame non resizable. It means, disabling the maximize button of the frame.

The setResizable() method has been used to make the frame resizable or not. If you pass the boolean value false to the setResizable() method then the frame will be non-resizable otherwise frame will be resizable. The setResizable() is the method of the JFrame class which takes a boolean valued argument (true or false).

Screen shot for the result of the program:

Non Resizable Frame.

Here is the code of the program :

import javax.swing.*;

public class SwingFrameNonResizable{
public static void main(String[] args){
JFrame frame = new JFrame("Non Resizable Frame");
frame.setResizable(false);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Saturday, March 21, 2009

Shahabinary alpha version 0.002

Hi.

Shahabinary English to Norwegian dictionary :

Shahabinary is a OS free, freeware PC application which is at least runable on Linux & Windows (TESTED). The code is not open source this time (like Shahabinary 0.001) but I will post the source code in near future. Due to some technical difficulties with GUI, the word recognition ability has been disabled but will be functional again in version 0.003. This version enjoys a better GUI and much more comfortable design with significant changes in the code which makes it smaller and easier to compile. The only thing you need to do for running is to unzip the folder and double click on Shahabinary v. 0.0002.jar.

Again I repeat the words and database are not trustable and this is just an exercise in my learning progress. This project the focus is mostly on the technical part and not the dictionary parts of the application.

Click here
to download the latest Shahabinary …

Enjoy …

Thursday, February 19, 2009

Testing EOF of DataInputStream JAVA

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class TestEOF {
  // Throw exceptions to console:
  public static void main(String[] argsthrows IOException {
    DataInputStream in = new DataInputStream(new BufferedInputStream(
        new FileInputStream("TestEOF.java")));
    while (in.available() != 0)
      System.out.print((charin.readByte());
  }
///:~



Source

Tuesday, January 20, 2009

Shahabinary Alpha 0.002

Hi.

Shahabinary English to Norwegian dictionary :

Shahabinary is a OS free, freeware PC application which is at least runable on Linux & Windows (TESTED). The code is not open source this time (like Shahabinary 0.001) but I will post the source code in near future. Due to some technical difficulties with GUI, the word recognition ability has been disabled but will be functional again in version 0.003. This version enjoys a better GUI and much more comfortable design with significant changes in the code which makes it smaller and easier to compile. The only thing you need to do for running is to unzip the folder and double click on Shahabinary v. 0.0002.jar.

Again I repeat the words and database are not trustable and this is just an exercise in my learning progress. This project the focus is mostly on the technical part and not the dictionary parts of the application.

Click here
to download the latest Shahabinary ...

Enjoy ...