JavaFX Fehler beim setzen von Farben

Lucaaa

Bekanntes Mitglied
Hallo!
In Folgendem Code:
Java:
package com.ludevstudio.schoolmanager.Schedule;
import java.util.ArrayList;
import java.util.ResourceBundle;
import com.ludevstudio.schoolmanager.DataBaseControler;
import com.ludevstudio.schoolmanager.Subject;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
public class Schedule extends GridPane {
 String name;
 int days;
 int lessons;
 Boolean hasZeroLesson;
 Boolean hasWeekend;
 
 ResourceBundle bundle = ResourceBundle.getBundle("bundle");
 DataBaseControler controller;
 
 // Hex Colors
 final String basicColor = "blue";
 
 
 // Resources
 ArrayList<Subject> allSubjects;
 
 
 public Schedule(String name, int lessons, Boolean hasZeroLesson, Boolean hasWeekend) {
  controller =new DataBaseControler();
  
  this.name = name;
  this.lessons = lessons;
  this.hasZeroLesson = hasZeroLesson;
  this.hasWeekend = hasWeekend;
 
 // init
  setupGridpane();
  createEmptySchedule();
  load();
 }
 
 
 
 // tinit the Schedules UI before loading subjects
 private void createEmptySchedule() {
  // set nubmer of days
  if(hasWeekend) { 
   days = 7; 
   } else { 
    days = 5;
   } 
   
  // set top Left corner
  Label topLeftCorner = new Label();
  topLeftCorner.setText("");
  GridPane.setHgrow(topLeftCorner, Priority.ALWAYS);
  GridPane.setVgrow(topLeftCorner, Priority.ALWAYS);
  topLeftCorner.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
  topLeftCorner.setStyle("-fx-background-color: "+basicColor+";");
  add(topLeftCorner, 0, 0);
  
  
  
  // set the day Headline cells
  for(int i = 1; i<=days; i++) {
   add(dayUI(new Day(i)), i, 0);
  }
  
  // set the lessons headlines
  for(int i = 1; i<=lessons; i++) {
   add(lessonUI(new Lesson(i)), 0, i);
  }
  
  
 }
 
 
 // gridpane Settings
 private void setupGridpane() {
  // set Lines
  this.setGridLinesVisible(true);
  
  // set Stretching
  this.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
  
  
  
  // Set Column Width
  
  ColumnConstraints c =new ColumnConstraints();
  c.setMaxWidth(50);
  this.getColumnConstraints().add(c);
  for(int i =0; i<days; i++) {
   ColumnConstraints cc =new ColumnConstraints();
   cc.setPercentWidth(99/days);
   this.getColumnConstraints().add(cc);
  }
  
  
 }
 
 
 // Load schedule Subjects
   private void load() {
    
    // Load all existing Subjects
    ArrayList<String> allSubjectIDs = controller.getSubjectList();
    allSubjects =new ArrayList<>();
    
    for(String ID : allSubjectIDs) {
     ArrayList<String> subjectInfos = controller.loadSubject(ID);
     
     
     allSubjects.add(new Subject(subjectInfos.get(0), subjectInfos.get(1), 
       subjectInfos.get(2), subjectInfos.get(3), Color.web(subjectInfos.get(4))));
    }
    
    // fill subjects into schedule
    ArrayList<String[]> subjectIDs = controller.loadScheduleSubjects(name);
    
    for(int i=0; i<lessons; i++) {
     for(int j=0; j<days; j++) {
      Subject subject = getSubject(subjectIDs.get(i)[j]);
      if(subject!=null) {
      add(subjectUI(subject), j+1, i+1);
     }
     }
    }
    
   }
 
   
   
   
 // Cell UIs
 
 private Label dayUI(Day day) {
  Label cell =new Label();
  cell.setText(day.getName());
  cell.setStyle("-fx-background-color: "+basicColor+"; "
    + "-fx-text-fill: white; "
    + "-fx-padding: 7; "
    + "-fx-font-size: 1.4em;  "
    + "-fx-font-weight: bold;");
  cell.setAlignment(Pos.CENTER);
  cell.setMaxWidth(Double.MAX_VALUE);
  GridPane.setHgrow(cell, Priority.ALWAYS);
  return cell;
 }
 
 
 private Label lessonUI(Lesson lesson) {
  Label cell =new Label();
  cell.setText(lesson.getNumber()+"");
  cell.setStyle("-fx-background-color: "+basicColor+"; "
    + "-fx-text-fill: white; "
    + "-fx-padding: 7; "
    + "-fx-font-size: 1.4em;  "
    + "-fx-font-weight: bold;");
  cell.setAlignment(Pos.CENTER);
  cell.setMaxWidth(Double.MAX_VALUE);
  GridPane.setHgrow(cell, Priority.ALWAYS);
  return cell;
 }
 private VBox subjectUI(Subject subject) {
  String colorStr = "#"+Integer.toHexString(subject.getColor().hashCode());
  
  VBox cell =new VBox();
  Label labName =new Label(subject.getName());
  labName.setStyle("-fx-text-fill: white; -fx-font-weight: bold; -fx-font-size: 150%;");
  Label labTeacher =new Label(subject.getTeacher());
  labTeacher.setStyle("-fx-text-fill: white; -fx-font-weight: bold;");
  Label labRoom =new Label(subject.getRoom());
  labRoom.setStyle("-fx-text-fill: white; -fx-font-weight: bold;");
  cell.getChildren().addAll(labName, labTeacher, labRoom);
  cell.setStyle("-fx-background-color: "+colorStr+"; "
    + "-fx-padding: 5 10 5 10;");
  
  return cell;
 }
 
 
 
 
 // Other
 private Subject getSubject(String ID) {
  for(Subject s : allSubjects) {
   if(s.getID().equals(ID)) {
    return s;
   }
  }
  return null;
 }
 
 
}

In der load() übergebe ich eine aus einer Datenbank geladene Farbe.
Der geladene String sieht folgendermaßen aus:
Code:
00ff00

Fehlermeldung:
Code:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
 at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
 at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
 at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Invalid color specification
 at javafx.scene.paint.Color.web(Color.java:389)
 at javafx.scene.paint.Color.web(Color.java:636)
 at com.ludevstudio.schoolmanager.Schedule.Schedule.load(Schedule.java:123)
 at com.ludevstudio.schoolmanager.Schedule.Schedule.<init>(Schedule.java:47)
 at com.ludevstudio.schoolmanager.SchedulePage.updateView(SchedulePage.java:151)
 at com.ludevstudio.schoolmanager.SchedulePage.initFooter(SchedulePage.java:104)
 at com.ludevstudio.schoolmanager.SchedulePage.<init>(SchedulePage.java:72)
 at com.ludevstudio.schoolmanager.MainWindow.initComponents(MainWindow.java:66)
 at com.ludevstudio.schoolmanager.MainWindow.start(MainWindow.java:56)
 at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
 at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
 at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
 at java.security.AccessController.doPrivileged(Native Method)
 at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
 at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
 at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
 at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
 ... 1 more

Kann mir da wer helfen?
 
Bist du dir sicher, dass der String so aussieht und nicht noch irgendwelche anderen Zeichen (Leerzeichen, Zeilenumbruch, .. ) beinhaltet?
 
An sich sollte Color.web("00ff00") wunderbar funktionieren. Dementsprechend muss ja irgendwo etwas mit dem String passieren, sonst würde die Exception nicht geworfen werden.
Lass dir den String (inkl. Länge) am Besten einfach mal ausgeben - irgendwo muss sich ja etwas eingeschlichen haben 😉
 
Das ist wirklich merkwürdig. Was für einen Farbwert gibt dir denn System.out.println(Color.web("lime"));? Müsste ja theoretisch 0x00FF00FF sein.
Welche Java-Version nutzt du?
 
Wenn ich Color.Web() den String "lime" übergebe, müsste es ja eigentlich Grün werden oder?
Bei mir ist es pink!
Hab gerade noch mal deinen gesamten Code angeschaut. In deiner subjectUI Methode schreibst du sowas:
Java:
String colorStr = "#"+ Integer.toHexString(subject.getColor().hashCode());
Deswegen kommt als Farbe auch pink heraus. hashcode() entspricht nicht zuverlässig dem RGB Integer-Wert. Versuch mal lieber sowas: String colorString = color.toString().replace("0x", "#");.

Warum die Exception fliegt weiß ich bisher immer noch nicht. Ich kann mir wirklich nur vorstellen, dass es irgendein Subject gibt, welches keine "richtige" Farbe als Farbstring besitzt.
 
Zuletzt bearbeitet:
Der komplette Stacktrace besteht aus einer Zeile? 😵
Vorhin ja.
Nach einem Eclipse Neustart kommt wieder das:
Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.IllegalArgumentException: Invalid color specification at javafx.scene.paint.Color.web(Color.java:389) at javafx.scene.paint.Color.web(Color.java:636) at com.ludevstudio.schoolmanager.Schedule.Schedule.load(Schedule.java:124) at com.ludevstudio.schoolmanager.Schedule.Schedule.<init>(Schedule.java:47) at com.ludevstudio.schoolmanager.SchedulePage.updateView(SchedulePage.java:151) at com.ludevstudio.schoolmanager.SchedulePage.initFooter(SchedulePage.java:104) at com.ludevstudio.schoolmanager.SchedulePage.<init>(SchedulePage.java:72) at com.ludevstudio.schoolmanager.MainWindow.initComponents(MainWindow.java:66) at com.ludevstudio.schoolmanager.MainWindow.start(MainWindow.java:56) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177) ... 1 more
 
Wir sind wieder an der selben Stelle angelangt wie am Anfang. Das was bei subjectInfos.get(4) in Zeile 124 zurückkommt scheint nicht konform mit der Farbspezifikation von Color.web zu sein. Wie bereits davor gesagt kann ich mir nicht vorstellen, dass es an was anderem liegt. Prüf am besten noch mal Haar genau nach, ob es sich bei diesem String immer um einen validen Farbcode handelt - ob keine Sonderzeichen wie Space, Zeilenumbruch oder was auch immer dran hängen.
 
String immer um einen validen Farbcode handelt
In der Datenbank waren 2 Leerzeilen (nicht null, sondern ein leerer String) Ich hatte zwar mit einer Abfrage geprüft, ob der String != null ist, aber nicht ob er leer ist. Werde das mal ändern (müssen).
Jetzt, wo ich die beiden Zeilen entfernt habe, funktioniert es wunderbar 🙂
Danke für deine Hilfe @Robat
 
Ich hatte gestern noch dein Repository angeschaut und zum Laufen gebracht. Mir ist aufgefallen, dass du beim Erstellen von Tabellen keine Datentypen angibst und in der Erstellung von der Subjects Tabelle die ID vergessen hattest. Ich frage mich ob das so bei dir überhaupt funktioniert hat mit dem Stand.
Den Fehler mit den Farben konnte ich nicht reproduzieren. Aber mit deiner Erklärung ergibt es auch Sinn.
 

Neue Themen


Zurück
Oben