Tuesday, 30 August 2016

JavaFX: List View

JavaFx:How to create a List View in Java Fx?

For generating List View you should have a structure as provided below in JavaFx project.
We have created this List View to display the selected value in console. You can modify code according to your requirement.

Main.java

package application_listview;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root=FXMLLoader.load(getClass().getResource("/application_listview/Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

MainController.java

package application_listview;


import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;

public class MainController implements Initializable  {
@FXML
public ListView<String> listview;
ObservableList<String> list=FXCollections.observableArrayList("one","two","three","four");
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
listview.setItems(list);
listview.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
}
public void buttonAction(ActionEvent event){
ObservableList<String> names;
names=listview.getSelectionModel().getSelectedItems();
for(String name :names){
System.out.println(name);
}
}

}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application_listview.MainController">
   <children>
      <ListView fx:id="listview" layoutX="50.0" layoutY="72.0" prefHeight="200.0" prefWidth="200.0" />
      <Button layoutX="124.0" layoutY="25.0" mnemonicParsing="false" onAction="#buttonAction" text="Button" />
   </children>
</AnchorPane>

Output


Reference & More

Monday, 29 August 2016

JavaFX: Combo box

JavaFx:How to create a combo box in Java Fx?


For generating combo box you should have a structure as provided below in JavaFx project.



We have created this combo box to display the selected value in a label. You can modify code according to your requirement.

Main.java

package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root=FXMLLoader.load(getClass().getResource("/application/Main.fxml")); 
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

MainController.java

package application;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;

public class MainController implements Initializable{
@FXML
public Label mylabel;
@FXML
public ComboBox<String> combobox;
ObservableList<String> list=FXCollections.observableArrayList("first","second","third");
@Override
public void initialize(URL location, ResourceBundle resources) {
combobox.setItems(list);
}
public void combochanged(){
mylabel.setText(combobox.getValue());
}
}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <ComboBox fx:id="combobox" layoutX="50.0" layoutY="101.0" onAction="#combochanged" prefHeight="49.0" prefWidth="194.0" promptText="select Name" />
      <Label fx:id="mylabel" layoutX="53.0" layoutY="178.0" prefHeight="34.0" prefWidth="187.0" text="Label" />
   </children>
</AnchorPane>

Output

You will get an output display as provided below.

Reference & More



Tuesday, 23 August 2016

Java:-Find particular substring from a .txt file.

Java:-The below mentioned code enables user to find a particular length of a string whenever required from a file(.txt file or something like that).

Example:-I have a requirement to extract 10 digit card number which was written like CardNumber:="'1234567890".

Note:-Put your sample txt file in workspace or modify file reader argument accordingly.

Code:-

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class PatternMatch {

 public static void main(String[] args) {
  try {
   BufferedReader br= new BufferedReader(new FileReader("C:\\workspace\\newfile.txt"));
   String line;
   int lastindex=0;
   try {
    while((line=br.readLine())!=null){
    while(lastindex!=-1){
   lastindex = line.indexOf("CardNumber",lastindex);
    if(lastindex!=-1){
    String substr=line.substring(lastindex+14,lastindex+14+10);
     System.out.println(substr);
    lastindex=lastindex+14+10;
     }
     }
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }

}

Monday, 22 August 2016

Java:-Read from text file using buffered reader.

Java:-Read from a text file in Java

This code enables you to read from a .txt text file format."C:\\workspace\\newfile.txt" is a sample workspace . Replace path with required path.


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {

public static void main(String[] args) {
try {
BufferedReader br= new BufferedReader(new
FileReader("C:\\workspace\\newfile.txt"));
String line;
try {
while((line=br.readLine())!=null){
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}