Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import javax.swing.*;
- import javax.swing.event.ChangeEvent;
- import javax.swing.event.ChangeListener;
- import javax.swing.event.ListSelectionEvent;
- import javax.swing.event.ListSelectionListener;
- class Book {
- private String title;
- private String ISBN;
- private int year;
- private String publisher;
- private String picture;
- public Book(String title, String ISBN, int year, String publisher, String picture) {
- this.title = title;
- this.ISBN = ISBN;
- this.year = year;
- this.publisher = publisher;
- this.picture = picture;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getISBN() {
- return ISBN;
- }
- public void setISBN(String ISBN) {
- this.ISBN = ISBN;
- }
- public int getYear() {
- return year;
- }
- public void setYear(int year) {
- this.year = year;
- }
- public String getPublisher() {
- return publisher;
- }
- public void setPublisher(String publisher) {
- this.publisher = publisher;
- }
- public String getPicture() {
- return picture;
- }
- public void setPicture(String picture) {
- this.picture = picture;
- }
- @Override
- public String toString() {
- return title;
- }
- }
- class Database {
- static Book []books = new Book[5];
- public static void initDatabase(){
- books[0] = new Book("Profession Android 4 Application Development","978111810227",2011,"Wrox; 3 edition","picture1.jpg");
- books[1] = new Book("Learning jQuery","1849516545",2011,"Packt Publishing; 3 edition","picture2.jpg");
- books[2] = new Book("Java Swing","0596004087",2002,"O'Reilly Media; 2nd edition","picture3.gif");
- books[3] = new Book("Eloquent JavaScript\n" +
- "A Modern Introduction to Programming","1593272820",2010,"No Starch Press; 1 edition","picture4.png");
- books[4] = new Book("JavaScript & jQuery: The Missing Manual","1449399029",2012,"Pogue Press; Second Edition edition","picture5.jpg");
- }
- public static Book[] getBooks(int year){
- ArrayList<Book> b = new ArrayList<Book>();
- int k = 0;
- for(int i = 0;i<books.length;i++){
- if(books[i].getYear() >= year){
- b.add(books[i]);
- }
- }
- Book[] b1 = new Book[b.size()];
- for(int i = 0;i<b.size();i++){
- b1[i] = b.get(i);
- System.out.println(b1[i]);
- }
- return b1;
- }
- public static Book[] getBooks() {
- return books;
- }
- }
- public class Lab4 extends JFrame implements ListSelectionListener, ActionListener{
- final JList list;
- JList list1;
- JScrollPane scroller,scroller1;
- JPanel panel1,panel2,panel3,p11,p12,p13;
- JSlider slider;
- JLabel lblSlika,lblTitle,lblISBN,lblYear,lblPublisher;
- JTextField txtTitle,txtISBN,txtYear,txtPublisher;
- JButton addToCard;
- JTextArea card;
- Database ob;
- final DefaultListModel<Book> listModel;
- public Lab4(){
- ob.initDatabase();
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setLayout(new GridLayout(1,3));
- this.setSize(650,500);
- this.setTitle("Book");
- this.panel1=new JPanel();
- this.panel1.setBorder(BorderFactory.createTitledBorder("Book List:"));
- this.panel1.setLayout(new BorderLayout());
- this.list = new JList(ob.getBooks());
- this.list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- this.list.setLayoutOrientation(JList.VERTICAL);
- this.list.setBorder(BorderFactory.createLineBorder(Color.blue,1));
- this.list.setBorder(BorderFactory.createTitledBorder("Book List:"));
- this.list.addListSelectionListener(this);
- this.slider = new JSlider(JSlider.HORIZONTAL,2000,2013,2000);
- final JLabel pom = new JLabel();
- this.slider.addChangeListener(new ChangeListener(){
- @Override
- public void stateChanged(ChangeEvent arg0) {
- String str=String.format("%d",slider.getValue());
- pom.setText(str);
- int value = slider.getValue();
- list.setListData(Database.getBooks(value));
- }});
- this.p12=new JPanel();
- this.p12.setLayout(new BoxLayout(this.p12,BoxLayout.Y_AXIS));
- this.p12.add(slider);
- this.p12.add(pom);
- this.panel1.add(list,BorderLayout.NORTH);
- this.panel1.add(p12,BorderLayout.CENTER);
- this.panel2=new JPanel();
- this.panel2.setLayout(new GridLayout(2,1));
- this.lblSlika=new JLabel();
- this.p11=new JPanel();
- this.p11.setLayout(new BoxLayout(this.p11,BoxLayout.Y_AXIS));
- this.lblTitle=new JLabel("Title");
- this.lblISBN=new JLabel("ISBN");
- this.lblYear=new JLabel("Year");
- this.lblPublisher=new JLabel("Publisher");
- this.txtTitle=new JTextField(5);
- this.txtISBN=new JTextField(5);
- this.txtYear=new JTextField(5);
- this.txtPublisher=new JTextField(5);
- this.addToCard=new JButton("Add to card>>");
- this.addToCard.addActionListener(this);
- this.p11.add(this.lblTitle);
- this.p11.add(this.txtTitle);
- this.p11.add(this.lblISBN);
- this.p11.add(this.txtISBN);
- this.p11.add(this.lblYear);
- this.p11.add(this.txtYear);
- this.p11.add(this.lblPublisher);
- this.p11.add(this.txtPublisher);
- this.p11.add(this.addToCard);
- this.panel2.add(this.lblSlika);
- this.panel2.add(this.p11);
- this.panel3=new JPanel();
- this.panel3.setLayout(new BorderLayout());
- this.panel3.setBorder(BorderFactory.createTitledBorder("Ordered books:"));
- listModel = new DefaultListModel<Book>();
- list1 = new JList<Book>(listModel);
- this.list1.setLayoutOrientation(JList.VERTICAL);
- this.scroller1=new JScrollPane(this.list1);
- this.panel3.add(this.scroller1);
- this.add(this.panel1);
- this.add(this.panel2);
- this.add(this.panel3);
- }
- @Override
- public void valueChanged(ListSelectionEvent arg0) {
- // TODO Auto-generated method stub
- if(arg0.getValueIsAdjusting()==false)
- {
- Book ob2=(Book)list.getSelectedValue();
- this.txtTitle.setText(ob2.getTitle());
- this.txtISBN.setText(ob2.getISBN());
- String str=String.format("%d",ob2.getYear());
- this.txtYear.setText(str);
- this.txtPublisher.setText(ob2.getPublisher());
- ImageIcon ic= new ImageIcon("D:\\FINKI\\3 semestar\\IA\\Lab4\\pictures\\" + ob2.getPicture());
- Image img = ic.getImage().getScaledInstance(200, 190, java.awt.Image.SCALE_SMOOTH);
- ic = new ImageIcon(img);
- this.lblSlika.setIcon(ic);
- }
- }
- public static void main(String[] args) {
- Lab4 ob=new Lab4();
- ob.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent arg0) {
- listModel.addElement((Book)list.getSelectedValue());
- }
- }
Add Comment
Please, Sign In to add comment