I'm asking for your help.
I'm developing an application in JavaFX who "scan" Mp3 files to get ID3tag.
Here is my problem. I did a foreach loop of a list for every .mp3 found but I'd like to increment a label which inform the progression of the list.
Here is my code
private ArrayList checkMp3File(ArrayList<String> lsMp3file, String sDir) throws UnsupportedTagException, InvalidDataException, IOException
{
this.currentData = 1;
int size = lsMp3file.size();
ArrayList<DataSong> lsds = new ArrayList<>();
for(String mp3file : lsMp3file)
{
this.labelUpdate.setText(this.current++ + " of " + " size");
DataSong ds = new DataSong();
Mp3File mp3 = new Mp3File(mp3file);
ds.setLenghtOfMp3inSec(mp3.getLengthInSeconds());
ds.setBitRateOfMp3(mp3.getBitrate());
ds.setSampleRate(mp3.getSampleRate());
ds.setVbrOrCbr(mp3.isVbr());
}
Actually, when the loop progress my window interface is completely freeze.
And only when the loop is finished, the label updated.
Someone can explain why ?
I already thank you for your answers.
EDIT :
Here is my fully code
public class LaunchOption extends Pane {
private final HBox launchAndSend = new HBox();
private final HBox browseAndField = new HBox();
private final HBox jsonAndAdvance = new HBox();
private ArrayList<DataSong> lsWithData = new ArrayList<>();
private String sendJson;
private File selectedDirectory;
private User user;
private int currentData;
private final ProgressIndicator pi = new ProgressIndicator(0);
private final VBox containerElement = new VBox();
private final TextArea displayJson = new TextArea();
private final TextField pathDir = new TextField();
private final TextField nbrOfData = new TextField();
private final Button btnScan = new Button();
private final Button btnSend = new Button();
private final Button btnCheckJson = new Button();
private final Button btnDirectoryBrowser = new Button();
private final Label nbMp3 = new Label();
public Label listAdvance = new Label();
private final Stage home;
public LaunchOption(Stage home){
this.home = home;
configureBtnCheckJson();
configureBtnScan();
configureBtnSend();
configureLabelMp3();
configureBtnDirectoryBrowser();
configureTextAreaDisplayJson();
configureTextFieldPathDir();
configureTextFieldNbDataMp3();
configureHBoxlaunchSend();
configureHBoxBrowseAndField();
configureHBoxJsonAndAdvance();
configureContainer();
this.getChildren().addAll(containerElement,launchAndSend);
}
private void configureLabelMp3()
{
nbMp3.setText("MP3");
}
private void configureBtnScan(){
btnScan.setText("Scan");
btnScan.setOnAction(event->{
ArrayList<String> Mp3FileData;
Mp3FileData = mapFilesMp3(selectedDirectory.getAbsolutePath());
System.out.println("ListSize = " + Mp3FileData.size());
nbrOfData.setText(String.valueOf(Mp3FileData.size()));
try {
lsWithData = checkMp3File(Mp3FileData, selectedDirectory.getAbsolutePath());
} catch (UnsupportedTagException ex) {
Logger.getLogger(MusiScanMp3agic.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidDataException ex) {
Logger.getLogger(MusiScanMp3agic.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MusiScanMp3agic.class.getName()).log(Level.SEVERE, null, ex);
}
pi.setProgress(1);
});
}
private void configureBtnDirectoryBrowser(){
btnDirectoryBrowser.setText("Browse ...");
btnDirectoryBrowser.getStyleClass().add("round-red");
btnDirectoryBrowser.setOnAction(event-> {
DirectoryChooser dc = new DirectoryChooser();
selectedDirectory = dc.showDialog(home);
pi.setProgress(0.35);
if(selectedDirectory == null)
{
pathDir.setText("No directory selected");
}
else
{
pathDir.setText(selectedDirectory.getAbsolutePath());
String Text = pathDir.getText();
System.out.println(Text.toString());
}
});
}
private static String regexMp3()
{
return "^.*\\.(mp3)$";
}
private ArrayList mapFilesMp3(String sDir){
ArrayList<String> ls = new ArrayList<>();
printFnames(sDir,ls);
return ls;
}
private static void printFnames(String sDir, ArrayList<String> ls)
{
File[] faFiles = new File(sDir).listFiles();
for(File file : faFiles)
{
if(file.getName().matches(regexMp3()))
{
// System.out.println(file.getAbsolutePath());
ls.add(file.getAbsolutePath());
}
if(file.isDirectory())
{
printFnames(file.getAbsolutePath(), ls);
}
}
}
private ArrayList checkMp3File(ArrayList<String> lsMp3file, String sDir) throws UnsupportedTagException, InvalidDataException, IOException
{
this.currentData = 1;
int size = lsMp3file.size();
ArrayList<DataSong> lsds = new ArrayList<>();
for(String mp3file : lsMp3file)
{
System.out.println(this.currentData++);
DataSong ds = new DataSong();
Mp3File mp3 = new Mp3File(mp3file);
ds.setLenghtOfMp3inSec(mp3.getLengthInSeconds());
ds.setBitRateOfMp3(mp3.getBitrate());
ds.setSampleRate(mp3.getSampleRate());
ds.setVbrOrCbr(mp3.isVbr());
if(mp3 != null){
ds.setAbsoluteLocation(mp3.getFilename());
ds.setLocation(removeSDir(mp3.getFilename(), sDir));
if(mp3.hasId3v2Tag())
{
ID3v2 id3v2Tag = mp3.getId3v2Tag();
if(!(id3v2Tag.getArtist() == null))
{
ds.setArtist(id3v2Tag.getAlbumArtist());
}
if(!(id3v2Tag.getAlbum() == null))
{
ds.setAlbum((id3v2Tag.getAlbum()));
}
if(!(id3v2Tag.getTitle() == null))
{
ds.setTitle(id3v2Tag.getTitle());
}
if(!(id3v2Tag.getTrack() == null))
{
ds.setTrackOnAlbum(id3v2Tag.getTrack());
}
if(!(id3v2Tag.getYear() == null) && !(id3v2Tag.getYear().isEmpty()))
{
ds.setYearReleased(id3v2Tag.getYear());
}
if(!(id3v2Tag.getGenreDescription() == null))
{
ds.setGenre(id3v2Tag.getGenreDescription());
}
if(!(id3v2Tag.getComposer() == null))
{
ds.setComposer(id3v2Tag.getComposer());
}
if(!(id3v2Tag.getPublisher() == null))
{
ds.setPublisher(id3v2Tag.getPublisher());
}
if(!(id3v2Tag.getOriginalArtist() == null))
{
ds.setOriginArtist(id3v2Tag.getOriginalArtist());
}
if(!(id3v2Tag.getAlbumArtist() == null))
{
ds.setAlbumArtString(id3v2Tag.getAlbumArtist());
}
if(!(id3v2Tag.getCopyright() == null))
{
ds.setCopyright(id3v2Tag.getCopyright());
}
if(!(id3v2Tag.getUrl() == null))
{
ds.setUrl(id3v2Tag.getUrl());
}
}
}
lsds.add(ds);
}
return lsds;
}
I presume that what I should do is to make my checkMp3File method into a Task method which will do a background thread ?
There is not enough code to be sure but I think you are probably calling your method on the JavaFX application thread which then blocks your UI.
You should read the documentation about concurrency in JavaFX.
https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm
As a seminar, I was asked to implement a phone book on a file and use a design pattern. I chose the observer. The implementation is a client-server. I created a server class that manages the file. The client communicate to the server, and the client thread registered to the observer. My goal is to whenever there is an update of the file (deletion, adding etc.) all the clients's GUI refresh.
I would like for help.
The server class:
public class Server extends Application implements Observer {
//implements Observer
private TextArea ta = new TextArea();
private int clientNo = 0;
private int flag = 0;
private int lockId;
private RandomAccessFile raf;
#SuppressWarnings("rawtypes")
private ArrayList<HandleAClient> clients;
private HandleAClient clientThread;
//Specify the size of five string fields in the record
final static int NAME_SIZE = 32;
final static int STREET_SIZE = 32;
final static int CITY_SIZE = 20;
final static int STATE_SIZE = 2;
final static int ZIP_SIZE = 5;
final static int RECORD_SIZE = (NAME_SIZE + STREET_SIZE
+ CITY_SIZE + STATE_SIZE + ZIP_SIZE);
private int[] fromToSortChoices =
{ 0, NAME_SIZE, NAME_SIZE + STREET_SIZE ,
NAME_SIZE + STREET_SIZE + CITY_SIZE ,
NAME_SIZE + STREET_SIZE + CITY_SIZE + STATE_SIZE,
NAME_SIZE + STREET_SIZE + CITY_SIZE + STATE_SIZE + ZIP_SIZE
};
private Socket socket;
private ServerSocket serverSocket;
final static String fileName0 = "address0";
public static void main(String[] args)
{ launch(args);
}
#SuppressWarnings({ "rawtypes", "unchecked" })
public void start(Stage primaryStage){
// Open or create a random access file
try
{
raf = new RandomAccessFile(fileName0 + ".dat", "rw");
raf.setLength(0);
this.clients = new ArrayList<Server.HandleAClient>();
}
catch (IOException e1)
{ System.out.print("Error: " + e1);
System.exit(0);
}
// Create a scene and place it in the stage
Scene scene = new Scene(new ScrollPane(ta), 450, 200);
primaryStage.setTitle("Server"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
primaryStage.setAlwaysOnTop(true);
primaryStage.setOnCloseRequest(e->{
try{
socket.close();
serverSocket.close();
Platform.exit();
System.exit(0);
}
catch(Exception e1){
}});
new Thread( () ->{
try {
serverSocket = new ServerSocket(8000);
Platform.runLater(() ->
ta.appendText("Server started at " + new Date() + '\n'));
while(true){
socket = serverSocket.accept();
clientNo++;
Platform.runLater(() ->
ta.appendText("Starting thread for client " + clientNo + " at " + new Date() + '\n'));
InetAddress clientInetAddress = socket.getInetAddress();
Platform.runLater(() ->{
ta.appendText("Client " + clientNo + "'s host name is " + clientInetAddress.getHostName() + "\n");
ta.appendText("Client " + clientNo + "'s IP Address is " + clientInetAddress.getHostAddress() + "\n");
});
clientThread =
new HandleAClient(socket,clientNo);
Thread t = new Thread(clientThread);
clientThread.addObserver(Server.this);
clients.add(clientThread);
t.start();
}
}
catch(IOException ex) {
System.err.println(ex);
}
}).start();
}
class HandleAClient extends Observable implements Runnable {
//extends Observable
private Socket connectToClient;
private int id;
private long currentPosition = 0;
DataInputStream FromClient;
DataOutputStream ToClient;
public HandleAClient(Socket socket,int id) {
this.connectToClient = socket;
this.id = id;
}
public void run() {
try {
// Create data input and output streams
FromClient =
new DataInputStream(connectToClient.getInputStream());
ToClient =
new DataOutputStream(connectToClient.getOutputStream());
currentPosition = raf.getFilePointer();
ToClient.writeLong(currentPosition);
long fromPosition = raf.getFilePointer() - 2 * RECORD_SIZE;
long numberOfRecordsToCopy =
(raf.length()- fromPosition) / (2 * RECORD_SIZE);
long numberOfRecords;
while (true) {
System.out.println("server wait");
// synchronized (Thread.class) {
ToClient.writeLong(raf.length());
ToClient.flush();
String type = FromClient.readUTF();
switch (type) {
case ("Clear"):
{
raf.setLength(0);
setChanged();
notifyObservers();
}
break;
case ("Add"):
{
writeAddress(raf.length());
//currentPosition = raf.getFilePointer();
setChanged();
notifyObservers();
ToClient.writeLong(currentPosition);
}
break;
case ("First"):
{
System.out.println("server start first");
if (raf.length() > 0) readAddress(0);
currentPosition = raf.getFilePointer();
ToClient.writeLong(currentPosition);
System.out.println("server done first");
}
break;
case ("Next"):
{
currentPosition = FromClient.readLong();
if (currentPosition < raf.length()) {
readAddress(currentPosition);}
currentPosition = raf.getFilePointer();
ToClient.writeLong(currentPosition);
}
break;
case ("Previous"):
{
currentPosition = FromClient.readLong();
if (currentPosition - 2 * 2 * RECORD_SIZE > 0)
readAddress(currentPosition - 2 * 2 * RECORD_SIZE);
else if (raf.length() > 0) readAddress(0);
currentPosition = raf.getFilePointer();
ToClient.writeLong(currentPosition);
}
break;
case ("Last"):
{
long lastPosition = raf.length();
ToClient.writeLong(lastPosition);
if (lastPosition > 0)
readAddress(lastPosition - 2 * RECORD_SIZE);
currentPosition = raf.getFilePointer();
ToClient.writeLong(currentPosition);
}
break;
case ("Midst"):
{
numberOfRecords = (raf.length()) / (2 * RECORD_SIZE);
ToClient.writeLong(numberOfRecords);
if (numberOfRecords > 0)
readAddress((numberOfRecords/2)*(2*RECORD_SIZE));
currentPosition = raf.getFilePointer();
ToClient.writeLong(currentPosition);
}
break;
case "Update":
{
currentPosition = FromClient.readLong();
if (currentPosition > 0) {
currentPosition = currentPosition - 2*RECORD_SIZE;
ToClient.writeLong(currentPosition);}
writeAddress(currentPosition);
//currentPosition = raf.getFilePointer();
setChanged();
notifyObservers();
ToClient.writeLong(currentPosition);
}
break;
case "Delete":
{
currentPosition = FromClient.readLong();
numberOfRecords = (raf.length()) / (2 * RECORD_SIZE);
ToClient.writeLong(numberOfRecords);
if (numberOfRecords == 0) {
currentPosition = raf.getFilePointer();
ToClient.writeLong(currentPosition);
return;
}
else if (numberOfRecords == 1)
{
raf.setLength(0);
currentPosition = raf.getFilePointer();
ToClient.writeLong(currentPosition);
return;
}
fromPosition = currentPosition;
long toPosition = fromPosition - 2*RECORD_SIZE;
numberOfRecordsToCopy = (raf.length()- fromPosition) / (2 * RECORD_SIZE);
ToClient.writeLong(numberOfRecordsToCopy);
for (int i=0; i < numberOfRecordsToCopy; i++ )
{
readAddress(fromPosition);
writeAddress(toPosition);
fromPosition += 2*RECORD_SIZE;
toPosition += 2*RECORD_SIZE;
}
raf.setLength(raf.length() - 2*RECORD_SIZE);
ToClient.writeLong(raf.length());
readAddress(0);
currentPosition = raf.getFilePointer();
setChanged();
notifyObservers();
ToClient.writeLong(currentPosition);
}
break;
case "Insert":
{
currentPosition = FromClient.readLong();
if (raf.length() == 0)
{
writeAddress(raf.length());
ToClient.writeLong(raf.getFilePointer());
return;
}
fromPosition = currentPosition-2*RECORD_SIZE;
long currentFrom = raf.length() - 2*RECORD_SIZE;
numberOfRecordsToCopy = (raf.length()- fromPosition) / (2 * RECORD_SIZE);
ToClient.writeLong(numberOfRecordsToCopy);
for (int i=0; i < numberOfRecordsToCopy; i++ )
{
readAddress(currentFrom);
writeAddress(currentFrom + 2*RECORD_SIZE);
currentFrom -= 2*RECORD_SIZE;
}
writeAddress(fromPosition);
raf.setLength(raf.length() - 2*RECORD_SIZE);
readAddress(0);
currentPosition = raf.getFilePointer();
setChanged();
notifyObservers();
ToClient.writeLong(currentPosition);
}
break;
case "Duplicate":
{
numberOfRecords = (raf.length()) / (2 * RECORD_SIZE);
ToClient.writeLong(numberOfRecords);
for (int i=0; i < numberOfRecords; i++ )
{
readAddress(i*2*RECORD_SIZE);
writeAddress(raf.length());
}
if (numberOfRecords>0) readAddress(0);
setChanged();
notifyObservers();
ToClient.writeLong(currentPosition);
}
break;
case "Sort":
{
sortFile();
if (raf.length() > 0)
{
readAddress(0);
}
setChanged();
notifyObservers();
}
break;
}
//}
Platform.runLater(() ->{
ta.appendText("Done operation for client "+ this.id + "\n");
});
}
}
catch(IOException e) {
System.err.println(e);
}
}
private void sortFile() {
try {
int size = (int) (raf.length() / RECORD_SIZE / 2);
int ascORdes, from, to, idx;
raf.seek(0);
if (raf.length() == 0) return ;
ascORdes = FromClient.readBoolean() ? -1 : 1;
String fileToSort[];
fileToSort = new String[size];
for (int i = 0; i < size;i++)
{
fileToSort[i] =
FixedLengthStringIO.readFixedLengthString(RECORD_SIZE, raf);
}
idx = FromClient.readInt();
from = fromToSortChoices[idx];
to = fromToSortChoices[idx+1];
HeapSortaORd.heapSortaORd(fileToSort, ascORdes, from, to);
raf.seek(0);
for (int i = 0; i < size;i++)
{
FixedLengthStringIO.writeFixedLengthString(
fileToSort[i], RECORD_SIZE, raf);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
private void readAddress(long position) {
try {
raf.seek(position);
String name =
FixedLengthStringIO.readFixedLengthString(NAME_SIZE, raf);
String street =
FixedLengthStringIO.readFixedLengthString(STREET_SIZE, raf);
String city =
FixedLengthStringIO.readFixedLengthString(CITY_SIZE, raf);
String state =
FixedLengthStringIO.readFixedLengthString(STATE_SIZE, raf);
String zip =
FixedLengthStringIO.readFixedLengthString(ZIP_SIZE, raf);
ToClient.flush();
ToClient.writeUTF(name);
ToClient.flush();
ToClient.writeUTF(street);
ToClient.flush();
ToClient.writeUTF(city);
ToClient.flush();
ToClient.writeUTF(state);
ToClient.flush();
ToClient.writeUTF(zip);
ToClient.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeAddress(long position) {
try
{ raf.seek(position);
String name = FromClient.readUTF();
String street = FromClient.readUTF();
String city = FromClient.readUTF();
String state = FromClient.readUTF();
String zip = FromClient.readUTF();
FixedLengthStringIO.writeFixedLengthString(name
== null ? " " : name, NAME_SIZE, raf);
FixedLengthStringIO.writeFixedLengthString(street
== null ? " " : street, STREET_SIZE, raf);
FixedLengthStringIO.writeFixedLengthString(city
== null ? " " : city, CITY_SIZE, raf);
FixedLengthStringIO.writeFixedLengthString(state
== null ? " " : state, STATE_SIZE, raf);
FixedLengthStringIO.writeFixedLengthString(zip
== null ? " " : zip, ZIP_SIZE, raf);
}
catch (IOException e1)
{ e1.printStackTrace();
}
}
}
#Override
public void update(Observable observable, Object object) {
this.flag = 0;
for (int i = 0; i < clients.size(); i++) {
System.out.println("File changed. Client " + clients.get(i).id + " back to first");
clients.get(i).currentPosition = 0;
}
}
}
The client class:
public class Client extends Application
//extends Observable
{ // IO streams
DataOutputStream toServer = null;
DataInputStream fromServer = null;
Socket socket;
//Specify the size of five string fields in the record
final static int NAME_SIZE = 32;
final static int STREET_SIZE = 32;
final static int CITY_SIZE = 20;
final static int STATE_SIZE = 2;
final static int ZIP_SIZE = 5;
final static int RECORD_SIZE = (NAME_SIZE + STREET_SIZE
+ CITY_SIZE + STATE_SIZE + ZIP_SIZE);
final static String fileName0 = "address0";
// Text fields
private TextField jtfName = new TextField();
private TextField jtfStreet = new TextField();
private TextField jtfCity = new TextField();
private TextField jtfState = new TextField();
private TextField jtfZip = new TextField();
// Buttons
private Button jbtAdd = new Button("Add");
private Button jbtFirst = new Button("First");
private Button jbtNext = new Button("Next");
private Button jbtPrevious = new Button("Previous");
private Button jbtLast = new Button("Last");
private Button jbtMidst = new Button("Midst");
private Button jbtUpdate = new Button("Update");
private Button jbtDelete = new Button("Delete");
private Button jbtInsert = new Button("Insert");
private Button jbtClear = new Button("Clear");
private Button jbtDuplicate = new Button("Duplicate");
private Button jbtDoSort = new Button("Perform Sort");
private GridPane jpSortPanel;
private RadioButton jrbSortDecsend;
private RadioButton jrbSortAcsend;
private ToggleGroup bgSortType;
private ObservableList<String> sortChoices =
FXCollections.observableArrayList(
"Name", "Street", "City", "State", "Zip");
private GridPane mainPane =new GridPane();
#SuppressWarnings("rawtypes")
private ComboBox jcbSortChoices;
private DataOutputStream ToServer;
private DataInputStream FromServer;
private long fileLength;
private long currentPosition;
private long numberOfRecords;
private long numberOfRecordsToCopy;
private TextArea ta = new TextArea();
private int flag = 0;
#Override // Override the start method in the Application class
public void start(Stage primaryStage)
{
/** Make scene and add to Application **/
jtfName.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
jtfStreet.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
jtfCity.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
jtfState.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
jtfZip.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
// Panel jpAddress for holding labels & text fields
GridPane jpAddress = new GridPane();
String LabelColumn[]={"Name","Street","City"};
Label l[]=new Label[LabelColumn.length];
for(int i=0;i<LabelColumn.length;i++)
{
l[i] = new Label(LabelColumn[i]);
l[i].setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}
jpAddress.addColumn(0,l);
// Panel jpStateZip for holding state and zip
GridPane jpStateZip = new GridPane();
jpStateZip.addRow(0, new Label("State"),jtfState,new Label("Zip"),jtfZip);
// Panel p3 for holding jtfCity and jpStateZip
GridPane p3 = new GridPane();
p3.addRow(0, jtfCity,jpStateZip);
growChildren(jpStateZip);
growChildren(p3);
jpAddress.addColumn(1, jtfName, jtfStreet,p3);
// Set the panel with line border
jpAddress.getStyleClass().add(
"{ -fx-border-color: black;-fx-border-width: 5;}");
growChildren(jpAddress);
// Add buttons to a panel
GridPane jpButton = new GridPane();
HBox hb = new HBox();
hb.getChildren().addAll(jbtAdd,jbtFirst,jbtNext,jbtPrevious,jbtLast,jbtMidst,jbtUpdate,jbtDelete,jbtInsert,jbtClear,jbtDuplicate);
jpButton.addRow(0, hb);
growChildren(jpButton);
// Add jpAddress and jpButton to the mainPane
mainPane.addColumn(0, jpAddress,jpButton,ta);
/* ============ sort Panel ============ */
jpSortPanel = new GridPane();
jrbSortAcsend = new RadioButton("Ascending");
jrbSortAcsend.setSelected(true);
jrbSortDecsend = new RadioButton("descending");
jcbSortChoices = new ComboBox<String>(sortChoices);
jcbSortChoices.getSelectionModel().selectFirst();
bgSortType = new ToggleGroup();
bgSortType.getToggles().addAll(jrbSortAcsend,jrbSortDecsend);
jpSortPanel.addColumn(0, jrbSortAcsend,jrbSortDecsend,jcbSortChoices);
growChildren(jpSortPanel);
/* ========== end of sort panel ======= */
jbtDoSort.setMaxWidth(Double.MAX_VALUE);
mainPane.addColumn(1, jpSortPanel,jbtDoSort);
growChildren(mainPane);
try {
socket = new Socket("localhost", 8000);
// Create an input stream to receive data from the server
FromServer = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
ToServer = new DataOutputStream(socket.getOutputStream());
currentPosition = FromServer.readLong();
primaryStage.setOnCloseRequest(e->{
try{
socket.close();
Platform.exit();
System.exit(0);
}
catch(Exception e1){
e1.printStackTrace();
}});
}
catch (IOException ex) {
ex.printStackTrace();
}
jbtClear.setOnAction(e ->
{
try {
fileLength = FromServer.readLong();
ToServer.writeUTF("Clear");
clearTextFields();
currentPosition = 0;
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("client done clear");
});
jbtAdd.setOnAction(e ->
{
try {
fileLength = FromServer.readLong();
ToServer.writeUTF("Add");
ToServer.writeUTF(jtfName.getText());
ToServer.writeUTF(jtfStreet.getText());
ToServer.writeUTF(jtfCity.getText());
ToServer.writeUTF(jtfState.getText());
ToServer.writeUTF(jtfZip.getText());
currentPosition = FromServer.readLong();
System.out.println("client done add");
} catch (Exception e1) {
e1.printStackTrace();
}
});
jbtFirst.setOnAction(e ->
{
System.out.println("client start first");
try {
first();
System.out.println("client done first");
} catch (Exception e1) {
e1.printStackTrace();
}
});
jbtNext.setOnAction(e ->
{
try {
fileLength = FromServer.readLong();
ToServer.writeUTF("Next");
ToServer.writeLong(currentPosition);
if (currentPosition < fileLength) {
readAddress();}
currentPosition = FromServer.readLong();
System.out.println("client done next");
} catch (Exception e1) {
e1.printStackTrace();
}
});
jbtPrevious.setOnAction(e ->
{
try {
fileLength = FromServer.readLong();
ToServer.writeUTF("Previous");
ToServer.writeLong(currentPosition);
if (currentPosition - 2 * 2 * RECORD_SIZE > 0)
readAddress();
else if (fileLength > 0) readAddress();
currentPosition = FromServer.readLong();
System.out.println("client done previous");
} catch (Exception e1) {
e1.printStackTrace();
}
});
jbtLast.setOnAction(e ->
{
try {
fileLength = FromServer.readLong();
ToServer.writeUTF("Last");
long lastPosition = FromServer.readLong();
if (lastPosition > 0)
readAddress();
currentPosition = FromServer.readLong();
System.out.println("client done last");
} catch (Exception e1) {
e1.printStackTrace();
}
});
jbtMidst.setOnAction(e ->
{
try {
fileLength = FromServer.readLong();
ToServer.writeUTF("Midst");
numberOfRecords = FromServer.readLong();
if (numberOfRecords > 0)
readAddress();
currentPosition = FromServer.readLong();
System.out.println("client done midst");
} catch (Exception e1) {
e1.printStackTrace();
}
});
jbtUpdate.setOnAction(e ->
{
try {
fileLength = FromServer.readLong();
ToServer.writeUTF("Update");
ToServer.writeLong(currentPosition);
if (currentPosition > 0)
currentPosition = FromServer.readLong();
ToServer.writeUTF(jtfName.getText());
ToServer.writeUTF(jtfStreet.getText());
ToServer.writeUTF(jtfCity.getText());
ToServer.writeUTF(jtfState.getText());
ToServer.writeUTF(jtfZip.getText());
currentPosition = FromServer.readLong();
System.out.println("client done update");
} catch (Exception e1) {
e1.printStackTrace();
}
});
jbtDelete.setOnAction(e ->
{ try {
fileLength = FromServer.readLong();
ToServer.writeUTF("Delete");
ToServer.writeLong(currentPosition);
numberOfRecords = FromServer.readLong();
if (numberOfRecords == 0) {
currentPosition = FromServer.readLong();
return;
}
else if (numberOfRecords == 1)
{
clearTextFields();
currentPosition = FromServer.readLong();
return;
}
numberOfRecordsToCopy = FromServer.readLong();
for (int i=0; i < numberOfRecordsToCopy; i++ )
{
readAddress();
ToServer.writeUTF(jtfName.getText());
ToServer.writeUTF(jtfStreet.getText());
ToServer.writeUTF(jtfCity.getText());
ToServer.writeUTF(jtfState.getText());
ToServer.writeUTF(jtfZip.getText());
}
fileLength = FromServer.readLong();
readAddress();
currentPosition = FromServer.readLong();
System.out.println("client done delete");
}catch (Exception e1) {
e1.printStackTrace();
}
});
jbtInsert.setOnAction(e ->
{
try {
fileLength = FromServer.readLong();
ToServer.writeUTF("Insert");
ToServer.writeLong(currentPosition);
if (fileLength == 0)
{
ToServer.writeUTF(jtfName.getText());
ToServer.writeUTF(jtfStreet.getText());
ToServer.writeUTF(jtfCity.getText());
ToServer.writeUTF(jtfState.getText());
ToServer.writeUTF(jtfZip.getText());
currentPosition = FromServer.readLong();
System.out.println("client done insert");
return;
}
String recordToInsert[] = { jtfName.getText(), jtfStreet.getText(), jtfCity.getText(),
jtfState.getText(), jtfZip.getText()};
numberOfRecordsToCopy = FromServer.readLong();
for (int i=0; i < numberOfRecordsToCopy; i++ )
{
readAddress();
ToServer.writeUTF(jtfName.getText());
ToServer.writeUTF(jtfStreet.getText());
ToServer.writeUTF(jtfCity.getText());
ToServer.writeUTF(jtfState.getText());
ToServer.writeUTF(jtfZip.getText());
}
//readAddress();
jtfName.setText(recordToInsert[0]);
jtfStreet.setText(recordToInsert[1]);
jtfCity.setText(recordToInsert[2]);
jtfState.setText(recordToInsert[3]);
jtfZip.setText(recordToInsert[4]);
ToServer.writeUTF(jtfName.getText());
ToServer.writeUTF(jtfStreet.getText());
ToServer.writeUTF(jtfCity.getText());
ToServer.writeUTF(jtfState.getText());
ToServer.writeUTF(jtfZip.getText());
readAddress();
currentPosition = FromServer.readLong();
System.out.println("client done insert");
} catch (Exception e1) {
e1.printStackTrace();
}
});
jbtDuplicate.setOnAction(e ->
{
try {
fileLength = FromServer.readLong();
ToServer.writeUTF("Duplicate");
//ToServer.writeLong(currentPosition);
numberOfRecords = FromServer.readLong();
for (int i=0; i < numberOfRecords; i++ )
{
readAddress();
ToServer.writeUTF(jtfName.getText());
ToServer.writeUTF(jtfStreet.getText());
ToServer.writeUTF(jtfCity.getText());
ToServer.writeUTF(jtfState.getText());
ToServer.writeUTF(jtfZip.getText());
}
if (numberOfRecords>0) readAddress();
currentPosition = FromServer.readLong();
System.out.println("client done duplicate");
} catch (Exception e1) {
e1.printStackTrace();
}
});
jbtDoSort.setOnAction(e ->
{
try {
fileLength = FromServer.readLong();
ToServer.writeUTF("Sort");
ToServer.writeBoolean(jrbSortDecsend.isSelected());
ToServer.writeInt(jcbSortChoices.getSelectionModel().getSelectedIndex());
} catch (Exception e1) {
e1.printStackTrace();
}
if (fileLength > 0) readAddress();
System.out.println("client done sort");
});
/** Add to details and run Application **/
Scene scene = new Scene (mainPane, -100, -100);
primaryStage.setTitle("Complex Address System"); // Set the window title
primaryStage.setScene(scene); // Place the scene in the window
primaryStage.setAlwaysOnTop(true);
primaryStage.show(); // Display the window
primaryStage.setOnCloseRequest( new EventHandler<WindowEvent>(){
public void handle(WindowEvent event){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}
});
}
private void first() {
try {
fileLength = FromServer.readLong();
ToServer.writeUTF("First");
ToServer.flush();
if (fileLength > 0) readAddress();
currentPosition = FromServer.readLong();
} catch (Exception e1) {
e1.printStackTrace();
}
}
#SuppressWarnings("static-access")
private void growChildren(GridPane pane)
{ pane.getChildren().forEach(chiled ->
{ pane.setHgrow(chiled, Priority.ALWAYS);
pane.setVgrow(chiled, Priority.ALWAYS);
});
}
private void readAddress() {
try {
String name = FromServer.readUTF();
String street = FromServer.readUTF();
String city = FromServer.readUTF();
String state = FromServer.readUTF();
String zip = FromServer.readUTF();
jtfName.setText(name);
jtfStreet.setText(street);
jtfCity.setText(city);
jtfState.setText(state);
jtfZip.setText(zip);
} catch (IOException e) {
e.printStackTrace();
}
}
private void clearTextFields() {
jtfName.setText(null);
jtfStreet.setText(null);
jtfCity.setText(null);
jtfState.setText(null);
jtfZip.setText(null);
}
public static void main(String[] args)
{ launch(args);
}
}
I'd appreciate any help at all, I have spent hours at this to no avail.
So I need to add an instance of a class I found online called GridDisplay to the Parent root, as I need the scene to then display this for me. But I also need to use the fxml file to set up the root and hence the scene. Here is what I have so far:
#FXML private BorderPane mainPanel = new BorderPane();
#FXML private TilePane tilePane = new TilePane();
#FXML private Pane topCanvas = new Pane();
#FXML private Pane leftCanvas = new Pane();
#FXML private Pane rightCanvas = new Pane();
#FXML private Pane bottomCanvas = new Pane();
#FXML private ScrollPane scrollPane = new ScrollPane();
#FXML private Button stacksButton = new Button();
#FXML private Button queueButton = new Button();
#FXML private Button stepStacksButton = new Button();
#FXML private Button stepQueueButton = new Button();
private MazeSolver solver;
private GridDisplay gridDisplay;
private ArrayList<String> maze;
private String fileLocation;
private int height;
private int width;
#Override
public void start(Stage primaryStage) throws IOException, InterruptedException
{
solver = new MazeSolver(fileLocation);
int[] dimensions = solver.getDimensions();
this.width = dimensions[0];
this.height = dimensions[1];
maze = solver.getMazeLayout();
primaryStage.setTitle("Maze Solver");
//this is the root which uses the fxml file and in turn sets up the scene
Parent root = FXMLLoader.load(getClass().getResource("MazeApp.fxml"));
Scene scene = new Scene(root);
//Represents the grid with Rectangles
gridDisplay = new GridDisplay(height, width);
gridDisplay.setMaze(maze);
mainPanel = new BorderPane();
//here i set the scroll pane as the mainPanel's content
mainPanel.setCenter(scrollPane);
//and then set the gridDisplay as the scroll panes content
scrollPane.setContent(gridDisplay.getDisplay());
//re paints the grid and its boxes
gridDisplay.createElements();
primaryStage.setScene(scene);
primaryStage.show();
}
The order in which I do any of these things makes no difference, it just never shows. I am always left with the window and a blank space where the maze should be, like so: https://www.dropbox.com/s/rqxa9dy2w9lw4tz/Screenshot%202015-03-11%2022.01.46.png?dl=0 (sorry about linking, cant post image directly).
Any help would be greatly appreciated.
EDIT:
My GridDisplay class:
public class GridDisplay{
private static final double ELEMENT_SIZE = 30;
private static final double GAP = ELEMENT_SIZE / 90;
private TilePane tilePane = new TilePane();
private Group display = new Group(tilePane);
private int nRows;
private int nCols;
private ArrayList<String> maze = null;
public GridDisplay(int nRows, int nCols)
{
tilePane.setStyle("-fx-background-color: rgba(255, 215, 0, 0.1);");
tilePane.setHgap(GAP);
tilePane.setVgap(GAP);
setColumns(nCols);
setRows(nRows);
}
public void setColumns(int newColumns)
{
nCols = newColumns;
tilePane.setPrefColumns(nCols);
createElements();
}
public void setRows(int newRows)
{
nRows = newRows;
tilePane.setPrefRows(nRows);
createElements();
}
public TilePane getDisplay()
{
return tilePane;
}
public void createElements()
{
tilePane.getChildren().clear();
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nCols; j++) {
if(maze==null)
tilePane.getChildren().add(createElement(Color.RED));
else
{
Color color = Color.WHITE;
if(Character.toString(maze.get(i).charAt(j)).equals("#"))
color = Color.BLACK;
if(Character.toString(maze.get(i).charAt(j)).equals("o"))
color = Color.GREEN;
if(Character.toString(maze.get(i).charAt(j)).equals("*"))
color = Color.RED;
tilePane.getChildren().add(createElement(color));
}
}
}
}
public void colorSolved()
{
tilePane.getChildren().clear();
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nCols; j++) {
if(maze==null)
tilePane.getChildren().add(createElement(Color.RED));
else
{
Color color = Color.WHITE;
if(Character.toString(maze.get(i).charAt(j)).equals("#"))
color = Color.BLACK;
if(Character.toString(maze.get(i).charAt(j)).equals("o"))
color = Color.GREEN;
if(Character.toString(maze.get(i).charAt(j)).equals("*"))
color = Color.RED;
tilePane.getChildren().add(createElement(color));
}
}
}
}
public void setMaze(ArrayList<String> maze)
{
this.maze = maze;
}
private Rectangle createElement(Color color) {
Rectangle rectangle = new Rectangle(ELEMENT_SIZE, ELEMENT_SIZE);
rectangle.setStroke(Color.BLACK);
rectangle.setFill(color);
return rectangle;
}
}
I cannot share the code for the xml as it will not paste into this window properly, here is a picture:
https://www.dropbox.com/s/27tbliubdlqdfzm/Screenshot%202015-03-12%2010.09.25.png?dl=0
Since your root is a BorderPane, you can just set the appropriate region in the code:
BorderPane root = FXMLLoader.load(getClass().getResource("MazeApp.fxml"));
Scene scene = new Scene(root);
//Represents the grid with Rectangles
gridDisplay = new GridDisplay(height, width);
gridDisplay.setMaze(maze);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(gridDisplay);
root.setCenter(scrollPane);
// etc
I have shown on a 10-key layout dynamically created, now I need to retrieve the ID of the button pressed to release the necessary procedure. Here the code.
Button Btn;
Toast msg;
RelativeLayout rl;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
Btn = (Button) findViewById( R.id.Btntu );
for (int i = 0; i < 10; i++) {
Btn = new Button(this);
Btn.setText("Número: " + i);
Btn.setGravity(Gravity.CENTER);
Btn.setId(i);
rl = new RelativeLayout(this);
RelativeLayout.LayoutParams parametros = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Btn.setLayoutParams(parametros);
rl.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT)
);
rl.addView(Btn);
ll.addView(rl);
}
I need the function to retrieve the ID of the button pressed.
Thank you all.
:o)
Have you tried getId() ?
http://developer.android.com/reference/android/view/View.html#getId%28%29
for (int i = 0; i < 10; i++) {
Btn = new Button(this);
Btn.setText("Número: " + i);
Btn.setGravity(Gravity.CENTER);
Btn.setId(i);
Btn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Log.d("Button", "ID clicked: " + Btn.getId());
// Or even
Log.d("View", "ID clicked: " + v.getId());
}
});
(...)
}
I can add SplitPane in a XYChart by clicking the "Add Pane" button, and it works fine.
Now I would like to remove SplitPane by clicking the "Remove pane" button.
Here is full code
public class SplitPaneFxTest extends Application {
SplitPane splitPane = new SplitPane();
double percSplit = 0.50;
int idxSplit = 0;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(SplitPaneFxTest.class, args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SplitPane Test");
Group root = new Group();
Scene scene = new Scene(root, 800, 650, Color.WHITE);
//CREATE THE SPLITPANE
splitPane.setPrefSize(scene.getWidth(), scene.getHeight());
splitPane.setOrientation(Orientation.VERTICAL);
//ADD LAYOUTS AND ASSIGN CONTAINED CONTROLS
BorderPane upperPane = new BorderPane();
HBox hbox = new HBox();
Button button1 = new Button("Add Pane");
hbox.getChildren().addAll(button1);
upperPane.setTop(hbox);
Button button2 = new Button("Remove Pane");
hbox.getChildren().addAll(button2);
upperPane.setTop(hbox);
BorderPane lowerPane = new BorderPane();
splitPane.getItems().addAll(upperPane);
splitPane.setDividerPosition(idxSplit, 0.70);
idxSplit++;
splitPane.getItems().addAll(lowerPane);
idxSplit++;
root.getChildren().add(splitPane);
primaryStage.setScene(scene);
primaryStage.show();
button1.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
BorderPane myborderpane = new BorderPane();
splitPane.getItems().addAll(myborderpane);
ObservableList<SplitPane.Divider> splitDiv = splitPane.getDividers();
System.out.println("splitDiv.size() "+splitDiv.size());
percSplit = 1/(double)(splitDiv.size()+1);
for (int i = 0; i< splitDiv.size(); i++) {
System.out.println("i "+i+" percSplit "+percSplit);
splitPane.setDividerPosition(i, percSplit);
percSplit += 1/(double)(splitDiv.size()+1);
}
}
});
}
}
Any help really appreciated.
You can store a list of inner panes and remove them based on this list:
final List<Pane> panes = new ArrayList<Pane>();
button1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
BorderPane myborderpane = new BorderPane();
//adding
panes.add(myborderpane);
splitPane.getItems().addAll(myborderpane);
ObservableList<SplitPane.Divider> splitDiv = splitPane.getDividers();
System.out.println("splitDiv.size() " + splitDiv.size());
percSplit = 1 / (double) (splitDiv.size() + 1);
for (int i = 0; i < splitDiv.size(); i++) {
System.out.println("i " + i + " percSplit " + percSplit);
splitPane.setDividerPosition(i, percSplit);
percSplit += 1 / (double) (splitDiv.size() + 1);
}
}
});
button2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
if (panes.size() > 0) {
// removing from both list and splitPane childs
Pane toDelete = panes.remove(0);
splitPane.getItems().remove(toDelete);
}
}
});
Also you can remove panes directly from ScrollPane.getChildren() but it can involve tricky and unreliable code to determine which pane to remove.