Image doesn't show in image button - java

I have create an image button but it didn’t show the image. The file is on src\MyPackage folder. How can I map it?
There is my code:
jpAnnotation=new JPanel();
jpAnnotation.setLayout(new FlowLayout(FlowLayout.LEADING));
JButton btnUnderline =new JButton(new ImageIcon ("UnderlineIcon.gif"));
btnUnderline.setSize(50, 260);
btnUnderline.setAlignmentX(JButton.LEFT_ALIGNMENT);
btnUnderline.setHorizontalAlignment(JButton.LEFT);
btnUnderline.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0){
ActionEvent ae = new ActionEvent(bean, 0, "Underline");
bean.actionPerformed(ae);
}
});
jpAnnotation.add(btnUnderline);

Just a little code snippet:
btnUnderline.setIcon(
new ImageIcon(getClass().getResource("/path/to/UnderlineIcon.gif")));
Brief explanation
Using this statement for loading your image, you don't have to care about the right URL to your file, because you automatically get the correct URL.
This is based on loading the resource from the class path and not from the filesystem path!

Try this:
btnUnderline.setIcon( new ImageIcon( "C:\\YourFolder\src\MyPackage\UnderlineIcon.gif" ) );
If of course you're using Windows. Alternatively you can move the gif to the same directory as where you're executing your code from.

Related

How coud I create these kind of Button using Java Swing?

I want to create button without any borders or shadow, but an icon instead using java swing component. How can I accomplish this?
Real Button
JButton btnNewButton = new JButton("");
btnNewButton.setContentAreaFilled(false);
btnNewButton.setBorderPainted(false);
btnNewButton.setBorder(null);
btnNewButton.setIcon(new ImageIcon(path));
This will give you a real button without any borders around the given image to work with. Note that in this state the button doesn't have a "click animation" anymore. For such an animation you could use the .setSelectedIcon(selectedIcon);
Clickable Image
ImageIcon img = new ImageIcon(path);
JLabel button = new JLabel(img);
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
//Set pressed or something else
}
});
But this one just provide you a clickable Image and should only be used when a clickable Image without any other intentions is needed.
Note that this way is just a workaround.

image/icon use a relative path

I don't really get how to properly use a relative path in Eclipse. I created a res folder next to the src folder with a image folder in it.
This is my current code:
public class ToolbarView extends JToolBar {
JButton addButton = new JButton("\\images\\button.png");
Try doing this
JButton addButton = new JButton(new ImageIcon(getClass().getResource("images\button.png")));
You can create an icon using getClass().getResource(...) like this :
Icon icon = new ImageIcon(getClass().getResource("/images/button.png"));
JButton b = new JButton(icon);
Based on the forum instead of writing down /res/image.png you just have to write /image.png .It worked for me. (The image was in the src/main/resources folder by the way)
you could write it like this instead.
JButton addButton = new JButton(new ImageIcon("images/button.png"));

Why does this simple code not work

I am having trouble with this simple example code from the book. It is supposed to represent the same image 2 times in one window (north and south labels), one above the other. When I run it, it displays this instead of this (I am sorry for not cutting the images or resizing them) Below is my code. I am running Eclipse Juno on Ubuntu 13.04.
package gui;
import java.awt.BorderLayout;
import javax.swing.*;
public class Gui {
public static void main(String[] args) {
JLabel northLabel = new JLabel ( "North" );
ImageIcon labelIcon = new ImageIcon ("GUItip.gif");
JLabel centerLabel = new JLabel (labelIcon);
JLabel southLabel = new JLabel (labelIcon);
southLabel.setText("South");
JFrame application = new JFrame();
application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
application.add(northLabel, BorderLayout.NORTH);
application.add(centerLabel, BorderLayout.CENTER);
application.add(southLabel, BorderLayout.SOUTH);
application.setSize(300, 300);
application.setVisible(true);
}
}
You need to concentrate on the following statement:
ImageIcon labelIcon = new ImageIcon ("GUItip.gif");
When initiating new ImageIcon.. it searches the provided address in execution folder by default i.e. in this case "GUItip.gif" shall be searched within workspace/user directory.
One solution is to make available GUItip.gif image in you workspace (program execution) folder.
Another solution would be to provide absolute path.. eg.
C:\USER\Workspace\project_name\GUItip.gif
Though a better approach would be to create a specific folder where you save all images used in your project. Create a final static String variable with absolute path to your folder. Now it would be easy for any programmer in that project to know where to look for images.
There are good approaches to use this mapping.. through XML to be loaded in the beginning.. through resourcebundle etc but that is a different topic altogether.
The image probably isn't loading properly. Try using a try/catch block to see if that's the case.
Ex:
Image img;
File f = new File(//image url);
try {
img = ImageIO.read(f);
} catch (IOException e) {
String curr_dir = System.getProperty("user.dir");
throw new IllegalArgumentException("Image could not be found from " + curr_dir);
}
ImageIcon labelIcon = new ImageIcon(img);

How to drag and drop files to copy into directory?

I have this label:
//---- label5 ----
label5.setText("Drag and drop your texture\npack in the box *.zip files");
label5.setIcon(new ImageIcon(
"E:\\Chituri\\MBMLauncherJava\\src\\resursele\\dasdasdasd.png"));
But I want to make something like this:
//---- label5 ----
label5.setText("Drag and drop your texture\npack in the box *.zip files");
label5.setIcon(new ImageIcon(
"E:\\Chituri\\MBMLauncherJava\\src\\resursele\\dasdasdasd.png"));
// drag files in the label >
// automatically move files in particular folder (E:\\Chituri)
How I can do this?
i try this...
label5.setTransferHandler(transfer);
label5.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
JLabel texturidrag = (JLabel)e.getSource();
TransferHandler handle = texturidrag.getTransferHandler();
handle.exportAsDrag(texturidrag, e, TransferHandler.COPY);
}
});
Try to read this post, especially answer and implement method processFiles to filter input files and handle just images, if you found image just create ImageIcon as you want.

Size of JFilechooser

I am trying to put a JFileChooser box on my GUI but if I just do this
JFileChooser filechooser = new JFileChooser ();
then it will just show a huge file selection window on the panel (I do not want that), so I want to make a small box filechooser (with a name for example "choose file") that when I click it, a window will popup, so then I can choose the file.
Use a button to open your file chooser and use setPreferredSize() method to make the file chooser smaller in size:
JButton button = new JButton("Choose a file!");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle( "Choose a file" );
fileChooser.setVisible( true );
fileChooser.setPreferredSize( new Dimension(100, 100) );
}
});
Call
filechooser.setPreferredSize (new java.awt.Dimension (800, 800));
before calling showOpenDialog with whatever Dimension you like.
But I would suggest to either maximize the Dialog, because in the moment I like to open a File, I don't like to watch something else - find a file, and close the dialog, without much scrolling, because somebody thought it looks more nice.
If you like to prevent wasting space, you can precalculate the needed size for the window, which might be a lot of work, but could pay off, if you use the Component frequently.

Categories