Component Position (Vaadin) - java

I have a simple portlet with a few components : 3 Button objects, 1 Slider, 1 MenuBar and a picture assigned to a Label (generated by servlet). Now when I switch between pictures for a Label (I have more of them), I want the picture Label to be placed at the old picture Label object's position:
My picture Label is in the left corner of the portlet. The Button objects, MenuBar, and the Slider are under the picture Label when I select another picture Label the new picture Label is being drawn under the other components (under the Button objects , MenuBar , Slider ) so the Button objects... are top and the picture Label is at the bottom of the portlet
for example, I change the background of the picture Label by selecting the color in the menu :
newItem1.addItem("Blue",new Command(){
public void menuSelected(MenuItem selectedItem){
if(pictureA.isVisible()){
pictureB.setVisible(false);
pictureC.setVisible(false);
window.removeComponent(pictureA);
pictureA= new Label("<img src=http://localhost:8888/portlet/KiviatDiagramm?background=blue", Label.CONTENT_XHTML);
window.addComponent(pictureA);
} else {
window.showNotification("", Notification.TYPE_WARNING_MESSAGE);
}
}
});
UPDATE :
I have switched from Label objects to embedded images (Embedded) (which is a lot better) I have tried to reassign the resource on the Embedded object with the new color but it doesn't work, here is what I've done :
public void init() {
URL PictureAUrl= null;
try {
pictureAUrl= new URL("http://localhost:8888/portlet/pictureA");
} catch (MalformedURLException e) {
e.printStackTrace();
}
URL PictureBUrl= null;
try {
pictureAUrl= new URL("http://localhost:8888/portlet/pictureB");
} catch (MalformedURLException e) {
e.printStackTrace();
}
URL pictureCUrl= null;
try {
pictureCUrl= new URL("http://localhost:8888/portlet/pictureC");
} catch (MalformedURLException e) {
e.printStackTrace();
}
final Embedded pictureA = new Embedded("", new ExternalResource(pictureAURL));
pictureA .setType(Embedded.TYPE_IMAGE);
final Embedded pictureB = new Embedded("", new ExternalResource(pictureBURL));
pictureB .setType(Embedded.TYPE_IMAGE);
final Embedded pictureC = new Embedded("", new ExternalResource(pictureCURL));
pictureC .setType(Embedded.TYPE_IMAGE);
newItem.addItem("ColorBlue", new Command(){
public void menuSelected(MenuItem selectedItem) {
if(!pictureA.equals(pictureB )){
Resource newPictureResource = new ExternalResource("http://localhost:8888/portlet/pictureA?background=blue");
newPictureResource.setType(Embedded.TYPE_IMAGE);
pictureA.setResource(newPictureResource);
}
else {
window.showNotification("Please select pictureA");
}
}
});

rickthomas is correct, you should use the replaceComponent method. I'm pretty sure that the main problem here is that after you have removed the picture, you call addComponent(pictureA) which actually adds the component to the end of the component list. If you don't have a reference to the old picture and it's the first component, then you can use this:
window.replaceComponent(window.getComponentIterator().next(), newPicture);
In addition to that, you don't have to write HTML to show images. You can use Embedded.
If the images are in your classpath, you can use the following:
Embedded newPicture = new Embedded("", new ClassResource("my-picture.png", myApplication));
newPicture.setType(Embedded.TYPE_IMAGE);
window.replaceComponent(oldPicture, newPicture);
If they are found somewhere else, use this:
URL url = new URL("http://localhost:8888/portlet/KiviatDiagramm?background=blue");
Embedded newPicture = new Embedded("", new ExternalResource(url));
newPicture.setType(Embedded.TYPE_IMAGE);
window.replaceComponent(oldPicture, newPicture);
This might solve your problem.

Looking at the Vaadin API javadoc,
I found this
public void replaceComponent(Component oldComponent,Component newComponent)
I haven't tested it... but it should work.

Related

Getting ImageIcon- image path in runtime

I'm trying to get the image file path or file name of an ImageIcon. I've created a screen in my Java gui app, which contains properties for a custom JButton (extends JButton). From that screen I'm setting some of the main button properties, as if it is enabled, focusable and its ImageIcon. The problem is that as if now, whenever I use this button class, which is in every screen btw, I'm loading all possible images for ImageIcons when the exdened JButton class is used. That causes the screen to freeze before any of the components are shown, while the images are loaded from classpath. In order to change that, in the settings screen, where I hava a JComboBox, containing all images for icons, there, at least that's what I can think of, should be a way to get only the name of the chosen ImageIcon- image path.
The buttons properties are stored in a properties file and that's where I intend to store the .png images names if I can get to them. The idea is to set the image name and when the button is loaded to the screen to look for and load only the image it's supposed to.
Here is a snipet of the button class now; The images are way more, but for demo purposes, I think those are enough. I'd be very grateful if anyone can help with this matter.
public class CustomButton extends JButton {
static Properties FieldProp;
public CustomButton (String text, String name) {
FieldProp = new LoadProperties().loadMainProp();
this.setText(text);
this.setName(name);
Image imgdel=null;
Image imgsmbl=null;
Image imgsmrd=null;
Image imgsmgr=null;
Image imgadd=null;
Image imgauto=null;
Image imgauto1=null;
Image imgavail=null;
Image imgbarc=null;
Image imgdb=null;
Image imgdoc=null;
Image imgexc=null;
Image imgexc1=null;
try {
imgdel = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/delete.png")));
imgsmbl = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/small_blue.png")));
imgsmrd = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/small_red.png")));
imgsmgr = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/small_green.png")));
imgadd = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/add_plus.png")));
imgauto = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/automation.png")));
imgauto1 = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/automation1.png")));
imgavail = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/available.png")));
imgbarc = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/barcode.png")));
imgdb = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/db.png")));
imgdoc = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/doc.png")));
imgexc = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/excel.png")));
imgexc1 = ImageIO.read(Objects.requireNonNull(getClass().getResource("/img/import.png")));
} catch (NullPointerException e) {
JOptionPane.showMessageDialog(null,e+"\n"+e.getMessage()+"\n"+ Arrays.toString(e.getStackTrace()),"",JOptionPane.ERROR_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(null,e+"\n"+e.getMessage()+"\n"+ Arrays.toString(e.getStackTrace()),"",JOptionPane.ERROR_MESSAGE);
}catch (Exception e) {
JOptionPane.showMessageDialog(null,e+"\n"+e.getMessage()+"\n"+ Arrays.toString(e.getStackTrace()),"",JOptionPane.ERROR_MESSAGE);
}
ImageIcon delIcon = new ImageIcon(imgdel);
ImageIcon blIcon = new ImageIcon(imgsmbl);
ImageIcon rdIcon = new ImageIcon(imgsmrd);
ImageIcon grIcon = new ImageIcon(imgsmgr);
ImageIcon addIcon = new ImageIcon(imgadd);
ImageIcon autoIcon = new ImageIcon(imgauto);
ImageIcon autoIcon1 = new ImageIcon(imgauto1);
ImageIcon availIcon = new ImageIcon(imgavail);
ImageIcon barcIcon = new ImageIcon(imgbarc);
ImageIcon dbIcon = new ImageIcon(imgdb);
ImageIcon docIcon = new ImageIcon(imgdoc);
ImageIcon excIcon = new ImageIcon(imgexc);
ImageIcon excIcon1 = new ImageIcon(imgexc1);
Object[] items =
{
noIcon,
delIcon,
blIcon,
rdIcon,
grIcon,
addIcon,
autoIcon,
autoIcon1,
availIcon,
barcIcon,
dbIcon,
docIcon,
excIcon,
excIcon1
};
try {
int iconPosition = Integer.parseInt(FieldProp.getProperty(this.getName() + "Icon"));
String iconProp = FieldProp.getProperty(this.getName() + "Icon");
if (!iconProp.equals("0")) {
this.setIcon((ImageIcon) items[iconPosition]);
}
}catch (Exception e){
e.printStackTrace();
}
try {
this.setEnabled((Boolean.parseBoolean(FieldProp.getProperty(this.getName() + "Enabled"))));
this.setFocusable((Boolean.parseBoolean(FieldProp.getProperty(this.getName() + "Focusable"))));
}catch(Exception e){}

How to embed the broadcast from the camera in the container in Codename1?

In my application there is a video button. Here's the code.
#Override
protected void onGUI1_Button1Action (Component c, ActionEvent event){
try {
String value = Capture.captureVideo();
if (value != null) {
final Form previous = Display.getInstance().getCurrent();
Form preview = new Form("Preview");
preview.setLayout(new BorderLayout());
MediaPlayer pl = new MediaPlayer();
if (!value.startsWith("file:/")) {
value = "file:/" + value;
}
pl.setDataSource(value);
preview.addComponent(BorderLayout.CENTER, pl);
preview.setBackCommand(new Command("Back") {
public void actionPerformed(ActionEvent evt) {
previous.showBack();
}
});
preview.show();
}
} catch (Exception ex) {
Log.e(ex);
Dialog.show("Error", "" + ex, "OK", null);
}
}
I picked up this code from github. I don't want to broadcast the video on full screen. I need a video from camera was built into some container. That container must cover only part of the screen. I have built a GUI and put a container(Media Player) into some part of screen.
How to change the code for this purpose?
You can place a camera view finder right into your app with a new cn1lib: https://github.com/codenameone/CameraKitCodenameOne
Overlaying native widgets has been possible for a year or so by now.
Original answer which was correct when it was written is below:
Embedding camera or overlaying component on the preview screen is not yet available in codename one.
This could be done using native interface with peer component. Have a look at how Native map was implemented here

How to display graph generated with graphstream inside a JavaFX GUI Component?

I've been trying to display Graph generated with GraphStream in a GUI which is made using JavaFX.Here is what I have so far
Graph graphInit = new Graph(); //class that handles graph population
SingleGraph graph = new SingleGraph("Graph"); //GraphStream Class
SwingNode graphViewer = new SwingNode(); //JavaFX Component to display Swing elements
graph = graphInit.genGraph(graph);
Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
View view = viewer.addDefaultView(false); // false indicates "no JFrame".
graphViewer.setContent((JComponent) view);
graphHook.getChildren().add(graphViewer);
The graphViewer is a SwingNode Component.
I think GraphStream doesn't support JavaFX yet, but I'm not sure. Also, I found gs-fx which I don't know how to use.
Any help would be appreciated. If there is some other library like GraphStream to generate the graph and display it in GUI, that would work too.
public void imagesie(Graph g){
FileSinkImages pic = new FileSinkImages(OutputType.PNG, Resolutions.HD1080);
g.display();
pic.setLayoutPolicy(LayoutPolicy.COMPUTED_FULLY_AT_NEW_IMAGE);
try {
pic.setAutofit(true);
pic.writeAll(g, "sample.png");
File file = new File("sample.png");
Image image = new Image(file.toURI().toString());
imageview.setImage(image);
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}

Load image to ImageView JavaFX

I would like to display image(saved in project folder) in dialog window, but when I run my method showDialogWithImage I get FileNotFoundExcpetion: imgs\pic1.jpg (The system cannot find the file specified), although the image is located there.
I have tried load image on this way too:
Image image = new Image(getClass().getResourceAsStream(path));, but got the same problem.
Are there some others possibilities to load image to ImageView ?
Thank you for help!
My Java code is located in src\myProject\gui in project folder.
path="imgs\pic1.jpg" // imgs is located in project folder
public void showDialogWithImage(String path) {
final Stage dialogStage = new Stage();
logger.info(path);
InputStream is = null;
try {
is = new FileInputStream(path); // here I get FileNotFoundException
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Image image = new Image(is);
ImageView view = new ImageView();
view.setImage(image);
Button btnOK = new Button("OK");
btnOK.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
dialogStage.close();
}
});
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create()
.children(view, btnOK).alignment(Pos.CENTER)
.padding(new Insets(35)).build()));
dialogStage.show();
}
getClass().getResourceAsStream(path) will start its file search from the location of the calling class. So by using this path "imgs\pic1.jpg", you're saying this is your file structure
src\myProject\gui\imgs\pic1.jpg
To have the search traverse back, you need the extra separator before imgs. So
"\imgs\pic1.jpg"
Also, I think when you use a back slash as separator, you need to escape it. So
"\\imgs\\pic1.jpg
Or just use forward slash
"/imgs/pic1.jpg
Another option is to use a class loader, that will search from the root, where you don't need the beginning separator
getClass().getClassLoader().getResourceAsStream("imgs/pic1.png");
when you load an image with path you need replace seperator file "\" with "/" for example
String ImageName="MyImage.png";
File file = new File("src\\Messages\\Attachements\\Images");
try {
if (!file.exists()) {
FileUtils.forceMkdir(file);
}
}
} catch (Exception io) {
io.printStackTrace();
}
Image image = new Image("/Messages/Attachements/Images/"+ImageName=);
ImageReceived.setImage(image);

Change jLabel icon

It seems like i'm not the only one with that question but I can't find an answer that solves the problem.
I created a Label and assign an Icon to it using WYSIWYG interface designer.
Now I want to change the icon dynamically during runtime.
The logic way would be like this (my first attempt) :
ImageIcon newIcon = new ImageIcon("SomePath");
jLabel1.setIcon(newIcon);
When I do this the Icon simply disapears from the interface so I googled it and someone said to "flush" the icon whatever this means I tried it :
ImageIcon newIcon = new ImageIcon("SomePath");
newIcon.getImage().flush();
jLabel1.setIcon(newIcon);
Still having the same problem.. The icon disapears.
What am I doing wrong ?
Update (Full Method) :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
attempted = myEngine.Attempt('q', word);
if(attempted)
{
this.jTextArea1.setText(myEngine.ChangeEncrypt('q', word, this.jTextArea1.getText()));
}
else
{
JOptionPane.showMessageDialog(null,"The Letter Q is not in the word", "Error",JOptionPane.WARNING_MESSAGE);
jButton1.setEnabled(false);
life ++;
ImageIcon newIcon = myEngine.UpdatePicture(life);
newIcon.getImage().flush();
jLabel1.setIcon(newIcon);
}
This is the UpdatePicture Method :
public ImageIcon UpdatePicture(int life)
{
ImageIcon emptyIcon = new ImageIcon();
if (life == 0)
{
ImageIcon iconZero = new ImageIcon("/hang0.gif");
return iconZero;
}
if (life == 1)
{
ImageIcon iconOne = new ImageIcon("/hang1.gif");
return iconOne;
}
if (life == 2)
{
ImageIcon iconTwo = new ImageIcon("/hang2.gif");
return iconTwo;
}
if (life == 3)
{
ImageIcon iconThree = new ImageIcon("/hang3.gif");
return iconThree;
}
if (life == 4)
{
ImageIcon iconFour = new ImageIcon("/hang4.gif");
return iconFour;
}
if (life == 5)
{
ImageIcon iconFive = new ImageIcon("/hang5.gif");
return iconFive;
}
if (life == 6)
{
ImageIcon iconSix = new ImageIcon("/hang6.gif");
return iconSix;
}
return emptyIcon;
}
Not sure the whole code was necessary but still it might help.
The life variable starts at 0.
I checked and in the UpdatePicture it hits the "/hang1.gif"; and returns it.
If the file is in your src folder then :
ImageIcon ii = new ImageIcon(getClass().getResource("/myFile.gif"));
You really should not put that slash before the name of your icon. It works like that.
I know this is an old question thread, but was having a hell of a time getting a icon replaced on a jLabel when my app was running. This is what finally fixed it:
I created a new folder in the source package named images and put the images in there.
In my Frame (main class) I added this below the initComponents method:
private void moreComponents() {
// get images for initial screen prompts (Skd2_startPanel, loading label1).
// StartPanel is a panel on the Frame that has the loading label on it))
try {
lcImage= new ImageIcon(ImageIO.read(getClass().getResource("/images/LC.png")));
clImage= new ImageIcon(ImageIO.read(getClass().getResource("/images/CL.png")));
} catch (IOException ex) {
Logger.getLogger(Skd2_Frame.class.getName()).log(Level.SEVERE, null, ex);
}
}
LC and CL.png are the images I want as the label icons.
In a different class I added the following when I wanted the icon to change:
loadingLabel1.setIcon(lcImage); // or clImage as needed.
You'll need the following imports:
import java.util.logging.Logger;
import javax.imageio.ImageIO;
And add these two declarations just below the variables declarations:
static ImageIcon lcImage;
static ImageIcon clImage;
Hope this helps someone searching the web for an answer.

Categories