Issue
i have some problem with java fx, i'm developing a Service Mail and i have a problem with showing server stage ( with java fx). By using the pattern MVC i created: 1) MainServer.java ( where i start the java fx application) 2) ControllerServer.java ( Controller class) 3) ServerLog.java ( Model class, not important for my question, but had to mention it).
Now, i have another class called Server.java where there is the real server code, i tried to merge MainServer.java with Server.java by moving Server.java code into ControllerServer.java, server is working good, but stage does not show the stage, i think the problem is in while loop inside the initializable.
public class Server {
private static Object lock = new Object();
private static ServerLog serverLog = new ServerLog();
public static void getService(Socket s) throws IOException {
// not important for my question so deleted
}
public static void main(String[] args){
new File("users").mkdir();
try {
ServerSocket socket = new ServerSocket(8189);
while(true) {
Socket s = socket.accept();
getService(s);
}
}catch(IOException e){e.printStackTrace();}
}
}
public class ServerMain extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("View/server.fxml"));
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.setTitle("Server Log");
primaryStage.show();
}
}
public class ControllerServer implements Initializable {
@FXML
private ListView myListView;
//protected List<String> listLog = new ArrayList<>();
protected ListProperty<String> listProperty = new SimpleListProperty<>();
private ServerLog serverLog = new ServerLog();
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
serverLog.fillLogList();
} catch (IOException e) {
e.printStackTrace();
}
if (serverLog.logList!= null) {
for (int i = 0; i < serverLog.logList.size(); i++) {
System.out.println(serverLog.logList.get(i));
}
ObservableList<String> seasonList = FXCollections.observableArrayList(serverLog.logList);
myListView.setItems(seasonList);
}
}
}
what i want to do is delete ServerMain.java and transfer what ServerMain.java was doing into Server ( so server need to show the stage when opened)
Solution
had to edit my answer because at the end it worked, but not properly, with this class now i can see java fx stage correctly but server stopped work correctly, so if anyone have a better solution i would be honored to read this solution :( .
public class Server extends Application {
private static Object lock = new Object();
private static ServerLog serverLog = new ServerLog();
public static void getService(Socket s) throws IOException { ... }
public static void main(String[] args){
launch(args);
new File("users").mkdir();
try {
ServerSocket socket = new ServerSocket(8189);
while(true) {
Socket s = socket.accept();
getService(s);
}
}catch(IOException e){e.printStackTrace();}
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("View/server.fxml"));
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.setTitle("Server Log");
primaryStage.show();
}
}
Answered By - Ignorant_People_97