Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 4.28 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Java Swing Thumbnail View of Images to select image
  2. boolean isSelected = false;
  3. JButton jButton;
  4. void imageClickTest() throws MalformedURLException, IOException {
  5.     final JFrame frame = new JFrame("Demo");
  6.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7.     frame.setSize(400, 400);
  8.     frame.setLayout(new BorderLayout());
  9.  
  10.     final JTabbedPane tabbedPane = new JTabbedPane();
  11.  
  12.     JPanel pane = new JPanel();
  13.     JButton button;
  14.     pane.setLayout(new BorderLayout());
  15.  
  16.     button = new JButton("I'm second button");
  17.     button.setIcon(new ImageIcon(ImageIO.read(new URL("http://cdn5.iconfinder.com/data/icons/ie_Financial_set/24/26.png"))));
  18.     button.addActionListener(new ActionListener() {
  19.         @Override
  20.         public void actionPerformed(ActionEvent e) {
  21.             JButton button = (JButton) e.getSource();
  22.             if(isSelected) {
  23.                 System.out.println("two selected");
  24.                 button.setBorder(BorderFactory.createEtchedBorder());
  25.                 isSelected = false;
  26.                 JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  27.                 jSplitPane.add(button);
  28.                 jButton.setBorder(BorderFactory.createEtchedBorder());
  29.                 jButton.setText("First click me");
  30.                 jSplitPane.add(jButton);
  31.                 jSplitPane.setDividerLocation(150);
  32.                 tabbedPane.addTab("Image Comparision", jSplitPane);
  33.             }
  34.         }
  35.     });
  36.     pane.add(button, BorderLayout.SOUTH);
  37.  
  38.     button = new JButton("First click me");
  39.     button.setIcon(new ImageIcon(ImageIO.read(new URL("http://cdn4.iconfinder.com/data/icons/REALVISTA/web_design/png/24/testimonials.png"))));
  40.     button.addActionListener(new ActionListener() {
  41.         @Override
  42.         public void actionPerformed(ActionEvent e) {
  43.             JButton button = (JButton) e.getSource();
  44.             button.setBorder(BorderFactory.createLineBorder(Color.RED, 5));
  45.             button.setText("Now Click on second button.");
  46.             jButton = button;
  47.             isSelected = true;
  48.         }
  49.     });
  50.     pane.add(button, BorderLayout.NORTH);
  51.  
  52.     button = new JButton("I'm just extra button");
  53.     button.setIcon(new ImageIcon(ImageIO.read(new URL("http://cdn2.iconfinder.com/data/icons/crystalproject/64x64/apps/kservices.png"))));
  54.     button.setEnabled(false);
  55.     pane.add(button, BorderLayout.CENTER);
  56.  
  57.     tabbedPane.addTab("ImagePane", pane);
  58.     frame.add(tabbedPane, BorderLayout.CENTER);
  59.     frame.setVisible(true);
  60. }
  61.        
  62. import javax.swing.*;
  63.     import java.awt.*;
  64.     import java.awt.Event.*;
  65.     import java.io.File;
  66.     import java.awt.image.BufferedImage;
  67.     import javax.imageio.ImageIO;
  68.     import java.io.IOException;
  69.  
  70.     public class SwindDesign {
  71.     public static void main(String[] args) throws IOException {
  72.         JFrame frame = new JFrame("Split Pain");
  73.         frame.setSize(700, 500);
  74.         frame.setVisible(true);
  75.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  76.         frame.setLayout(new GridLayout());
  77.  
  78.         //panel
  79.         JPanel panel = new JPanel();
  80.         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  81.         panel.add(new PicturePanel());
  82.  
  83.        JTabbedPane jtp = new JTabbedPane();
  84.  
  85.          jtp.addTab("Set Image", panel);
  86.           jtp.addTab("Compare Image", new JButton());
  87.           frame.add(jtp);
  88.  
  89.     }
  90. }
  91. class PicturePanel extends JPanel {
  92.  
  93.     File folder = new File("C:/Documents and Settings/All Users/Documents/My      Pictures/Sample Pictures");
  94.     File[] listOfFiles = folder.listFiles();
  95.     ImageIcon[] img ;
  96.     JComponent lblimg;
  97.     JTabbedPane jtp = new JTabbedPane();
  98.     private BufferedImage[] b = new BufferedImage[10];
  99.  
  100.     public PicturePanel() throws IOException {
  101.         for (int i = 0; i < listOfFiles.length; i++) {
  102.             System.out.println("chek panth"+listOfFiles[i].getName().toString());
  103.             b[i] = ImageIO.read(new File("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/" + listOfFiles[i].getName().toString()));
  104.         }
  105.     }
  106.  
  107.     @Override
  108.     protected void paintComponent(Graphics g) {
  109.         super.paintComponents(g);
  110.         Graphics2D g2 = (Graphics2D) g;
  111.         int k = 10;
  112.         for (int j = 0; j < listOfFiles.length - 1; j++) {
  113.             g2.drawImage(b[j], k, 0, 100, 100, null);
  114.             k = k + 75;
  115.             }
  116.     }
  117. }