Skip to main content

Showing an image in a JDialog

No replies
ntomer
Offline
Joined: 2011-09-16
Points: 0

Hi,

I have an application, which does some automated bot operations on a web site. This web site has a captcha at the time of submission, so I thought of creating a class extending from JDialog in which I will show the captcha image from that web site, user will enter the captcha and click the button. My program will get the captcha string and proceed.

But I am not able to show the image on JDialog; please tell me where I am going wrong.

Regards

Nitin

package com.newgen.omni.importdoc.swingForm;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.imageio.ImageIO;

public class ImportDocSwingForms extends javax.swing.JDialog
{
public String captcha = "";
public boolean bClose = false;

BufferedImage image;

public ImportDocSwingForms(String strURL, BufferedImage image)
{
initComponents();

// changed by Nitin
setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
setModal(true);

this.image = image;

captcha = "";
bClose = false;

jButton1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();

String field1 = jTextField1.getText();
bClose = true;

if(field1.isEmpty())
{
JOptionPane.showMessageDialog(null, "Fill Mandatory fields", "Error", JOptionPane.INFORMATION_MESSAGE);
}
else
{
setCaptcha(field1);
ImportDocSwingForms.this.dispose();
}
}
});
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();

setMinimumSize(new java.awt.Dimension(800, 250));

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel1.setText("Please enter the characters shown in the image");

jButton1.setText("Done");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(115, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(49, 49, 49))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 258, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-425)/2, (screenSize.height-345)/2, 425, 345);
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
String path = "http://cms.outlookindia.com/images/thumb/ashok_banker_thumb150.jpg";

BufferedImage image = null;
try
{
//URL url = new URL(path);
//URLConnection connection = url.openConnection();
//InputStream in = connection.getInputStream();

File file = new File("C:\\temp

DSCF1928.JPG");
FileInputStream fIS = new FileInputStream(file);

image = ImageIO.read(fIS);
fIS.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}

ImportDocSwingForms isf = new ImportDocSwingForms("www.google.com",image);
isf.setVisible(true);
System.out.println(isf.captcha);
isf.dispose();

}
});
}

void setCaptcha(String captcha)
{
this.captcha = captcha;
}

protected void paintComponent(Graphics g)
{
// Center image in this component.
g.drawImage(image, 0, 0, this);
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}