It's all about Java

Friday, 15 May 2015

JavaFX: arranging components on GridPane

JavaFX provides different layout panes for arranging components on them. for example GridPane, BorderPane etc.
This article describes about arranging components on GridPane. GridPane layout manager allows arranging components/controls using overloaded method. ie add.

                       1. add(controlinstance, colindex, rowindex)

                      EX:  GridPane pane = new GridPane();
                               pane.add(new Separator(), 0, 0);

the above example code puts the separator on 0th row and 0th column of a gridPane.

                     2. add(Node controlinstance,int colIndex,int rowIndex,int colSpan,int rowSpan)   
                          
                     EX: GridPane pane = new GridPane();
                             pane.add(new Separator(),0 , 0, 2, 0);

Following code describes the GridPane with a login screen.


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import com.atmecs.utilities.Validations;


public class Main extends Application {


Button buttonLogin;
Button buttonCancel;
TextField textFieldUsername;
PasswordField PasswordFieldPassword;


@Override
public void start(Stage primaryStage) {

initComponents(primaryStage);

}

private void initComponents(Stage primaryStage) {
        
GridPane gridPane = new GridPane();

gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.setPadding(new Insets(25, 25, 25, 25));

Scene scene = new Scene(gridPane, 320, 230);
primaryStage.setScene(scene);

Text title = new Text("Login");
title.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
gridPane.add(title, 0, 0, 2, 1);

Label labelUsername = new Label("Username:");
gridPane.add(labelUsername, 0, 1);
textFieldUsername = new TextField();
gridPane.add(textFieldUsername, 1, 1);

Label labelPassword = new Label("Password:");
gridPane.add(labelPassword, 0, 2);
PasswordFieldPassword = new PasswordField();
gridPane.add(PasswordFieldPassword, 1, 2);

buttonLogin = new Button("Login");


buttonCancel = new Button("Cancel");

gridPane.add(new Separator(), 0, 4, 2, 1);
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(buttonLogin);
hbBtn.getChildren().add(buttonCancel);

gridPane.add(hbBtn, 1, 5);

primaryStage.show();
setListeners();
}
        public static void main(String[] args) {
launch(args);
}

}





Output:

Wednesday, 6 May 2015

Send SMSs with Java

Through this article I want to share some knowledge that I have on sending bulk SMSs through java and .NET applications.
SMSLIB is an open source API which allows the programmers to write java or .NET code to send multiple smss to different phone numbers.
It requires Mobile phone or GSM MODEM along with any SIM.

Please go through this link to get the SMSLIB API.
                                                  http://smslib.org/download/
 

Saturday, 23 July 2011

JForm: Fill the form with style

After understanding and using swing toolkit, I understood that there is a allot of scope to develop Custom components easily.
I have developed a component called JForm which basically a button and its selection pops up a window if it is not visible.

Technically the JForm component is a combination of 2 components they are:

1. JButton
2. Window

JButton is a API of swing toolkit where as Window is a API of AWT.

Behaviour of JForm:

JForm is a usual button like JButton. but it is glued to a Window component. this Window takes a JPanel class as an arguments.

In the given example has 2 components they are 1. JTextArea  2. JButton [Submit] which are appended to a JPanel..

The below image shows the effect before selecting the JForm:






This image shown is the effect after selecting the JForm:

                                     



the below code is an example which includes code for JForm custom component and main() includes usage of JForm.


import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class JForm extends JButton {

Point location;
int X = 0;
int Y = 0;
int W = 0;
int H = 0;
JFrame frame;
MyWindow window;

public JForm(JFrame frame, JPanel panel) {

this.frame = frame;
this.setBackground(Color.gray);
this.setText("click me!...");
JForm.this.window = new MyWindow();
JForm.this.window.add(panel);
this.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

if (window.isVisible() == false) {
JForm.this.window.setSize(10, 20);
location = getLocationOnScreen();
X = location.x;
Y = location.y;
JForm.this.window.setLocation(X - 5, Y + JForm.this.getHeight());
window.pack();
window.setVisible(true);
}else{
window.setVisible(false);
}
}
});
}

class MyWindow extends Window {
public MyWindow() {
super(JForm.this.frame);
frame.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent evt) {
if (JForm.this.isShowing() == true) {
location = JForm.this.getLocationOnScreen();
X = location.x;
Y = location.y;
JForm.this.window.setLocation(X - 5, Y + JForm.this.getHeight());
}
}

@Override
public void componentHidden(ComponentEvent arg0) {
if (JForm.this.isShowing() == true) {
location = JForm.this.getLocationOnScreen();
X = location.x;
Y = location.y;
JForm.this.window.setLocation(X - 5, Y + JForm.this.getHeight());
}
}

@Override
public void componentMoved(ComponentEvent arg0) {
if (JForm.this.isShowing() == true) {
location = JForm.this.getLocationOnScreen();
X = location.x;
Y = location.y;
JForm.this.window.setLocation(X - 5, Y + JForm.this.getHeight());
}
}

@Override
public void componentShown(ComponentEvent arg0) {
if (JForm.this.isShowing() == true) {
location = JForm.this.getLocationOnScreen();
X = location.x;
Y = location.y;
JForm.this.window.setLocation(X - 5, Y + JForm.this.getHeight());
}
}
});
}

}

public static void main(String[] args) {
JFrame frame = new JFrame();

GridBagLayout l = new GridBagLayout();

GridBagConstraints c = new GridBagConstraints();
c.gridwidth = 40;
c.gridheight = 20;
frame.setLayout(l);
JPanel panel = new JPanel();
GridBagLayout pl = new GridBagLayout();
GridBagConstraints pC = new GridBagConstraints();
JTextArea ta = new JTextArea(5, 10);
panel.add(ta, pC);
JButton button = new JButton("Submit");
pC.gridx = 50;
pC.gridy = 40;
panel.add(button, pC);

panel.setBackground(Color.gray);
JForm form = new JForm(frame, panel);
l.setConstraints(form, c);
frame.getContentPane().add(form, c);
frame.setSize(200, 200);
frame.pack();
frame.setVisible(true);
}
}

Next time I will come up with a JForm with different shapes like rounded, rounded corners etc..  

Popular posts

CHEAT SHEET for java List interface, LinkedList and ArrayList

Consider Below points before choosing List concrete implementations for your use case:  ArrayList is a linear data structure and  re-s...