Menu
  • HOME
  • TAGS

How to set an ImageIcon to a JButton and resize the picture according to the button's size?

java,image,swing,imageicon

ImageIcon icon = ...; JButton b = ...; Image im = icon.getImage(); Image im2 = im.getScaledInstance(b.getWidth(), b.getHeight(), ...); b.setIcon(new ImageIcon(im2)); ...

Adding ImageIcon to JPanel not working

java,jpanel,panel,imageicon

ImageIcon(String) assumes that the specified String value is a file on the file system Based on you description I would guess that the image is embedded within the application, meaning that it is not longer accessible as a File, but instead needs to accessed as a URL or InputStream Because...

Add image to JCheckBoxMenuItem

java,image,swing,imageicon,jmenuitem

This can be done with a standard check box menu item, simply by adjusting the horizontal text position. import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; import javax.swing.border.EmptyBorder; public class CheckBoxMenuItemIconPosition { private JComponent ui = null; private JMenuBar mb = null; CheckBoxMenuItemIconPosition() { initUI(); } public void initUI() { if (ui!=null)...

Insert a GIF image into JFrame and make it visible and invisible on demand

java,swing,jframe,paint,imageicon

I want to achieve this by making my current frame view.getFrame2() invisible and make a view.getFrame3() visible. Don't be hiding/showing different windows. Users don't like frames disappearing. Instead you can simply display a modal JDialog containing your image on top of your main frame. Or another approach is to...

Java: Can't repaint ImageIcon

java,swing,jbutton,imageicon

You should make sure you're loading the file you think you're loading. Try this: ImageIcon icon = new ImageIcon("path/to/new/icon"); System.out.println(icon.getDescription()); This will be null if the file is not loaded properly. If this is your problem, try using an absolute path (e.g. /home/waTEXmelon/program/blah.jpg on Linux or C:\\Users\\waTEXmelon\\program\\blah.jpg) on Windows. If...

Image not showing in Jtable with renderer

java,swing,jtable,imageicon,tablecellrenderer

The code in a renderer should be very fast and efficient. You should not be doing processing to create the image every time the cell is rendered. Instead you should be storing an ImageIcon in the TableModel. Then you override the getColumnClass(...) method of the TableModel to return Icon.class and...

Where do I get the iOS toolbar icons for up/down arrow?

ios,iphone,objective-c,imageicon

They are not available. They're just a list of icons they have developed for iOS as a showcase. You should look into making them yourself and applying them as a UIButtonTypeCustom. Small drawback is that if the iOS style changes (like iOS6 -> iOS7) you need to manually update your...

Why is my JLabel grid not functioning properly?

java,swing,jlabel,grid-layout,imageicon

I figured out the problem and was able to simplify the code significantly. Here is the new updated code that works: ImageIcon man; ImageIcon grass; public int xPosition=0; public int yPosition=0; public int oldX =0; public int oldY = 0; class ButtonListener implements ActionListener{ @Override public void actionPerformed(ActionEvent evt) {...

Java GUI Update Existing Image

java,swing,user-interface,jlabel,imageicon

Don't create a new instance of the image label, simple set the image label's icon property... image.setIcon(newPark); You may also want to read up on Initial Threads and the importance of creating your UI's within the context of the Event Dispatching Thread...

Swing: JList holding multiple Components as a single item

java,swing,jtable,jlist,imageicon

JList is a generic class. Create a class which extends JPanel. (Let us call it RowPanel) Put all elements in it required to be present in a single row (using a horizontal layout) Create your JList using those panels like JList<RowPanel> list = new JList<RowPanel>(); You can refer to this...

Getting errors trying to use KeyListeners and ImageIcons?

java,swing,graphics,stack-overflow,imageicon

The problem is that you have infinite recursion in your constructors. Layout() calls addKeyListener(new Move());, and Move extends Layout, so Move's constructor also calls Layout(), which in turn calls Move(). You need to refactor your code to avoid that. A simple approach would be making Move not extend Layout, and...

How to create an instance of ImageIcon

java,swing,imageicon

Class.getResource() is for accessing stuff via classloader, e.g. things in the same jar as your application. To access a file from filesystem create URL from file, e.g. new File(path).toURI().toURL();...

JAVA JPanel not displaying image

java,jpanel,bufferedimage,imageicon

I want to pop up a new window which will display this image in the URL, not add it to an existing frame I asked why you would expect this to display as a window, and you stated, I would expect it to because I have instantiated it and...

Making a chess game in Java, I want to move the pieces

java,swing,jbutton,imageicon,chess

Try the following code. I don't know the exact code for working with ImageIcons on JButtons, but this gets the ideas across: JButton pieceToMoveButton = null; //variable that persists between actionPerformed calls public void actionPerformed(ActionEvent actionEvent) { JButton button = (JButton)actionEvent.getSource(); if (pieceToMoveButton == null) //if this button press is...

Changing Content of JTable Cells Individually

java,jtable,jpanel,cell,imageicon

To change the data in the table you use: table.setValueAt(...); This will update the data in the TableModel. However, adding a JPanel to the table is not the way a table is designed to be used. Typically you add data to the model. Then you use a renderer to display...

JCheckBox with Image

java,swing,jlabel,imageicon,jcheckbox

Without knowing the layout manager, it's difficult to be 100%, but if I was doing this, I might use a GridBagLayout... testate.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridy = 0; gbc.insets = new Insets(4, 4, 4, 4); gbc.anchor = GridBagConstraints.WEST; for (String testata : listaTestate) { gbc.gridx = 0;...

Resize an ImageIcon for an JLabel

java,image,swing,resize,imageicon

Although I didn't understand the setDimensions(), but I think you are trying to fit your image into screen width and height. By multiplying int values of width and height in setDimensions(), you will simply be able to multiply small int numbers. For bigger numbers you will run out of memory...

Can't Get ImageIcon to Display

java,swing,graphics,jframe,imageicon

Comment out frame.setLayout(null); and you'll most likely see the game. The default layout of a JFrame is currently a BorderLayout An object added to a BorderLayout without a constraint defaults to the CENTER. An object in the CENTER of a BorderLayout is stretched to the available with and height. Since...

Java add icon to JCalendar

java,swing,imageicon,jcalendar

Adding an icon to the buttons of a JDayChooser is not supported. You'd have to extend JDayChooser and modify one of the buttons in the protected array named days. As the panel is already fairly crowded, I'm not sure the effect would be appealing. Alternatively, implement the IDateEvaluator interface and...

Unable to change button text to icon immediate after click button in java swing

java,swing,jbutton,embedded-resource,imageicon

The GUI system can only do one thing at a time, like most code (except for code that uses threads). Calling your listener is a thing. The GUI system cannot do anything else while your listener is running. Your database operation needs to run on another thread (which you can...

Jbutton with an image as background can't be clicked

java,swing,jbutton,actionlistener,imageicon

You have either given it a title, or an image, not both. You should use the JButton butoEtnies = new JButton("Etnies", /*Here goes the icon*/); Or you can also try: JButton butoEtnies = new JButton("Etnies"); butoEtnies.setIcon(/*Here goes the icon*/); Besides, here's the Java API for JButton EDIT #2 Next, provides...

Unable to get resources using getResource() [duplicate]

java,swing,user-interface,nullpointerexception,imageicon

Try something like this for setting the image icon ImageIcon imageIcon = new ImageIcon(getClass().getResource("myImage.png")); Your image file should be located within the src folder if your using an IDE. If your not using an IDE , put the image in the same folder as your .java files....

Adding icons to jTable in different rows

java,image,swing,jtable,imageicon

This is solved easily by simply making sure that the column for the icons returns Icon.class in the table model's getColumnClass(...) method. Fortunately JTables know how to display icons without any extra work if you do this. For example. A modification of my code from there uses this table model:...

Autoresize the image using imageicon on jlabel

java,swing,imageicon

you can scale your image the following way, Image img = format.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH); jLabel15.setIcon(new ImageIcon(img)); I have scalled the image to 50X50 you can scale it to your desired size ...

Sudoku: Selection image does not appear

java,imageicon,sudoku

What I found is that the layout layers are backwards to what i would expect. The Label you add first will always stay on top. That label won't be drawn over by JComponents add after the original is on. So basically, the earlier in the code you add a component...

How to sort a JTable column that contains icons?

java,swing,jtable,awt,imageicon

The order doesn't matter, just as long as the rows are ordered in the sort. That's simplicity itself. Use table.setAutoCreateRowSorter(true); I already have that in my code It works just fine for me. The 3rd column is sorted ascending. import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.table.*;...

image doesnt shows on JLabel when click on next button, using java Swing

java,swing,jlabel,imageicon

Add frame.revalidate() and frame.repaint() after frame.getContentPane().add(image); Swing is lazy when it comes to performing updates to the layout hierarchy, which is good thing, imagine adding a few dozen components and having the entire component hierarchy been updated for each one. So, instead, you need to update the container manually when...

Flipping the cards for memory card game isn't working in java (without applets)

java,swing,jlabel,imageicon

Try label1.setIcon(image1); instead of label1 = new JLabel(image1); in EventClass. Because you create a new instance of JLabel with new Icon which is not added to your JFrame.

Converting from Object[][] to ImageIcon

java,swing,converter,imageicon

You have a 2D array so you need to use another index to reference the element at row i column j for (int i = 0; i < images.length; i++) { for (int j = 0; j < images[i].length; j++) { ImageIcon icon = new ImageIcon(images[i][j].toString()); ... } } It...

Removing an Image off a JFrame

java,swing,jframe,jlabel,imageicon

Take out add(panel1) and remove(panel1), and use setContentPane(panel1) instead.

Adding an image to JPanel within JLabel

java,image,swing,imageicon

Your application can't find the ""images/cirkel.png". You have few alternatives: Use an absolute path (like I do in the modified code below). Use resources (there are hundreds of good tutorials how to do this). I use absolute path for quick hacks. For anything serious I would chose resources as they...

Array of JLabel ImageIcon

java,arrays,jlabel,imageicon

change you for loop, it will not enter into loop as per your condition. change loop to this.. for (int i=0; i<8; i++) { gridLabel[i] = new JLabel("Hello"); } for (int i=0; i<8; i++) { gridLabel[i].setIcon(gridPic); add(gridLabel[i]); } it will work.....

Adding an image into a GUI made with Eclipse

java,swing,jpanel,imageicon

An Icon is not a component. You need to add the Icon to a component like a JLabel: ImageIcon image = new ImageIcon(getClass().getResource("EXTS.png")); //JPanel.add(image, BorderLayout.NORTH); JPanel.add(new JLabel(image), BorderLayout.NORTH); ...

Why is my JFrame image not changing the icon?

java,image,swing,imageicon

May be that BMP is not supported. If you follow the Java source code from the constructor of ImageIcon you end up at: (java.awt.Toolkit.java) /** * Returns an image which gets pixel data from the specified file, * whose format can be either GIF, JPEG or PNG. * ... */...

java imageicon bring to front

java,imageicon

Use an ArrayList and remove the selected card from the list and add it back at the top. Then paint the cards in the order of the list.

JLabel icon not changing during run-time

java,swing,jlabel,imageicon

Please make sure you do it on the swing thread. Also, make sure image was loaded correctly. Here is a simple code that I used to test and it is fine. public class Main { public static void main(String[] args) { final JFrame frame = new JFrame("TEST"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); final JLabel...

ImageIcon is displayed as a small square

java,swing,imageicon

This answer addresses my criticism of the answer by the OP. The example incorporates the advice added in various comments. import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.border.LineBorder; public class Main { public static void main(String[] args) throws Exception { URL url = new URL("http://i.imgur.com/o0E0aGD.png"); BufferedImage...

Image Path in Eclipse using Windows 7 vs Mac OS X

java,eclipse,windows,import,imageicon

You should just be able to do something like the following: picLabel.setIcon(new ImageIcon("/Project/icon.png")); Using the file path like in this example is a better way to retrieve files in any program. This makes it more compatible. So if you were going to deploy this program to other servers/machines you would...

Can't add JLabel to JPanel

java,swing,jlabel,imageicon

Things to be aware of... ImageIcon can fail silently...annoying I know...this is because... ImageIcon uses a background thread to load the images, this is because it was designed to allow for slow sources (dial up networks) which might need time to fully realise the image. You should use ImageIO.read to...

Simple Java GUI, cards not appearing

java,swing,user-interface,imageicon

You haven't actually used a proper file name "Images/picture1". Should be something like "Images/picture1.png" with the file format Also image files, generally should be read from the class path, if you plan on having them embedded to the program. To do so, you will first need to put the file...

Java paint icon not working for background image

java,swing,paintcomponent,imageicon

Now paintIcon has x, y parameters not width/height, which seems to be the error. In general I somewhat dislike the paintIcon method, and would store the Image and do: @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(backgroundImg.getImage(), 0, 0, null); } ...

Swing ImageIcon causing error and not making image appear

java,swing,nullpointerexception,embedded-resource,imageicon

I figured out what I did wrong. This: background.setIcon(new ImageIcon(getClass().getResource("spaceage.images.starfield.png"))); needed to be changed to this: background.setIcon(new ImageIcon(getClass().getResource("/spaceage/images/starfield.png"))); I got a NullPointerException because I entered the path to my image incorrectly, so getResource() couldn't find the image and returned null....

setImageIcon doesn't set JFrame icon on mac swing window

java,osx,swing,jframe,imageicon

Mac does not support frame icons, as seen in this answer. ...

How to change description image in JList java

java,swing,jlist,imageicon,defaultlistmodel

I agree with trashgod (+1 to his suggestion), a JTable will be a simpler solution, here's why... JList doesn't support editability, so you'd need to create it... So, first, we'd need some kind of ListModel that provided some additional functionality, in particular, the ability to set the value at a...

Java resize an image

java,swing,jlabel,imageicon

You can paint the image in a JPanel as the whole panel. Then whenever the panel is resized, the image will be resized along with it. Here's a quick-n-dirty runnable demo: import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; public class ImgFrame...

ImageIcon get Parent JButton

java,swing,imageicon

How can I get the JButton element from within the ImageIcon class? Yes you can do it using ImageIcon#getImageObserver() and ImageIcon#setImageObserver(). Sample code: ImageIcon icon = new ImageIcon(); JButton btn = new JButton(icon); // set the Image Observer of the ImageIcon icon.setImageObserver(btn); ... // get Image Observer back from...

How to put an ImageIcon in a JLabel that contains a hyperlink?

java,swing,hyperlink,jlabel,imageicon

For this you have to add mouse listener in JLabel label which contains ImageIcon means your image. Here is the Code : Icon i1 = new ImageIcon(getClass().getResource("image.jpg")); l4 = new JLabel(i1); l4.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { String command = "rundll32 url.dll,FileProtocolHandler https://www.google.com"; try { Process p =...