Displaying barcode on TextField with FXML controller - javafx

please I need help in displaying the generated barcode on the textField shown above. Here is my code I tried.
#FXML
private void barcodePrev(ActionEvent event){
if(imgPre !=null)
imgPre.clear();
else
imgPre = inputChar;
System.out.print(imgPre);
}
This code of mine isn't working; Sorry img removed due reputation.Thanks in advance.

Related

Error using sub-string method with textfield

We are have an error when we try to obtain the first few character of a string
First we tried to remove the last two offending characters
That produced the same error we are seeing when we try to obtain the first few characters
The On Key Typed method is attached to a textfield when MAX character are exceeded a custom alert
is fired. We have looked at many ways to remove or get sub-strings in various SO questions before posting
The string entered will never be the same hence we do not know the specific character to replace
Here is the code and a screen shot of the ERROR notice that the "o" in front of the original text
The string we are entering is "This is a test to see how many yo"
We are trying to obtain only the "This is a test to see how many"
The System.out.println(strNew) is exactly that but when strNew is add to the textfield the "o" shows up
Our question is how to prevent this ?
OR What is the cause of the odd text that is palaced in the textfield?
Here is the Minimal Code to test
public class Atest extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("test.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
The Controller
public class testController implements Initializable {
#FXML
TextField txtDesc;
#FXML
private void handleButtonAction(ActionEvent event) {
txtDesc.setText("Thanks");
}
#FXML
private void descLEN(){
txtDesc.setOnKeyTyped(event ->{
int maxCharacters = 10;
if(txtDesc.getText().length() > maxCharacters)
event.consume();
});
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
We have no idea how to post FXML Code all you need is a TextField
with the id txtDesc and set the OnKeyTyped to descLEN
Because you are consumed with the Custom Alert you may not like our answer
I call this We Hear You Knocking but You Can NOT Come In Your event is consume
OK the answer suggested by Sedrick is great but we have only used 3 Lines of Code
No Custom Alert just 30 Character and a bunch of consuming ha ha
#FXML
private void onType(){
txtDescription.setOnKeyTyped(event ->{
int maxCharacters = 30;
if(txtDescription.getText().length() > maxCharacters)event.consume();
});
All right 7 lines if you count the FXML tab and the declaration and formatting

javafx/ fxml - Switching Screens/ add a new Tab in controller

Firstly, sorry for my English. I hope, you unterstand it.
Last Month I started developing a little programm with FXML/ JavaFX.
I have two screens, and I'm switching between them. This is not the problem.
The problem is: On one Screen i have a listview, where i can choose an item. When i have chosen an item, i want to open a new tab on the other screen with the content of the item. Therefore, i have to click on a button.
When i have chosen an item, i can print out the selected item, but how do I open a new tab on the other screen. Both Screens a two different FXML and are activited from the controller. How can I add a Tab, although loading fxml?
public class Controller{
#FXML
private ListView<String> lv;
#FXML
private Button check;
#FXML
public void projectview (Event e3) throws Exception{
Stage stage = null;
Parent root = null;
if(e3.getSource()==check){
//Check is the declaration for the Button on the screen with the listview
String projectview= lv.getSelectionModel().getSelectedItem();
stage = (Stage) check.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("FXML1.fxml"));
//Here I want to add a tab in FXML1.fxml
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
}
If something is missing or not clear, please ask. I read other similar questions, but i don't know, what to do.
Thanks for help

ObservableList to TableView javafx

I'm programming with a library that I don't know the code, only the methods, and I can't modify it. I tried to make a tableview of "flights" and it works but I don't know how to put a name(or ID) for each fly. Can someone help me? Thanks. Some code here:
public class StageController {
#FXML private TableView<Flight> flightsTable;
#FXML private TableColumn<Flight, String> flightColumn;
public void start(Airport air){
final AirportFlights a = Data.getInstance().getAirportFlights(air);
ObservableList<Flight> flights = FXCollections.observableArrayList(a.getArrivals().getFlights().values());
flightsTable.setItems(flights);
}
You need to declare a valueFactory for your tablecolumn. If you have a field name inside your Flight class then, you can do :
flightColumn.setCellValueFactory(
new PropertyValueFactory<Flight, String>("name"));

JavaFX FXML menuItem action

I'm trying to set a variable value when a MenuItem i chosen in a MenuButton object.
I've tried to search for this but I've came up empty handed.
Here's the code to set the MenuItems:
private ObservableList<MenuItem> templateMenuItems = FXCollections.observableArrayList();
#FXML private MenuButton menu = new MenuButton();
#FXML
protected void getTemplates() throws IOException {
CaspReturn tls = this.socket.runCmd(new Tls(""));
String tlsList = tls.getResponse();
String[] tlsListSplitt = tlsList.split("\\n");
for (int i = 0; i < tlsListSplitt.length; i++) {
String[] tlsLine = tlsListSplitt[i].split("\"");
this.templateMenuItems.add(new MenuItem(tlsLine[1]));
}
this.menu.getItems().setAll(this.templateMenuItems);
}
I'm not sure how to write the code to get the text from a menuItem or which field in scenebuilder the method should be in.
It's not clear what your asking, but I'll assume that you want to know the text of a menu item when it is clicked. To do that, you need to add an event handler onto the menu item. The following is clipped from the JavaDoc for ContextMenu:
MenuItem item1 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
You can get the event source, cast in to MenuItem and then get the text from that.
There's a real problem with your code the way it's written, though. You have calls to an external database in getTemplates, and as it's implemented as an #FXML element that almost guarantees that it'll be run on the FXAT, which is really, really bad.
I'd refactor that so that the database access is in a Task, and then the MenuItem creation is a handler for the onSucceeded event of the Task. Then you need instantiate a ContextMenu and install the MenuItem's on it in that event handler.
The getTemplates() method should be called as the onAction event for the button.

Scene Builder 2 JavaFX2 button issue

I feel like I'm missing something easy but I'm not sure what it is. Here is my code:
#FXML
private Label label;
private Button startGameButton;
#FXML
private void startGame (ActionEvent event) {
label.setText("Ok. Let's begin.");
startGameButton.setVisible(false);
}
I made a button in Scene Builder and set its fx:id to startGameButton. I'm just trying to make the startGameButton disappear when I click on it. I thought this would be sufficient but I'm getting a
Caused by: java.lang.NullPointerException
at myjavafx2.FXMLDocumentController.startGame(FXMLDocumentController.java:29)
I feel as though the NullPointerException is telling me the button doesn't exist but I made it in Scene Builder. I don't think I would need to do Button startGameButton = new Button(); because Scene Builder should be doing that for me. What am I missing?
You have to add #FXML before your Button as well.
Like this:
#FXML
private Label label;
#FXML
private Button startGameButton;
#FXML
private void startGame (ActionEvent event) {
label.setText("Ok. Let's begin.");
startGameButton.setVisible(false);
}

Resources