2015-04-25 2 views
0

Я использую Eclipse, а моя база данных - hsqldb. У меня есть JFrame с 3 JPanel.Как обновить JList и JTextfield с помощью базы данных HSQLDB?

В моем первом JPanel у меня есть 3 JList, категория, бренды и продукт.

В моей базе данных у меня есть таблица категории (номер, имя) брендов (номер, имя) продукта (идентификатор, продукт, цена, описание, количество + внешний ключ на категории и брендах).

Я начал кодирование, но не могу этого сделать, когда я выбираю один элемент в категории и бренде JList, я должен поднять результат в продукте (один элемент или несколько), и когда я выбираю один элемент в Jlist Product, я должны привести результат в JPanel Information (цена, описание и количество).

Ниже код моего IHM

package IHM; 

import javax.swing.*; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 

import Donnees.Categories; 
import Donnees.CategoriesCellRenderer; 
import Donnees.CategoriesListModel; 
import Donnees.Marques; 
import Donnees.MarquesCellRenderer; 
import Donnees.MarquesListModel; 
import Donnees.Produits; 
import Donnees.ProduitsCellRenderer; 
import Donnees.ProduitsListModel; 
import Fabriques.FabCategories; 
import Fabriques.FabMarques; 

import java.awt.*; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 

public class Fenetre { 

static Connection conn; 

    public static void main(String[] args) throws ClassNotFoundException, SQLException { 
     Class.forName("org.hsqldb.jdbcDriver"); 
     conn=DriverManager.getConnection("jdbc:hsqldb:file:BDD/bdd","sa",""); 

     FabCategories.getInstance().demarrerConnexion(conn); 
     FabMarques.getInstance().demarrerConnexion(conn); 

     JFrame f = new JFrame("Gestion des Produits"); 
     f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     Container contentPane = f.getContentPane(); 
     contentPane.setLayout(new GridLayout(1,2,3, 3)); 

     JPanelProduit jPanelProduit = new JPanelProduit(); 
     JPanelInformations jPanelInformations = new JPanelInformations(); 
     JPanelVentes jPanelVentes = new JPanelVentes(); 

     jPanelProduit.setBackground(Color.GREEN); 
     jPanelProduit.setBackground(Color.YELLOW); 
     jPanelVentes.setBackground(Color.PINK); 

     contentPane.add(jPanelProduit); 
     contentPane.add(jPanelInformations); 
     contentPane.add(jPanelVentes); 

     f.setSize(700,700); 
     f.pack(); 
     f.setVisible(true); 
    } 
} 

class JPanelProduit extends JPanel implements ListSelectionListener { 

    public JPanelProduit() throws SQLException { 
     JPanel panelProduit = new JPanel(); 
     setLayout(new GridLayout(5,2,5,5)); 

     String labelCat = "Categories"; 
     String labelMark = "Marques";  
     String labelProd = "Produits"; 

     JList<Categories> listCategories= new JList<Categories>(); 
     JList<Marques> listMarques= new JList<Marques>(); 
     JList<Produits> listProduits= new JList<Produits>(); 

     JScrollPane listCategoriesScrollPane = new JScrollPane (listCategories);   

     add(new JLabel(labelCat)); 
     add(new JScrollPane(listCategoriesScrollPane)); 
     listCategories.setCellRenderer(new CategoriesCellRenderer());; 
     listCategories.setModel(new CategoriesListModel()); 
     listCategories.addListSelectionListener((this)); 

     add(new JLabel(labelMark)); 
     JScrollPane listMarquesScrollPane = new JScrollPane (listMarques);   
     add(new JScrollPane(listMarquesScrollPane)); 
     listMarques.setCellRenderer(new MarquesCellRenderer()); 
     listMarques.setModel(new MarquesListModel()); 
     listMarques.addListSelectionListener(this); 

     add(new JLabel(labelProd)); 
     JScrollPane listProduitScrollPane = new JScrollPane (listProduits);   
     add(new JScrollPane(listProduitScrollPane)); 
     listProduits.addListSelectionListener(this); 

    } 

    @Override 
    public void valueChanged(ListSelectionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
} 

class JPanelInformations extends JPanel { 

    public JPanelInformations() { 
     //JPanel PanelInformation = new JPanel(); remove new instance of panel 
     setLayout(new GridLayout(7,1,5,5)); 

     JLabel labelInfo = new JLabel ("INFORMATION"); 
     JLabel labelPrix = new JLabel ("Prix"); 
     JLabel labelDesc = new JLabel ("Description");  
     JLabel labelQuant = new JLabel ("Quantite");  
     JTextField fieldPrix = new JTextField (20); 
     JTextArea fieldDesc = new JTextArea (20, 20); 
     JTextField fieldQuantite = new JTextField (20); 

     add(labelInfo); //remove PanelInformation. 
     add(labelPrix);//remove PanelInformation. 
     add(fieldPrix);//remove PanelInformation. 
     add(labelDesc);//remove PanelInformation. 
     add(fieldDesc);//remove PanelInformation. 
     add(labelQuant);//remove PanelInformation. 
     add(fieldQuantite);//remove PanelInformation. 
    } 
} 

class JPanelVentes extends JPanel { 

    public JPanelVentes() { 
     //JPanel PanelVentes = new JPanel(); remove the new instance of JPanel 
     setLayout(new GridLayout(8,1,5,5)); 
     JLabel labelVendre = new JLabel ("VENDRE"); 
     JLabel labelQte = new JLabel ("Quantite"); 
     JLabel labelPromo = new JLabel ("Promotion"); 
     JLabel labelTot = new JLabel ("Total");  
     JTextField fieldQte = new JTextField (20); 
     JTextField fieldPromoEuros = new JTextField (20); 
     JTextField fieldPromoPourcent = new JTextField (20); 
     JTextField fieldTotal = new JTextField (20); 


     add (labelVendre); //remove PanelVentes 
     add (labelQte);//remove PanelVentes 
     add (fieldQte);//remove PanelVentes 
     add (labelPromo);//remove PanelVentes 
     add (fieldPromoEuros);//remove PanelVentes 
     add (fieldPromoPourcent);//remove PanelVentes 
     add (labelTot);//remove PanelVentes 
     add (fieldTotal);//remove PanelVentes 
    } 
} 
+0

Получите номер строки из выбранной строки из списка, а затем с номером ячейки вы можете получить детали в ячейках в виде строк. Затем вы можете передать эти значения в любое текстовое поле или метку по своему желанию. Код в методе щелчка таблицы. – Sanke

+0

Где я должен поставить код? У меня есть JPanel JPanelProduit, 3 addListSelectionListener? –

+0

добавьте код в событие MouseClicked. – Sanke

ответ

0

Я не загрузит значение моего элемента, но странное значение, например [email protected]

Как отобразить результат в моей JList Бренды

listCategories.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent arg0) { 
       Categories selectedCategories =  listCategories.getSelectedValue(); 
       System.out.println(selectedCategories); 
      } 
     }); 

     listMarques.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent arg0) { 
       Marques selectedMarques = listMarques.getSelectedValue(); 
       System.out.println(selectedMarques); 
      } 
     }); 
Смежные вопросы