NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.BorderLayout;
import javax.swing.SpringLayout;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.plaf.basic.BasicComboBoxUI.ComboBoxLayoutManager;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JComboBox;


public class Main extends JFrame {

private JFrame frame;

File f = null;
String path = null;
private ImageIcon format = null;
String fname=null;
int s = 0;
byte[] pimage=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Main() {
initialize();
Connect();
LoadImageID();
}


Connection con;
PreparedStatement pst;
ResultSet rs;
private JTextField textField;
public void Connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/img","root","");
} catch (ClassNotFoundException e) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
}catch(SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}


}
JComboBox comboBox = new JComboBox();
public void LoadImageID() {
try {
pst = con.prepareStatement("SELECT id FROM images");
rs = pst.executeQuery();
comboBox.removeAllItems();

while(rs.next()) {
comboBox.addItem(rs.getString(1));
}

}catch(SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
JLabel lblNewLabel_1 = new JLabel();
public void LoadImage() {
try {
byte[] imagedata = rs.getBytes("imgFile");
format = new ImageIcon(imagedata);
Image mm = format.getImage();
Image img2 = mm.getScaledInstance(200,200, Image.SCALE_SMOOTH);
ImageIcon image = new ImageIcon(img2);
lblNewLabel_1.setIcon(image);
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

}

private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 537, 527);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
frame.getContentPane().setLayout(springLayout);

JLabel label = new JLabel("New label");
springLayout.putConstraint(SpringLayout.NORTH, label, 58, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, label, 49, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, label, 57, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, label, 67, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(label);

JLabel lblNewLabel = new JLabel("");
springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel, 0, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, lblNewLabel, 28, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, lblNewLabel, 140, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, lblNewLabel, 158, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(lblNewLabel);



JButton btnNewButton = new JButton("Change");
springLayout.putConstraint(SpringLayout.NORTH, btnNewButton, 26, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, btnNewButton, 42, SpringLayout.EAST, lblNewLabel);
btnNewButton.addActionListener(new ActionListener() {
int count=1;
public void actionPerformed(ActionEvent e) {

Image img = new ImageIcon(this.getClass().getResource("/img"+count+".jpg")).getImage();
lblNewLabel.setIcon(new ImageIcon
(img.getScaledInstance(130, 130, Image.SCALE_SMOOTH)));
count++;
if(count>3) {
count=1;
}

}
});
frame.getContentPane().add(btnNewButton);
//Browse button
JButton btnNewButton_1 = new JButton("Browse");
springLayout.putConstraint(SpringLayout.NORTH, btnNewButton_1, 40, SpringLayout.SOUTH, lblNewLabel);
springLayout.putConstraint(SpringLayout.WEST, btnNewButton_1, 0, SpringLayout.WEST, lblNewLabel);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter fnwf = new FileNameExtensionFilter("PNG JPG JPEG","png","jpeg","jpg");
fileChooser.addChoosableFileFilter(fnwf);
int load = fileChooser.showOpenDialog(null);

if(load==fileChooser.APPROVE_OPTION) {
f = fileChooser.getSelectedFile();
path = f.getAbsolutePath();
textField.setText(path);
ImageIcon ii = new ImageIcon(path);
Image img = ii.getImage().getScaledInstance(200, 200, Image.SCALE_SMOOTH);
lblNewLabel.setIcon( new ImageIcon(img));
}

}
});
frame.getContentPane().add(btnNewButton_1);
//Insert button
JButton btnNewButton_2 = new JButton("Insert");
springLayout.putConstraint(SpringLayout.NORTH, btnNewButton_2, 0, SpringLayout.NORTH, btnNewButton_1);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {


System.out.print("Image Path - " + path);
File f = new File(path);

try {
InputStream is = new FileInputStream(f);
pst = con.prepareStatement("INSERT INTO images(imgName, imgPath, imgFile)VALUES(?,?,?)");
pst.setString(1,f.getName());
pst.setString(2, path);
pst.setBlob(3, is);

int inserted = pst.executeUpdate();
if(inserted>0) {
JOptionPane.showMessageDialog(null, "Image successfully inserted");
LoadImageID();
}
}catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}


}
});
frame.getContentPane().add(btnNewButton_2);

textField = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, textField, 15, SpringLayout.SOUTH, lblNewLabel);
springLayout.putConstraint(SpringLayout.WEST, textField, 28, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, textField, -6, SpringLayout.NORTH, btnNewButton_1);
springLayout.putConstraint(SpringLayout.EAST, textField, -344, SpringLayout.EAST, frame.getContentPane());
frame.getContentPane().add(textField);
textField.setColumns(10);


springLayout.putConstraint(SpringLayout.NORTH, comboBox, 15, SpringLayout.SOUTH, btnNewButton_1);
springLayout.putConstraint(SpringLayout.WEST, comboBox, 0, SpringLayout.WEST, lblNewLabel);
springLayout.putConstraint(SpringLayout.EAST, comboBox, 0, SpringLayout.EAST, btnNewButton_1);
frame.getContentPane().add(comboBox);
//Search buton
JButton btnNewButton_3 = new JButton("Search");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String imgID = comboBox.getSelectedItem().toString();
try {
pst = con.prepareStatement("SELECT * FROM images WHERE id=?");
pst.setString(1, imgID);
rs = pst.executeQuery();

if(rs.next()) {
LoadImage();
}else {
JOptionPane.showMessageDialog(null, "No image found");
}
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
springLayout.putConstraint(SpringLayout.WEST, btnNewButton_3, 28, SpringLayout.EAST, comboBox);
springLayout.putConstraint(SpringLayout.WEST, btnNewButton_2, 0, SpringLayout.WEST, btnNewButton_3);
springLayout.putConstraint(SpringLayout.SOUTH, btnNewButton_3, 0, SpringLayout.SOUTH, comboBox);
frame.getContentPane().add(btnNewButton_3);


springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel_1, 38, SpringLayout.SOUTH, comboBox);
springLayout.putConstraint(SpringLayout.WEST, lblNewLabel_1, 28, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, lblNewLabel_1, 178, SpringLayout.SOUTH, comboBox);
springLayout.putConstraint(SpringLayout.EAST, lblNewLabel_1, 100, SpringLayout.EAST, btnNewButton_3);
frame.getContentPane().add(lblNewLabel_1);
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.