Заморочка слушателя мыши - Java

Узнай цену своей работы

Формулировка задачи:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
 
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
 
public class BookSearchWindow extends JFrame implements MouseListener, ActionListener{
 
    BooksData booksData = new BooksData();
    InformDataAll dataAll = new InformDataAll();
    private JPanel searchWindowPanel = new JPanel();
    private JPanel selectSearchPanel = new JPanel();
    private JPanel bookTablePanel = new JPanel();
    private JLabel bookSearchLabel = new JLabel("Search by book title");
    private JTextField bookSearchField = new JTextField();
    private JLabel autorSearchLabel = new JLabel("Search by autor");
    private JTextField autorSearchField = new JTextField();
    private JLabel janreSearchLabel = new JLabel("Search by autor");
    private JTextField janreSearchField = new JTextField();
    private JButton searchButton = new JButton("Search");
    private String bookArray[][];
    private String tableTittle[] = { "Book title", "Janre", "Autor", "Publisher" };
    private int row;
    private int column;
    private JTable bookTable;
    private JScrollPane scrollPane;
    private JOptionPane DataShowPane;
 
    public BookSearchWindow() {
        fillBookArray();
        fillBookTable();
        setSize(600, 800);
        setLayout(new FlowLayout());
        setLocationRelativeTo(null);
 
        searchWindowPanel.setPreferredSize(new Dimension(600, 800));
        searchWindowPanel.setLayout(new FlowLayout());
 
        selectSearchPanel.setPreferredSize(new Dimension(600, 300));
        selectSearchPanel.setLayout(null);
        
        bookSearchLabel.setBounds(225, 30, 150, 20);
        bookSearchLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        bookSearchLabel.setForeground(Color.BLACK);
        bookSearchLabel.setHorizontalAlignment((int) CENTER_ALIGNMENT);
 
        bookSearchField.setBounds(200, 55, 200, 30);
        bookSearchField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        
        autorSearchLabel.setBounds(250, 95, 100, 20);
        autorSearchLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        autorSearchLabel.setForeground(Color.BLACK);
        autorSearchLabel.setHorizontalAlignment((int) CENTER_ALIGNMENT);
 
        autorSearchField.setBounds(200, 125, 200, 30);
        autorSearchField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        
        janreSearchLabel.setBounds(250, 165, 100, 20);
        janreSearchLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        janreSearchLabel.setForeground(Color.BLACK);
        janreSearchLabel.setHorizontalAlignment((int) CENTER_ALIGNMENT);
 
        janreSearchField.setBounds(200, 195, 200, 30);
        janreSearchField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        
        searchButton.setBounds(250, 250, 100, 30);
        searchButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
 
        bookTablePanel.setPreferredSize(new Dimension(600, 500));
        bookTablePanel.setLayout(new FlowLayout());
 
        scrollPane = new JScrollPane(bookTable);
        scrollPane.setPreferredSize(new Dimension(570, 500));
 
        bookTable.addMouseListener(this);
        searchButton.addActionListener(this);
 
        selectSearchPanel.add(bookSearchLabel);
        selectSearchPanel.add(bookSearchField);
        selectSearchPanel.add(autorSearchLabel);
        selectSearchPanel.add(autorSearchField);
        selectSearchPanel.add(janreSearchLabel);
        selectSearchPanel.add(janreSearchField);
        selectSearchPanel.add(searchButton);
        bookTablePanel.add(scrollPane);
        searchWindowPanel.add(selectSearchPanel);
        searchWindowPanel.add(bookTablePanel);
        add(searchWindowPanel);
 
        setVisible(true);
    }
 
    public void fillBookArray() {
        booksData.fillBookList();
        bookArray = new String[booksData.getBookList().size()][4];
        for (int i = 0; i < bookArray.length; i++) {
            bookArray[i][0] = booksData.getBookList().get(i).getBookTitle();
            bookArray[i][1] = booksData.getBookList().get(i).getJanre();
            bookArray[i][2] = booksData.getBookList().get(i).getAutorName() + " "
                    + booksData.getBookList().get(i).getAutorSurname();
            bookArray[i][3] = booksData.getBookList().get(i).getPublisherName();
        }
    }
 
    public void fillBookTable() {
        bookTable = new javax.swing.JTable(bookArray, tableTittle) {
            @Override
            public boolean isCellEditable(int arg0, int arg1) {
                return false;
            }
            @Override
            public boolean getCellSelectionEnabled() {
                return true;
            }
        };
    }
    
    public void fillBookArrayAndTableBySearch () {
        for (int i = 0; i < bookArray.length; i++) {
            for (int j = 0; j < bookArray[i].length; j++) {
                bookArray[i][j] = null;
            }
        }
        if ((!bookSearchField.getText().equalsIgnoreCase("") && !autorSearchField.getText().equalsIgnoreCase(""))
                || (!bookSearchField.getText().equalsIgnoreCase("") && !janreSearchField.getText().equalsIgnoreCase(""))
                || (!janreSearchField.getText().equalsIgnoreCase("") && !autorSearchField.getText().equalsIgnoreCase(""))) {
            for (int i = 0; i < booksData.getBookList().size(); i++) {
                if ((booksData.getBookList().get(i).getBookTitle().contains(bookSearchField.getText())) 
                    &&  booksData.getBookList().get(i).getAutorName().contains(autorSearchField.getText())) {
                    for (int j = 0; j < bookArray.length; ) {
                        bookArray[j][0] = booksData.getBookList().get(i).getBookTitle();
                        bookArray[j][1] = booksData.getBookList().get(i).getJanre();
                        bookArray[j][2] = booksData.getBookList().get(i).getAutorName() + " "
                            + booksData.getBookList().get(i).getAutorSurname();
                        bookArray[j][3] = booksData.getBookList().get(i).getPublisherName();
                        j++;
                        break;
                    }   
                }
            }
        }
        fillBookTable();
    }
 
    public void selectAndshowData() {
        row = bookTable.getSelectedRow();
        column = bookTable.getSelectedColumn();
        if (column == 0) {
            for (int i = 0; i < dataAll.getBooksList().size(); i++) {
                if (bookArray[row][0].equals(dataAll.getBooksList().get(i).getBookTitle())) {
                    DataShowPane = new JOptionPane(dataAll.getBooksList().get(i));
                    JDialog dialogPane = DataShowPane.createDialog((JFrame) null, "Book Data");
                    dialogPane.setLocationRelativeTo(searchWindowPanel);
                    dialogPane.setVisible(true);
                }
            }
        } else if (column == 2) {
            String autorDataSplit[] = new String[2];
            autorDataSplit = bookArray[row][column].split(" ");
            for (int i = 0; i < dataAll.getAutorsList().size(); i++) {
                if (autorDataSplit[0].equals(dataAll.getAutorsList().get(i).getAutorName())
                        && autorDataSplit[1].equals(dataAll.getAutorsList().get(i).getAutorSurname())) {
                    DataShowPane = new JOptionPane(dataAll.getAutorsList().get(i));
                    JDialog dialogPane = DataShowPane.createDialog((JFrame) null, "Autor Data");
                    dialogPane.setLocationRelativeTo(searchWindowPanel);
                    dialogPane.setVisible(true);
                }
            }
        } else if (column == 3) {
            for (int i = 0; i < dataAll.getPublishersList().size(); i++) {
                if (bookArray[row][3].equals(dataAll.getPublishersList().get(i).getPublisherName())) {
                    DataShowPane = new JOptionPane(dataAll.getPublishersList().get(i));
                    JDialog dialogPane = DataShowPane.createDialog((JFrame) null, "Publisher Data");
                    dialogPane.setLocationRelativeTo(searchWindowPanel);
                    dialogPane.setVisible(true);
                }
            }
        }
    }
 
    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == bookTable) {
            System.out.println("l");
            selectAndshowData();
        }
    }
 
    @Override
    public void mousePressed(MouseEvent e) {
    }
 
    @Override
    public void mouseReleased(MouseEvent e) {
    }
 
    @Override
    public void mouseEntered(MouseEvent e) {
    }
 
    @Override
    public void mouseExited(MouseEvent e) {
    }
 
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == searchButton) {
            fillBookArrayAndTableBySearch();
            searchWindowPanel.repaint();
            validate();
        }
    }
}
Создал JTable в котором информация о книгах по клику на ячейку выдается детальная информация. Все работает нормально пока не произвожу поиск книги JTable обновляется и слушатель не реагирует на него

Решение задачи: «Заморочка слушателя мыши»

textual
Листинг программы
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class Test extends JFrame implements ActionListener{
    
    JButton button = new JButton("1");
    JPanel panel = new JPanel();
 
    public Test() {
        setSize(500, 500);
        setLayout(new FlowLayout());
        setLocationRelativeTo(null);
        panel.setPreferredSize(new Dimension(500, 500));
        button.addActionListener(this);
        
        
        panel.add(button);
        add(panel);
        setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button){
            panel.remove(button);
            button = new JButton("2");
            panel.add(button);
            validate();
                System.out.println("1, 2");
        }
    }
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 3.929 из 5