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.
Related
im trying to change the icon of a jLabel named "mainDot". i have another folder in the source packages (named "Images") that contains the icons i want to put on the mainDot when I press it.
private void mainDotMousePressed(java.awt.event.MouseEvent evt) {
mainDot.setIcon(DOT_SMALL.jpg);
}
How would I be able to change the icon of mainDot?
public Test() {
JLabel t=new JLabel();
this.setLayout(new BorderLayout());
t.setIcon(new ImageIcon("Path to image goes here"));
this.add(t);
this.pack();
this.setVisible(true);
}
this one is working. and as suggested use an actionlistener to do it on button press
The problem I am running into is this: I have a grid of buttons in a JPanel, these buttons are supposed to change to an image of a queen when I click them. The code looks like this:
private Component createButtonBlack() {
final JButton button = new BoardButton();
final ImageIcon queen = new ImageIcon("/images/queen.png");
button.setBackground(Color.BLACK);
button.setPreferredSize(new Dimension(40, 40));
class QueenClick implements ActionListener {
public void actionPerformed(ActionEvent event) {
button.setIcon(queen);
button.repaint();
}
} // end QueenClick
ActionListener queenClicker = new QueenClick();
button.addActionListener(queenClicker);
return button;
} // end createButtonBlack
The problem (image not appearing) occurs on both the methods for creating black and white buttons but the methods are the same except for the color. Ideally I would like to be able to un-click the buttons and the image disappears but I do not know how to do that.
I am having difficulty with other parts of my 8queens GUI based problem so if you have any suggestions let me know!
Also if you need more code I will certainly supply it. Thank you.
State the exact problem when asking a question.
These buttons are supposed to change to an image of a queen when I click them.
So I'm guessing the icon doesn't change?
Did you:
Verify the ActionListener code is executed?
Verify the Icon was read properly?
You can easily add a System.out.println(...) to verify both of the above.
final ImageIcon queen = new ImageIcon("/images/queen.png");
I'm guessing the problem is the leading "/" in the path. The "/" tells the file system to look at the root of the drive.
if you have any suggestions let me know!
There is no need to create two methods. You can just do:
Component button = createButton();
button.setBackground( Color.BLACK );
There is no need to create individual ActionListeners. You can create a single generic listener with code like:
ActionListener queenClicker = new ActionListener()
{
#Override
public void actionPerformed(Action Event e)
{
JButton button = (JButton)e.getSource();
button.setIcon( queen );
//button.repaint(); // not needed the setIcon method will do the repaint()
}
}
When I open file that show the dialog box, I need to change Java image and add my own image. How to customize dialog box?
For example, I need to add the Encoding to the dialog box and how to add different type of files to Files of type dropdown box. For Example, i add text, java, html to Files of type box.
Here is my code,
FileDialog fd = new FileDialog(OpenExample.this, "Select File", FileDialog.LOAD);
fd.setVisible(true);
To provide an icon for a file chooser or dialog, set an icon for the parent frame.
import java.awt.image.BufferedImage;
import javax.swing.*;
public class FileChooserIcon {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
// see nice icons in chooser!
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {}
JLabel ui = new JLabel("Big Space");
ui.setBorder(new javax.swing.EmptyBorder(40, 200, 40, 200));
JFrame f = new JFrame("Show file chooser icon");
f.setIconImage(new BufferedImage(
20, 20, BufferedImage.TYPE_INT_RGB));
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setContentPane(ui);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(f); // use frame icon!
}
};
SwingUtilities.invokeLater(r);
}
}
.. how to add different type of files to Files of type dropdown box? For example: add text, java, html to Files of type box.
See How to Use File Choosers: FileChooserDemo2 which offers a file filter for Just Images..
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.
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.