Using a pre-loaded icon - java

I'm using the following to show a cross in a label, that accompanies an if statement.
JLabel.setIcon(new ImageIcon(path + "Resource/cross.png"));
Instead of loading the icon everytime I would prefer to have it imported into my project and call it from there. I know how to import it but how do I modify the line of code above to point to the imported icon.

you can create a static loader methode (in an utility class) and store it there
private static Map<String, Icon> lookUpMap = new HashMap<>();
public static Icon getImageIcon(String res){
Icon icon = lookUpMap.get(res);
if(icon = null){
icon = new ImageIcon(res);
lookUpMap.put(res, icon);
}
return icon;
}
you can access the icons now everywhere
JLabel.setIcon(UtilitClass.getImageIcon(path + "Resource/cross.png));

Related

JavaFX image path for style

I'm currently trying to make a custom button using JavaFX. I have defined 2 styles that contain 2 different images for 2 button states(Pressed and Free). I'm using Intellij Idea and when defining the paths it shows no error's, but the button doesn't show up. Its just transparent, but I can click it. I have tried specified different paths, but haven't gotten any result. Here is the code where I define the styles and my file tree. Thanks!
public class CustomButton extends Button {
private final String FONT_PATH = "src/Resources/GUI/pixelFont.ttf";
private final String BUTTON_PRESSED_STYLE = "-fx-background-color: transparent;" +
" -fx-background-image: url('../../Resources/GUI/Buttondown.png');";
private final String BUTTON_FREE_STYLE = "-fx-background-color: transparent;" +
" -fx-background-image: url('../../Resources/GUI/Buttonup.png');";
public CustomButton(String text) {
setFont();
setPrefWidth(180);
setPrefHeight(53);
setText(text);
setStyle(BUTTON_PRESSED_STYLE);
initButtonListeners();
}
}
File tree
Moving all of the files into the pre-generated by Intellij Idea resources folder and then using that folder solved the issue.

RTL Support Using Vaadin 14

I trying to build web app in Hebrew.
but all of the components or Navbar are LTR.
how can I make my NavBar or all my site to be RTL?
onemore question can I change the style of the navbar?
#Viewport("width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes, viewport-fit=cover")
#Theme(Lumo.class)
#Route
#PWA(name = "SimpleIT", shortName = "SimpeIT")
public class MainView extends AppLayout {
public static final String ITM_DASHBOARD = "DashBoard";
private Map<Tab, Component> tab2Workspace = new HashMap<>();
public MainView() {
// setPrimarySection(Section.DRAWER);
Image img = new Image("https://i.imgur" +
".com/GPpnszs.png", "Vaadin Logo");
img.setHeight("75px");
addToNavbar(new MenuBar(), img);
Tabs menu = new Tabs(dashBoard()
,new Tab("Customers"),new Tab("Todo"),new Tab("Tickets"));
menu.setOrientation(Tabs.Orientation.HORIZONTAL);
menu.addSelectedChangeListener(event -> {
final Tab selectedTab = event.getSelectedTab();
final Component component = tab2Workspace.get(selectedTab);
setContent(component);
});
addToNavbar(menu);
this.setPrimarySection(Section.NAVBAR);
setContent(new Span("click in the menu ;-) , you will see me never again.."));
}
private Tab dashBoard() {
final Span label = new Span("DashBoard");
final Icon icon = DASHBOARD.create();
final Tab tab = new Tab(new HorizontalLayout(icon,label));
tab2Workspace.put(tab, new DashBoardView());
return tab;
}
}
You can turn RTL by adding the CSS rule direction:rtl to the body. Alternatively you can use the RTL mode add-on that does that for you: https://vaadin.com/directory/component/rtl-mode/discussions
Many of the component work in RTL mode, but some have still some issues. They will be hopefully fixed in the first half of 2020.
Updated information: RTL is officially supported since Vaadin 14.3 (LTS) and Vaadin 17: https://vaadin.com/blog/localization-gets-an-update-with-right-to-left-rtl-support

Image referencing inside a NB project?

Ok, I am not sure what I am doing wrong here but I am not able to reference images in my NB Java project. My code is basically the same as this:
ImageIcon i = new ImageIcon("Resources/character.png");
I Have a package called Resources in my src, and character.png in that package, so what am I doing wrong?
If your file is within the class path (eg. <project>/src/Resources/character.png), load it as resource:
Here's an example:
public class Example
{
public ImageIcon loadImage()
{
// Note the '/'
final String path = "/Resources/character.png";
// Load the image as a resource
ImageIcon icon = new ImageIcon(this.getClass().getResource(path));
// Return the result
return icon;
}
}
// ...
Example e = new Example();
ImageIcon icon = e.loadImage();

Paths of the different elements in an RCP application

I'm using eclipse to created an RCP Application, and I'm not being able to load an image because I don't know how to find it in the generated code. I'm going to try to explain my particular issue.
Note: the project is a Game editor, and it is located here: http://chelder86.github.com/ArcadeTongame/
Firstly, this is the project structure:
The next code runs the RCP application correctly inside Eclipse, after changing the Working Workspace in the Eclipse Running Config.
package figures;
(...)
public class Sound extends ImageFigure {
public Sound() {
String picturePath = "src/figures/Sound48.png";
// or String picturePath = "bin/figures/Sound48.png";
Image image = new Image(null, picturePath);
this.setImage(image);
}
}
But it does not work when I create a Product and export it as an RCP Application. I mean, the RCP application works, but it does not load that image.
Note: build.properties has the image checked.
I tried different combinations like these with the same result: java.io.FileNotFoundException, when I run it in Eclipse:
package figures;
(...)
public class Sound extends ImageFigure {
public Sound() {
String picturePath = getClass().getResource("Sound48.png").getPath();
// or String picturePath = this.getClass().getClassLoader().getResource("bin/figures/Sound48.png").getPath();
// or String picturePath = this.getClass().getClassLoader().getResource("figures/Sound48.png").getHost();
// or similars
Image image = new Image(null, picturePath);
this.setImage(image);
}
}
How could I load it correctly?
Thanks for any help! :)
Carlos
Try creating a separate "figures" folder alongside "icons" folder. Put only the image files there, not .java files. Don't forget to add it to the class path and to build.properties. Then something like this should work:
InputStream in = getClass().getResourceAsStream("figures/Sound48.png");
Image image = new Image(Display.getDefault(), in);

How do I change the default application icon in Java?

I'm using NetBeans, trying to change the familiar Java coffee cup icon to a png file that I have saved in a resources directory in the jar file. I've found many different web pages that claim they have a solution, but so far none of them work.
Here's what I have at the moment (leaving out the try-catch block):
URL url = new URL("com/xyz/resources/camera.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
getFrame().setIconImage(img);
The class that contains this code is in the com.xyz package, if that makes any difference. That class also extends JFrame. This code is throwing a MalformedUrlException on the first line.
Anyone have a solution that works?
java.net.URL url = ClassLoader.getSystemResource("com/xyz/resources/camera.png");
May or may not require a '/' at the front of the path.
You can simply go Netbeans, in the design view, go to JFrame property, choose icon image property, Choose Set Form's iconImage property using: "Custom code" and then in the Form.SetIconImage() function put the following code:
Toolkit.getDefaultToolkit().getImage(name_of_your_JFrame.class.getResource("image.png"))
Do not forget to import:
import java.awt.Toolkit;
in the source code!
Or place the image in a location relative to a class and you don't need all that package/path info in the string itself.
com.xyz.SomeClassInThisPackage.class.getResource( "resources/camera.png" );
That way if you move the class to a different package, you dont have to find all the strings, you just move the class and its resources directory.
Try This write after
initcomponents();
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Your image address")));
/** Creates new form Java Program1*/
public Java Program1()
Image im = null;
try {
im = ImageIO.read(getClass().getResource("/image location"));
} catch (IOException ex) {
Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex);
}
setIconImage(im);
This is what I used in the GUI in netbeans and it worked perfectly
In a class that extends a javax.swing.JFrame use method setIconImage.
this.setIconImage(new ImageIcon(getClass().getResource("/resource/icon.png")).getImage());
You should define icons of various size, Windows and Linux distros like Ubuntu use different icons in Taskbar and Alt-Tab.
public static final URL ICON16 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug16.png");
public static final URL ICON32 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug32.png");
public static final URL ICON96 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug96.png");
List<Image> images = new ArrayList<>();
try {
images.add(ImageIO.read(HelperUi.ICON96));
images.add(ImageIO.read(HelperUi.ICON32));
images.add(ImageIO.read(HelperUi.ICON16));
} catch (IOException e) {
LOGGER.error(e, e);
}
// Define a small and large app icon
this.setIconImages(images);
You can try this one, it works just fine :
` ImageIcon icon = new ImageIcon(".//Ressources//User_50.png");
this.setIconImage(icon.getImage());`
inside frame constructor
try{
setIconImage(ImageIO.read(new File("./images/icon.png")));
}
catch (Exception ex){
//do something
}
Example:
URL imageURL = this.getClass().getClassLoader().getResource("Gui/icon/report-go-icon.png");
ImageIcon iChing = new ImageIcon("C:\\Users\\RrezartP\\Documents\\NetBeansProjects\\Inventari\\src\\Gui\\icon\\report-go-icon.png");
btnReport.setIcon(iChing);
System.out.println(imageURL);

Categories

Resources