i am making one ecomerce application. in my dashbord fram i have one search panal one product keeper scrollpanal and one manue panal i want that all my fatched product can be store inside product keeper and which can i scroll and my searchbar and manue will be at their place but i am getting my all product on one single row and it wven go out of screen . i dont know how to fix it .
here is my code snipets
User user;
JFrame frame = new JFrame();
JScrollPane productkeeper ;
JButton profileButton = new JButton(" 🙍Profile️ ");
JPanel logo = new TestPanel(new Color(118, 226, 229, 255), new Color(114, 197, 191, 255),new Color(114, 197, 191));
JButton logoutButton = new JButton(" Logout ");
JButton settingButton = new JButton("⚙️settings");
JButton helpButton = new JButton(" Help ");
JButton aboutButton = new JButton(" About ");
JButton exitButton = new JButton(" 🚪 Exit ");
JPanel menuPanel;
JPanel searchPanel;
private JPanel productPanel;
public Deshboard(User current_user) {
try {
UIManager.setLookAndFeel( new FlatLightLaf() );
UIManager.put( "Button.arc", 999 );
} catch( Exception ex ) {
System.err.println( "Failed to initialize LaF" );
}
this.user = current_user;
frame.setSize(800, 800);
productPanel = new JPanel();
productPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
menuPanel = new JPanel();
menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
profileButton.addActionListener(e->{
new Profile(user);
}) ;
menuPanel.add(profileButton);
menuPanel.add(logoutButton);
menuPanel.add(settingButton);
menuPanel.add(helpButton);
menuPanel.add(aboutButton);
menuPanel.add(exitButton);
menuPanel.setBackground(new Color(66, 64, 64, 255));
menuPanel.setPreferredSize(new Dimension(800,60));
logo.setPreferredSize(new Dimension(800,50));
logo.setOpaque(true);
logo.setBackground(Color.cyan);
searchPanel = new JPanel(new BorderLayout());
searchPanel.add(logo,BorderLayout.NORTH);
searchPanel.setPreferredSize(new Dimension(800,90));
JTextField searchBar = new IconTextField(new ImageIcon("src/resorce/searchlogo.jpg"));
searchBar.setBorder( new CompoundBorder(new EmptyBorder(2,10,2,10),new RoundedBorder(8)));
searchPanel.add(searchBar, BorderLayout.CENTER);
searchBar.setPreferredSize(new Dimension(400, 40));
searchBar.setMargin(new Insets(10, 10, 10, 10));
settingButton.setMargin(new Insets(10, 10, 10, 10));
exitButton.setMargin(new Insets(10, 10, 10, 10));
logoutButton.setMargin(new Insets(10, 10, 10, 10));
profileButton.setMargin(new Insets(10, 10, 10, 10));
helpButton.setMargin(new Insets(10, 10, 10, 10));
exitButton.setMargin(new Insets(10, 10, 10, 10));
aboutButton.setMargin(new Insets(10, 10, 10, 10));
JButton searchButton = new JButton("Search");
searchButton.setPreferredSize(new Dimension(100, 30));
searchButton.setBorder( new CompoundBorder(new EmptyBorder(5,10,5,10),new RoundedBorder(8)));
searchPanel.add(searchButton, BorderLayout.EAST);
JPanel productPanel = new ImagePanel("src/resorce/shopping-paper-bags-icon-isolated/JEMA GER 1722-04.jpg");
ArrayList<Product> products = getProducts();
for (int i = 0 ; i<products.size();i++) {
ProductCard productCard = new ProductCard(products.get(i));
productCard.setBorder(new EmptyBorder(10, 5, 10, 5));
int finalI = i;
productCard.addToWishlistButton.addActionListener(e -> {
new Product_add(false).Add_to_Wishlist(user.getName(), products.get(finalI).getName());
});
productCard.addToCartButton.addActionListener(e -> {
new Product_add(false).Add_to_Cart(user.getName(), products.get(finalI).getName());
});
Border combination = new CompoundBorder(new RoundedBorder(8),new ShadowBorder(3));
productCard.setBorder(combination);
productCard.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
productCard.setBackground(Color.lightGray);
}
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
System.out.println("clicked");
}
public void mouseExited(java.awt.event.MouseEvent evt) {
productCard.setBackground(Color.white);
}
}
);
productPanel.add(productCard);
}
productPanel.revalidate();
productPanel.repaint();
productkeeper = new JScrollPane(productPanel);
productkeeper.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
productkeeper.getVerticalScrollBar().setUnitIncrement(10);
frame.add(searchPanel,BorderLayout.NORTH);
frame.add(menuPanel,BorderLayout.SOUTH);
frame.add(productkeeper,BorderLayout.CENTER);
searchButton.addActionListener(e->{
updateProductPanel(getProducts(searchBar.getText()));
});
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void updateProductPanel(ArrayList<Product> products) {
frame.getContentPane().removeAll();
frame.setLayout(new BorderLayout());
productPanel = new ImagePanel("src/resorce/shopping-paper-bags-icon-isolated/JEMA GER 1722-04.jpg");
for (Product product : products) {
ProductCard productCard = new ProductCard(product);
productCard.addToWishlistButton.addActionListener(e -> {
new Product_add(false).Add_to_Wishlist(user.getName(), product.getName());
});
productCard.addToCartButton.addActionListener(e->{
new Product_add(false).Add_to_Cart(user.getName(), product.getName());
});
productPanel.add(productCard);
}
frame.add(menuPanel, BorderLayout.SOUTH);
frame.add(searchPanel, BorderLayout.NORTH);
frame.add(productPanel, BorderLayout.CENTER);
frame.revalidate();
frame.repaint();
}```