Hallo miteinander,
ich versuche gerade herauszufinden, wie ich Bilder anzeigen lassen kann, sobald ich in der ComboBox eine Auswahl anklicke. Ich habe eine Combobox mit den jeweiligen Unterpunkten und eine ImageView angelegt. Habe auch den Pfad für die Bilder schon angegeben.
Die Frage ist, wie ich das mit dem EventHandler etc. machen muss. Ich weiß nicht, wie ich ein Element von der ComboBox anspreche. Z.B "Wenn `Adapter` von der ComboBox angeklickt wird, soll das Bild von Adapter (adapter.png) erscheinen."
Ich bitte um Code Beispiel..
ich versuche gerade herauszufinden, wie ich Bilder anzeigen lassen kann, sobald ich in der ComboBox eine Auswahl anklicke. Ich habe eine Combobox mit den jeweiligen Unterpunkten und eine ImageView angelegt. Habe auch den Pfad für die Bilder schon angegeben.
Die Frage ist, wie ich das mit dem EventHandler etc. machen muss. Ich weiß nicht, wie ich ein Element von der ComboBox anspreche. Z.B "Wenn `Adapter` von der ComboBox angeklickt wird, soll das Bild von Adapter (adapter.png) erscheinen."
Ich bitte um Code Beispiel..
Code:
package pac;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.ResourceBundle;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class Controller {
public static final int WIN_WIDTH = 1200;
public static final int WIN_HEIGHT = 800;
private Pane root;
private Text text;
private TextField textfield;
private TextArea textarea;
private Label label;
private ComboBox cb = new ComboBox(FXCollections.observableArrayList("AbstractFactory", "Adapter", "Bridge",
"Builder", "Chain of Responsibility", "Command", "Composite", "Decorator", "Facade", "Factory Method",
"Flyweight", "Interpreter", "Iterator", "Mediator", "Memento", "Observer", "Prototyp", "Proxy", "Singleton",
"State", "Strategy"));
private ImageView AbstractFactory;
private ImageView Adapter;
private ImageView Bridge;
private ImageView Builder;
private ImageView ChainofResponsibility;
private ImageView Command;
private ImageView Composite;
private ImageView Decorator;
private ImageView Facade;
private ImageView FactoryMethod;
private ImageView Flyweight;
private ImageView Interpreter;
private ImageView Iterator;
private ImageView Mediator;
private ImageView Memento;
private ImageView Observer;
private ImageView Prototyp;
private ImageView Proxy;
private ImageView Singleton;
private ImageView State;
private ImageView Stategy;
public void init() {
root = new Pane();
text = new Text("Design Pattern Searching Machine");
text.setFont(new Font("Arial",20));
textfield = new TextField("Enter your key");
textfield.setPrefColumnCount(40);
textarea = new TextArea();
label= new Label("Please select a Pattern");
text.setLayoutX(50);
text.setLayoutY(90);
textfield.setLayoutX(50);
textfield.setLayoutY(130);
textarea.setLayoutX(50);
textarea.setLayoutY(180);
textarea.setPrefHeight(450);
textarea.setScrollTop(Double.MAX_VALUE);
textarea.setWrapText(true);
label.setLayoutX(650);
label.setLayoutY(110);
cb.setLayoutX(650);
cb.setLayoutY(130);
root.getChildren().addAll(text, textfield, textarea, label, cb);
}
public void ActionPerfomed(Event e) throws FileNotFoundException, MalformedURLException {
e.getEventType();
fotosLaden(); // Aktualisiere ImageViews
System.out.println("Hallo");
}
void fotosLaden() throws FileNotFoundException, MalformedURLException {
// Lege Array mit allen Dateien im Ordner Fotobox an
File file = new File("/Users/Erkus/Desktop/patternimages");
File[] files = file.listFiles();
// Sortieren: Neuere Datei oben
Arrays.sort(files, new Comparator<Object>() {
public int compare(Object o1, Object o2) {
File f0 = (File) o1, f1 = (File) o2;
long last0 = f0.lastModified(), last1 = f1.lastModified();
return (int) (last1 - last0);
}
});
// Gebe die Dateinamen wie oben sortiert aus
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
// Sortiere die Fotos in den ImageView ein
Image AbstractFactory = null;
ImageView Adapter = null;
ImageView Bridge = null;
Image Builder = null;
Image ChainOfResponsibility = null;
Image Command = null;
Image Composite = null;
Image Decorator = null;
Image Facade = null;
Image FactoryMethod = null;
Image Fylweight = null;
Image Interpreter = null;
Image Iterator = null;
Image Mediator = null;
Image Memento = null;
Image Observer = null;
Image Prototype = null;
Image Poxy = null;
Image Singleton = null;
Image State = null;
Image Strategy = null;
Image TemplateMethod = null;
Image Visitor = null;
try {
AbstractFactory = new Image(new FileInputStream(files[0]));
//Adapter = new Image(new FileInputStream(files[1]));
//Bridge = new Image(new FileInputStream(files[2]));
Builder = new Image(new FileInputStream(files[3]));
ChainOfResponsibility = new Image(new FileInputStream(files[4]));
Command = new Image(new FileInputStream(files[5]));
Composite = new Image(new FileInputStream(files[6]));
Decorator = new Image(new FileInputStream(files[7]));
Facade = new Image(new FileInputStream(files[8]));
FactoryMethod = new Image(new FileInputStream(files[9]));
//Flyweight = new Image(new FileInputStream(files[10]));
Interpreter = new Image(new FileInputStream(files[11]));
Iterator = new Image(new FileInputStream(files[12]));
Mediator = new Image(new FileInputStream(files[13]));
Memento = new Image(new FileInputStream(files[14]));
Observer = new Image(new FileInputStream(files[15]));
Prototype = new Image(new FileInputStream(files[16]));
// Proxy = new Image(new FileInputStream(files[17]));
Singleton = new Image(new FileInputStream(files[18]));
State = new Image(new FileInputStream(files[19]));
Strategy = new Image(new FileInputStream(files[20]));
TemplateMethod = new Image(new FileInputStream(files[21]));
Visitor = new Image(new FileInputStream(files[22]));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
//AbstractFactory = new ImageView();
Adapter = new ImageView();
//Bridge = new ImageView(Bridge);
/*Builder = new ImageView(Builder);
ChainOfResponsibility = new ImageView(ChainofResponsibility);
Command = new ImageView(Command);
Composite = new ImageView(Composite);
Decorator = new ImageView(Decorator);
Facade = new ImageView(Facade);
FactoryMethod = new ImageView(FactoryMethod);
Flyweight = new ImageView(Flyweight);
Interpreter = new ImageView(Interpreter);
Iterator = new ImageView(Iterator);
Mediator = new ImageView(Mediator);
Memento = new ImageView(Memento);
Observer = new ImageView(Observer);
Prototype = new ImageView(Prototype);
Proxy = new ImageView(Proxy);
Singleton = new ImageView(Singleton);
State = new ImageView(State);
Strategy = new ImageView(Strategy);
TemplateMethod = new ImageView(TemplateMethod);
Visitor = new ImageView(Visitor);
*/
}
public void ReadText() throws IOException {
try {
BufferedReader in = new BufferedReader(new FileReader("gof.txt"));
StringBuilder strBuilder = new StringBuilder();
String str;
while ((str = in.readLine()) != null) {
textarea.appendText(str + "\n");
}
in.close();
} catch (IOException e) {
e.toString();
}
textfield.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.ENTER) {
String text = textarea.getText();
Labeled errorText = null;
if (textfield.getText() != null && !textfield.getText().isEmpty()) {
int index = textarea.getText().indexOf(textfield.getText());
textarea.getText();
if (index == -1) {
errorText.setText("Search key Not in the text");
} else {
// errorText.setText("Found");
textarea.selectRange(index, index + textfield.getLength());
}
} else {
errorText.setText("Missing search key");
}
}
}
});
}
public Pane getPane() {
return this.root;
}
}