Java - Custom Image in JoptionPane.ShowConfirmDialog is not working - java

When the User enters the password and click on Ok button the password will be encrypted and stored in JTextArea. And this is working fine. But I want to add a custom logo in the showConfirmDialog and showMessageDialog popup. I tried with the below code, but the custom Image (logo) is not displaying in the message popup
public static void main(String[] args) {
Box box = Box.createHorizontalBox();
JLabel label = new JLabel("Enter your password : ");
box.add(label);
JPasswordField passwordField = new JPasswordField(24);
box.add(passwordField);
final ImageIcon icon = new ImageIcon("C:\\Users\\Test\\Internet.png");
int button = JOptionPane.showConfirmDialog(null, box, "Enter your password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.NO_OPTION, icon);
if (button == JOptionPane.OK_OPTION) {
String password = new String(passwordField.getPassword());
String encryptedPassword;
if (password != null && !password.equals("")) {
byte[] bytesEncoded = Base64.encodeBase64(password.getBytes());
JTextArea richTextField = new JTextArea(10, 10);
encryptedPassword = new String(bytesEncoded);
richTextField.setText(encryptedPassword);
richTextField.setOpaque(false);
richTextField.setEditable(false);
JOptionPane.showMessageDialog(null, richTextField);
} else {
JOptionPane.showMessageDialog(null,
"Password cannot be null. Please enter password to encrypt.");
}
}
}<br>
I'm passing ImageIcon object into the JoptionPane.showConfirmDialog as an argument. But when I run this, i don't see any Image displayed in the popup. I'm not sure what I'm doing wrong here.
Note : I need a custom Image to be displayed in both the popup's. showConfirmDialog and showMessageDialog
Any help would be greatly appreciated

Your code is perfectly fine. I just ran it in my environment and it worked fine. Which leads me to believe that your problem is the path of the image. I even tested it with a path for a image that wasn't there and the window show without showing any image.
I only changed two things, the path of the image obviously:
final ImageIcon icon = new ImageIcon("c:\\temp\\poke-ball-png-13_30x30.png");
This image I got from Free Icons PNG
And the Base64 class since there is no mention from where you are using it I use the java one:
import java.util.Base64;
....
byte[] bytesEncoded = Base64.getEncoder().encode(password.getBytes());
So be sure that your image "C:\\Users\\Test\\Internet.png" Is really there on the disk at that path

Related

Display image on a Java button

xstButton.setIcon(new ImageIcon("D://icon-tender-check-press.png"));
I am using this line of code to display an image on a Java button.
I cannot see the desired image on the button, need help!!!
You could try it like this:
Image image = ImageIO.read(getClass().getResource("D://icon-tender-check-press.png"));
button.setIcon(new ImageIcon(image));
But i would suggest to create a Folder in your project to store images:
Image image = ImageIO.read(getClass().getResource("images/icon-tender-check-press.png"));
button.setIcon(new ImageIcon(image));
Although i am not exactly sure what your question is
Probably file "D://icon-tender-check-press.png" doesn't exists or is not a valid image.
Check it exists first.
File f = new File("D://icon-tender-check-press.png");
if(f.exists() && !f.isDirectory()) {
System.out.println("File exists");
}

Resizing-scaling the image in JTextPane

I want to scale and resize with mouse selected image, change selected image with corner point, and change scale picture in JTextPane as in Microsoft Word.
This code just added image without change size in text editor but not have function propertes size picture.
Please somebody help me with code:
JFileChooser jf=new JFileChooser();
// Show open dialog
int option=jf.showOpenDialog(this);
// If user chooses to insert..
if(option == JFileChooser.APPROVE_OPTION) {
File file = jf.getSelectedFile();
if(isImage(file)) {
jTextPane1.insertIcon(new ImageIcon(file.getAbsolutePath()));
}else
// Show an error message, if not an image
JOptionPane.showMessageDialog(this,"The file is not an image.",
"Not Image", JOptionPane.ERROR_MESSAGE);
}

Setting JTextField font to what is selected in JFontChooser

I found this component recently and its great exactly what I wanted but I am unsure how to use it.
How would I set the font of my JTextField to what is selected by the user in the JFontChooser?
This is all I can find on it:
The JFontChooser class is a swing component for font selection. This class has JFileChooser like APIs. The following code pops up a font chooser dialog.
JFontChooser fontChooser = new JFontChooser();
int result = fontChooser.showDialog(parent);
if (result == JFontChooser.OK_OPTION)
{
Font font = fontChooser.getSelectedFont();
System.out.println("Selected Font : " + font);
}
what I want it to do is:
update: tNumber.setFont(new font(""Tahoma", Font.BOLD, 300));
To what ever the user has chosen for the font style and size in font chooser.
Use font.deriveFont(Font.BOLD, 300f) to create a new font, based on the current font with the properties you supply.
tNumber.setFont(font.deriveFont(Font.BOLD, 300f));
See Font#deriveFont(float, int) for more details
Well you were very close,
JFontChooser fontChooser = new JFontChooser();
int result = fontChooser.showDialog(parent);
if (result == JFontChooser.OK_OPTION)
{
Font font = fontChooser.getSelectedFont();
tNumber.setFont(font.deriveFont(Font.BOLD, 300f));//This is the line I added.
}
See setFont and deriveFont documentation.

java jLabel image does not update

I'm creating a feedback label that displays the picture that the user has chosen in a file dialog.
The moment when a picture file is selected, the label will update itself into that image of which the user has clicked.
The first time when the picture is chosen it works fine, however when another picture is chosen for the 2nd time onwards, it remains as the first picture.
Codes:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
//browse button
FileDialog fd = new FileDialog(this, "Choose a file", FileDialog.LOAD);
fd.setDirectory("C:\\");
fd.setFile("*.jpg"); // jpg files only
fd.setVisible(true);
String filename = fd.getFile();
if (filename == null) {
System.out.println("You cancelled the choice");
} else {
savePicture("temp"); // save it in temp.jpg. This overwrites any existing picture.
ImageIcon imgThisImg = null;
imgThisImg = new ImageIcon(absfilePath+ "/temp.jpg");
jLabel7.setIcon(null);
jLabel7.setIcon(imgThisImg);
jLabel7.revalidate();
jLabel7.repaint();
}
During debugging, the moment after savePicture() function is executed, the directory picture is updated. Therefore it's not an issue with overwritting the file. The file is overwritten correctly, why does it still display the previous image? Is there a cache or something that i need to clear?
Thanks.
Using ImageIO to the read file works best. Can be achieved using the following line.
jLabel7.setIcon(new JLabel(new ImageIcon(ImageIO.read(new File("C:\\Users\\Cameron Gillespie\\Documents\\NetBeansProjects\\OnlineCabsClient\\src\\images\\taxiBackground.png")))));
You are taking the label then setting the icon. Creating a new label and ImageIcon. Then using ImageIO to read the file. Reading the image and printing it to the label.

Saving a picture into a variable

I'm wanting to upload a picture and save it under a variable named contactPicture. I have tried looking online but can't find much, and what I have found seems to give errors. This is what i have so far.
Bitmap contactPicture = null; (Error 1)
JButton pictureanswer = new JButton("Browse");
pictureanswer.setForeground(Color.black);
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 9;
addPanel.add(pictureanswer,c);
pictureanswer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
File pictureDirectory = chooser.getSelectedFile();
contactPicture = BitmapFactory.decodeFile(pictureDirectory); (Error 2)
}
});
To explain whats happening here, I create a button "Browse". This then, when clicked, opens up the browse window to search for images. It saves this file directory under the variable, pictureDirectory.
This is the bit giving errors. I found that the final line of code should save the picture. However its giving errors.
Error 1: Bitmap cannot be reserved to a type.
Error 2: BitmapFactory cannot be reserved and error 1 again.
Please explain what Im doing wrong, all help appreciated! :)
You can jsut use something like:
java.awt.image.BufferedImage img = ImageIO.read(new FileInputStream(path));

Categories