Mitko_jos

lab04_ia v1

Nov 19th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.82 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.util.ArrayList;
  5.  
  6. import javax.swing.*;
  7. import javax.swing.event.ChangeEvent;
  8. import javax.swing.event.ChangeListener;
  9. import javax.swing.event.ListSelectionEvent;
  10. import javax.swing.event.ListSelectionListener;
  11.  
  12. class Book {
  13.    
  14.     private String title;
  15.     private String ISBN;
  16.     private int year;
  17.     private String publisher;
  18.     private String picture;
  19.    
  20.  
  21.     public Book(String title, String ISBN, int year, String publisher, String picture) {
  22.         this.title = title;
  23.         this.ISBN = ISBN;
  24.         this.year = year;
  25.         this.publisher = publisher;
  26.         this.picture = picture;
  27.     }
  28.  
  29.     public String getTitle() {
  30.         return title;
  31.     }
  32.  
  33.     public void setTitle(String title) {
  34.         this.title = title;
  35.     }
  36.  
  37.     public String getISBN() {
  38.         return ISBN;
  39.     }
  40.  
  41.     public void setISBN(String ISBN) {
  42.         this.ISBN = ISBN;
  43.     }
  44.  
  45.     public int getYear() {
  46.         return year;
  47.     }
  48.  
  49.     public void setYear(int year) {
  50.         this.year = year;
  51.     }
  52.  
  53.     public String getPublisher() {
  54.         return publisher;
  55.     }
  56.  
  57.     public void setPublisher(String publisher) {
  58.         this.publisher = publisher;
  59.     }
  60.  
  61.     public String getPicture() {
  62.         return picture;
  63.     }
  64.  
  65.     public void setPicture(String picture) {
  66.         this.picture = picture;
  67.     }
  68.  
  69.     @Override
  70.     public String toString() {
  71.         return title;
  72.     }
  73.    
  74.    
  75. }
  76.  
  77. class Database {
  78.    
  79.     static Book []books = new Book[5];
  80.    
  81.     public static void initDatabase(){
  82.         books[0] = new Book("Profession Android 4 Application Development","978111810227",2011,"Wrox; 3 edition","picture1.jpg");
  83.         books[1] = new Book("Learning jQuery","1849516545",2011,"Packt Publishing; 3 edition","picture2.jpg");
  84.         books[2] = new Book("Java Swing","0596004087",2002,"O'Reilly Media; 2nd edition","picture3.gif");
  85.         books[3] = new Book("Eloquent JavaScript\n" +
  86. "A Modern Introduction to Programming","1593272820",2010,"No Starch Press; 1 edition","picture4.png");
  87.         books[4] = new Book("JavaScript & jQuery: The Missing Manual","1449399029",2012,"Pogue Press; Second Edition edition","picture5.jpg");
  88.     }
  89.    
  90.     public static Book[] getBooks(int year){
  91.         ArrayList<Book> b = new ArrayList<Book>();
  92.         int k = 0;
  93.         for(int i = 0;i<books.length;i++){
  94.             if(books[i].getYear() >= year){
  95.                 b.add(books[i]);
  96.             }
  97.         }
  98.         Book[] b1 = new Book[b.size()];
  99.         for(int i = 0;i<b.size();i++){
  100.             b1[i] = b.get(i);
  101.             System.out.println(b1[i]);
  102.         }
  103.         return b1;
  104.     }
  105.    
  106.     public static Book[] getBooks() {
  107.         return books;
  108.     }
  109. }
  110.  
  111. public class Lab4 extends JFrame implements ListSelectionListener, ActionListener{
  112.     final JList list;
  113.     JList list1;
  114.     JScrollPane scroller,scroller1;
  115.     JPanel panel1,panel2,panel3,p11,p12,p13;
  116.     JSlider slider;
  117.     JLabel lblSlika,lblTitle,lblISBN,lblYear,lblPublisher;
  118.     JTextField txtTitle,txtISBN,txtYear,txtPublisher;
  119.     JButton addToCard;
  120.     JTextArea card;
  121.     Database ob;
  122.     final DefaultListModel<Book> listModel;
  123.    
  124.     public Lab4(){
  125.         ob.initDatabase();
  126.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  127.         this.setLayout(new GridLayout(1,3));
  128.         this.setSize(650,500);
  129.         this.setTitle("Book");
  130.        
  131.         this.panel1=new JPanel();
  132.         this.panel1.setBorder(BorderFactory.createTitledBorder("Book List:"));
  133.         this.panel1.setLayout(new BorderLayout());
  134.        
  135.        
  136.         this.list = new JList(ob.getBooks());
  137.         this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  138.        this.list.setLayoutOrientation(JList.VERTICAL);
  139.         this.list.setBorder(BorderFactory.createLineBorder(Color.blue,1));
  140.         this.list.setBorder(BorderFactory.createTitledBorder("Book List:"));
  141.         this.list.addListSelectionListener(this);
  142.         this.slider = new JSlider(JSlider.HORIZONTAL,2000,2013,2000);
  143.         final JLabel pom  = new JLabel();
  144.        
  145.         this.slider.addChangeListener(new ChangeListener(){
  146.             @Override
  147.             public void stateChanged(ChangeEvent arg0) {
  148.                 String str=String.format("%d",slider.getValue());
  149.                 pom.setText(str);
  150.                 int value = slider.getValue();
  151.                 list.setListData(Database.getBooks(value));
  152.             }});
  153.        
  154.         this.p12=new JPanel();
  155.         this.p12.setLayout(new BoxLayout(this.p12,BoxLayout.Y_AXIS));
  156.         this.p12.add(slider);
  157.         this.p12.add(pom);
  158.         this.panel1.add(list,BorderLayout.NORTH);
  159.         this.panel1.add(p12,BorderLayout.CENTER);
  160.        
  161.         this.panel2=new JPanel();
  162.         this.panel2.setLayout(new GridLayout(2,1));
  163.        
  164.         this.lblSlika=new JLabel();
  165.         this.p11=new JPanel();
  166.         this.p11.setLayout(new BoxLayout(this.p11,BoxLayout.Y_AXIS));
  167.         this.lblTitle=new JLabel("Title");
  168.         this.lblISBN=new JLabel("ISBN");
  169.         this.lblYear=new JLabel("Year");
  170.         this.lblPublisher=new JLabel("Publisher");
  171.         this.txtTitle=new JTextField(5);
  172.         this.txtISBN=new JTextField(5);
  173.         this.txtYear=new JTextField(5);
  174.         this.txtPublisher=new JTextField(5);
  175.         this.addToCard=new JButton("Add to card>>");
  176.         this.addToCard.addActionListener(this);
  177.         this.p11.add(this.lblTitle);
  178.         this.p11.add(this.txtTitle);
  179.         this.p11.add(this.lblISBN);
  180.         this.p11.add(this.txtISBN);
  181.         this.p11.add(this.lblYear);
  182.         this.p11.add(this.txtYear);
  183.         this.p11.add(this.lblPublisher);
  184.         this.p11.add(this.txtPublisher);
  185.         this.p11.add(this.addToCard);
  186.        
  187.         this.panel2.add(this.lblSlika);
  188.         this.panel2.add(this.p11);
  189.        
  190.         this.panel3=new JPanel();
  191.         this.panel3.setLayout(new BorderLayout());
  192.         this.panel3.setBorder(BorderFactory.createTitledBorder("Ordered books:"));
  193.          
  194.  
  195.         listModel = new DefaultListModel<Book>();
  196.         list1 = new JList<Book>(listModel);
  197.         this.list1.setLayoutOrientation(JList.VERTICAL);
  198.         this.scroller1=new JScrollPane(this.list1);
  199.         this.panel3.add(this.scroller1);
  200.        
  201.         this.add(this.panel1);
  202.         this.add(this.panel2);
  203.         this.add(this.panel3);
  204.        
  205.     }
  206.     @Override
  207.     public void valueChanged(ListSelectionEvent arg0) {
  208.         // TODO Auto-generated method stub
  209.         if(arg0.getValueIsAdjusting()==false)
  210.         {
  211.             Book ob2=(Book)list.getSelectedValue();
  212.             this.txtTitle.setText(ob2.getTitle());
  213.             this.txtISBN.setText(ob2.getISBN());
  214.             String str=String.format("%d",ob2.getYear());
  215.             this.txtYear.setText(str);
  216.             this.txtPublisher.setText(ob2.getPublisher());
  217.             ImageIcon ic= new ImageIcon("D:\\FINKI\\3 semestar\\IA\\Lab4\\pictures\\" + ob2.getPicture());
  218.             Image img = ic.getImage().getScaledInstance(200, 190, java.awt.Image.SCALE_SMOOTH);
  219.             ic = new ImageIcon(img);
  220.             this.lblSlika.setIcon(ic);
  221.            
  222.    
  223.     }
  224.        
  225.     }
  226.     public static void main(String[] args) {
  227.         Lab4 ob=new Lab4();
  228.         ob.setVisible(true);
  229.     }
  230.    
  231.     @Override
  232.     public void actionPerformed(ActionEvent arg0) {
  233.         listModel.addElement((Book)list.getSelectedValue());
  234.        
  235.     }
  236. }
Add Comment
Please, Sign In to add comment