Adding java swing combobox to javafx application - swing

I am trying to create a javafx application witch contanis just a tab pane and javafx combo box , this is my fxml witch i created by scene builder :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="288.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TabPane layoutX="-2.0" prefHeight="400.0" prefWidth="288.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="tab1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<ComboBox layoutX="32.0" layoutY="39.0" prefHeight="26.0" prefWidth="86.0" />
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
and this is my simple Main just for call fxml
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author asus
*/
public class Main extends Application{
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene= new Scene(FXMLLoader.load(getClass().getResource("/javaapplication3/test.fxml")));
primaryStage.setScene(scene);
primaryStage.setX(0);
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
}
I am looking for a way to use swing combobox wich integerated with my fxml file and my application How can I do it , is there any way to use swing component in scene builder of fxml?

Related

Eexpand 2 more Titledpanes at once

On these days i am refactoring java swing code to javafx.
And about some jpanels, i just refactored as JFXPanel and that JFXPanels` root is VBOX. the structure of JFXPanel is VBOX -> BorderPane -> TitledPane -> AnchorPane.
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="93.0" prefWidth="295.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<BorderPane prefHeight="259.0" prefWidth="309.0">
<top>
<TitledPane animated="false" prefHeight="93.0" prefWidth="297.0" style="-fx-background-color: #eeeeee;" text="Compensation Options" textFill="#1b75bc">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="295.0" prefWidth="267.0">
<children>
<Button fx:id="mResetCountButton" layoutX="25.0" layoutY="5.0" mnemonicParsing="false" text="Reset Counts" textAlignment="CENTER" />
<Button fx:id="plotStatsButton" layoutX="123.0" layoutY="5.0" mnemonicParsing="false" text="Stats Panel" textAlignment="CENTER" />
<Button fx:id="gateStatsButton" layoutX="208.0" layoutY="5.0" mnemonicParsing="false" text="Gate State" textAlignment="CENTER" />
<Button fx:id="mLoadMatrixButton" layoutX="28.0" layoutY="37.0" mnemonicParsing="false" text="Load Matrix" textAlignment="CENTER" />
<Button fx:id="viewCompMatrix" layoutX="119.0" layoutY="37.0" mnemonicParsing="false" text="View Matrix" textAlignment="CENTER" />
<Label fx:id="mJCompMatrixCombo" layoutX="208.0" layoutY="41.0" text="Matrix Name" />
</children>
</AnchorPane>
</content>
</TitledPane>
</top>
</BorderPane>
</children>
</VBox>
And now i want to make JFXPanels as Accordion. When i searched in google i just saw some code which use and when i make Accordion tag, i just can use TitledPane below tag.
Is there any other way to make JFXPanels as Accordion reusing my code?...
And actually using Tag, i just can expand one titledPane at once.
How can i make JavaFX application with Accordion which can expand many titledPanes at once?
I just tried as make titledPanesclicklistener and resizing VBox which is wrapping titledPane and titledpanes size and some panes which in inside of titledpane.
such as
boolean titledPaneClicked = false;
TiteldPane.setOnAction((e)->{
if(titledPaneClicked){
TitledPane.setHeight(0);
Vbox.setHeight(0);
soemotherPanes.setHeight(0);
titledPaneClicked = !titledPaneClicked;
}else{
TitledPane.setHeight(previousSize);
Vbox.setHeight(previousSize);
soemotherPanes.setHeight(previousSize);
titledPaneClicked = !titledPaneClicked;
}
});
And it did not work. Plz help me :)
If your question is about how you can open close 2 TitledPanes at once, you can do so programmatically, for example by using a button :
Main.fxml (to make your code mcve always post the fxml name and imports)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<VBox minWidth="-Infinity" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="tests.xml.Controller">
<children>
<BorderPane>
<top>
<TitledPane fx:id="aTitledPane" animated="false" style="-fx-background-color: #eeeeee;" text="Compensation Options" textFill="#1b75bc">
<content>
<AnchorPane>
<children>
<Label text="Top pane expanded" />
</children>
</AnchorPane>
</content>
</TitledPane>
</top>
<center>
<TitledPane fx:id="bTitledPane" animated="false" style="-fx-background-color: #eeeeee;" text="Other Options" textFill="#1b75bc">
<content>
<AnchorPane>
<children>
<Label text="Bottom pane expanded" />
</children>
</AnchorPane>
</content>
</TitledPane>
</center>
<bottom>
<Button fx:id="openCloseButton" onAction="#openClose" text="Close" textAlignment="CENTER" />
</bottom>
</BorderPane>
</children>
</VBox>
And it controller:
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
public class Controller {
#FXML
private TitledPane aTitledPane, bTitledPane;
#FXML
private Button openCloseButton;
private final static String OPEN = "Open", CLOSE = "Close";
#FXML
void initialize(){
openCloseButton.setText(OPEN);
aTitledPane.setExpanded(false);
bTitledPane.setExpanded(false);
}
#FXML
private void openClose(){
System.out.println(openCloseButton.getText());
if(openCloseButton.getText().equals(OPEN)){
openCloseButton.setText(CLOSE);
aTitledPane.setExpanded(true);
bTitledPane.setExpanded(true);
}else{
openCloseButton.setText(OPEN);
aTitledPane.setExpanded(false);
bTitledPane.setExpanded(false);
}
}
}
Test it using :
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FXMLTest extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("xml/Main.fxml"));
primaryStage.setTitle("Hello World");
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(null);
}
}
If you want that opening / closing one TitledPane will open / close the other without using a button, remove the button from the fxml, and bind TitledPane.expandedProperty() of the two TitledPanes in the controller:
import javafx.fxml.FXML;
import javafx.scene.control.TitledPane;
public class Controller {
#FXML
private TitledPane aTitledPane, bTitledPane;
#FXML
void initialize(){
aTitledPane.expandedProperty().bindBidirectional(bTitledPane.expandedProperty());
}
}
I solved this problem just using VBox.
I made VBox, titledPane and VBox which contains some Btns, Labels, and so on.
VBox mainBox = new VBox();
TitledPane T1 = new TitledPane();
VBox content1 = new VBox();
T1.setContent(content1);
TitledPane T2 = new TitledPane();
VBox content2 = new VBox();
T2.setContent(content2);
mainBox.getChildren().addAll(T1, T2);
and then in MainVBox, titledPanes are shown like accordion.

Error on donwloading files from an external folder primefaces javax.servlet.ServletException

i'm using jsf 2.2 primefaces 6.0 and i implemented a solution to download pictures.The issue is that the solution works fine when i put the images on ressource file internally like that:enter image description here
but when i try to download it from an external repository using a link then an error message appears :enter image description here
Here the xhtml code:
<p:column style="text-align: center" headerText="Télécharger">
<p:commandButton value="Download" ajax="false"
onclick="PrimeFaces.monitorDownload(start, stop);"
icon="ui-icon-arrowthick-1-s">
<p:fileDownload value="#{fileDownloadView.file}" />
<f:setPropertyActionListener value="#{a}"
target="#{demandeBean.demandeSelectionnee}" />
</p:commandButton>
</p:column>
Here the java Bean code:
package mBeans;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
#ManagedBean
#SessionScoped
public class FileDownloadView implements Serializable{
private StreamedContent file;
public FileDownloadView() throws FileNotFoundException, TransformerConfigurationException, TransformerException {
InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(
"http://localhost:18080/openCars/images/hichem.jpg");
file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");
}
public StreamedContent getFile() {
return file;
}
}
I need help to solve the problem.

How to persist image in mysql database using Spring MVC and JPA

This is my imageForm.jsp:
%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title></title>
</head>
<body>
<h2>Image Form</h2>
<form:form method="POST" action="/showImage">
Picture: <input type="file" name="image">
<br />
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
This is showImage.jsp:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<h2>Show Image</h2>
<p>Profile Picture : ${image.image}</p>
</body>
</html>
And this is my controller:
package com.springapp.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
#RequestMapping(value = "/imageForm")
public ModelAndView showImageForm(Model model) {
return new ModelAndView("imageForm", "command", new Image());
}
#Transactional
#RequestMapping(value = "/showImage")
public ModelAndView showResult(#ModelAttribute("")Image image, ModelAndView model) {
model.setViewName("showImage");
System.out.println("Transaction");
em.persist(image);
System.out.println("persisted");
model.addObject("image", image);
return model;
}
}
This is the Image.java model class:
package com.springapp.mvc;
import javax.persistence.*;
#Entity
public class Image {
#Id
private int imageID;
private byte[] image;
public int getImageID() {
return imageID;
}
public void setImageID(int imageID) {
this.imageID = imageID;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
}
And the persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="NewPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.springapp.mvc.Image</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/advocatoree"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
I can choose an image from my PC and when I press the submit button, the showImage.jsp page shows me this: Profile Picture : [B#1c6a19f
The entry is persisted in database, but under the image attribute, it shows: [BLOB - 39 B]
If I click on it, I get a .bin file downloaded. I don't how I should approach this problem, can someone please help me?
the best way to do it - save the picture in any working directory using FileOutputStream, in db you can save unique picture name or path to it. Also you shall receive from the client byte[] in base64 format. The problem may be that you get an byte array represented as string (like "asd561$%#!"), then use Base64.getDecoder().decode(your_string).

strange things on mybatis :Mapped Statements collection does not contain value for

There is a very strange problem happened when i tested the code on mybatis + spring with junit4.
It is ok to run in normal way - jetty environment?
But when it run on junit, errors appear.
BaseTest.java
package com.test.spring.action;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
#ContextConfiguration(
{"classpath:application-context.xml"
})
#RunWith(SpringJUnit4ClassRunner.class)
#TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true)
public class BaseTest extends AbstractTransactionalJUnit4SpringContextTests{
}
UserAction.java
package com.test.spring.action;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.test.spring.service.UserService;
#Component
public class UserAction extends BaseTest{
Logger logger = Logger.getLogger(BaseTest.class);
#Autowired
UserService userService;
#Test
public void testInsertAccount() {
userService.getAll();
}
}
the xml configrations below:
application-context.xml
....
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.test.spring.entity"/>
<!-- mapper和resultmap配置路径 -->
<property name="mapperLocations">
<list>
<value>classpath:com/test/spring/mapper/*-mapper.xml</value>
</list>
</property>
</bean>
.....
user-mapper.xml
<mapper namespace="com.test.spring.mapper.UserMapper">
<select id="getAll" resultType="user">
<![CDATA[
select * from user
]]>
</select> ...others selects
Could anyone help me figure out what's wrong with it?
I can provide more detail if you want.
Thanks.

JavaFX start method everytime included tabcontent is called

How can I start a method every time a tab is called? I have a Main.fxml with a tabpane and two tabs, I've included a separate fxml for each tab (tab1.fxml, tab2.fxml).
Main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="434.0" prefWidth="428.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainController">
<children>
<TabPane prefHeight="434.0" prefWidth="428.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab fx:id="tab1" text="Tab 1">
<content>
<fx:include source="tab1.fxml" />
</content>
</Tab>
<Tab fx:id="tab2" onSelectionChanged="#addView" text="Tab 2">
<content>
<fx:include source="tab2.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
MainController.java
public class MainController implements Initializable{
#FXML private Tab tab1;
#FXML private Tab tab2;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
#FXML public void addView(){
}
}
Each FXML has a Label which should show how often the tab(content) was called. So if I click on tab ("tab2") the counter label should show "1" and increment by +1 every time I call this tab again. This should happen by using a method within the tab controllers.
tab1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="249.0" prefWidth="257.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.tab1Controller">
<children>
<Label fx:id="lbl_1" layoutX="92.0" layoutY="53.0" text="not clicked" />
</children>
</AnchorPane>
tab1Controller.java
public class tab1Controller implements Initializable{
#FXML public Label lbl_1;
private static int counter=0;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
public void addViewCounter(){
lbl_1.setText(""+counter);
counter++;
}
}
Tab2.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="249.0" prefWidth="257.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.tab2Controller">
<children>
<Label fx:id="lbl_2" layoutX="92.0" layoutY="53.0" text="not clicked" />
</children>
</AnchorPane>
tab2Controller.java
public class tab1Controller implements Initializable{
#FXML public Label lbl_2;
private static int counter=0;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
public void addViewCounter(){
lbl_2.setText(""+counter);
counter++;
}
}
I've already tried to solve this problem by using static methods and labels but i get NullPointerExceptions, seems like this doesn't work anymore with java 8.
Also I've tried to get the controller by using a FXMLloader ... this way I also get a NullPointerExceptions.
Isn't there an onCall method for the included fmxl or the anchorpane? Or maybe something that makes the controller initialize again.
Any other solutions or ideas?
Firstly, your counters should not be static. They belong to the controller instances, not the controller class.
You need three things:
A reference to the tabPane in the main controller
A reference to each of the tab controllers in the main controller.
A listener on the tabPane's selected tab, so that you can call a method when the selected tab changes
For the first, just do:
<TabPane fx:id="tabPane" prefHeight="434.0" ... >
in Main.fxml, and
#FXML private TabPane tabPane ;
in MainController.java.
For the second, you can use the "Nested Controller" technique.
You need to add an fx:id attribute to each of the fx:includes, and then just add references for the controllers in MainController.java. The rule is that if your fx:include has fx:id="x", then the controller from the corresponding FXML file can be injected into a variable with name xController. Here I have changed the class names for the controllers to Tab1Controller and Tab2Controller to follow the standard conventions (and avoid confusion).
In Main.fxml, change the fx:includes to include an fx:id:
<fx:include fx:id="tab1Content" source="tab1.fxml" />
<fx:include fx:id="tab2Content" source="tab2.fxml" />
In MainController.java:
#FXML private Tab1Controller tab1ContentController ;
#FXML private Tab2Controller tab2ContentController ;
Now in MainController's initialize() method just set up the listener:
public void initialize() {
tabPane.getSelectionModel().selectedItemProperty()
.addListener((obs, oldTab, newTab) -> {
if (newTab == tab1) {
tab1ContentController.addViewCounter();
} else if (newTab == tab2) {
tab2ContentController.addViewCounter();
}
});
}