Question

the code is there for clearing the Frame area by clicking the sub menu(sub_menu_purchase and sub_menu_sale) of main menu.

public void clear() 
    { 

        Graphics g = getGraphics(); 

        Dimension d = getSize(); 

        g.setColor(Color.WHITE); 

        g.fillRect(0,0,d.width,d.height); 


    }
void sale()
    {
        lblinvoice =new JLabel("Invoice No. : ");
        lbldate =new JLabel("Date : ");
        lblform =new JLabel("From Party : ");
        lblto =new JLabel("To Party : ");

        txtto=new JTextField();
        txtfrom=new JTextField();

        btncancel=new JButton("Cancel");
        btnprint=new JButton("Print");
        btnreset=new JButton("Reset");
        btnsave=new JButton("Save");

        lblinvoice.setBounds(50,100,80,25);
        lbldate.setBounds(440,100,80,25);
        lblto.setBounds(50,135,80,25);
        txtto.setBounds(140,135,200,25);
        lblform.setBounds(50,170,80,25);
        txtfrom.setBounds(140,170,100,25);
        btnreset.setBounds(50,450,80,25);
        btnsave.setBounds(140,450,80,25);
        btnprint.setBounds(230,450,80,25);
        btncancel.setBounds(420,450,80,25);

        add(lblinvoice);
            add(lbldate);
            add(lblto);
                add(lblform);
                add(txtto);
                add(txtfrom);
                add(btncancel);
                add(btnprint);
            add(btnreset);
        add(btnsave);

        setVisible(true);

    }
void purchase()
    {
        lblinvoice =new JLabel("Invoice No. : ");
        lbldate =new JLabel("Date : ");
        lblparty =new JLabel("Party Name: ");

        txtparty=new JTextField();

        btncancel=new JButton("Cancel");
        btnprint=new JButton("Print");
        btnreset=new JButton("Reset");
        btnsave=new JButton("Save");

        lblinvoice.setBounds(50,100,80,25);
        lbldate.setBounds(440,100,80,25);
        lblparty.setBounds(50,135,80,25);
        txtparty.setBounds(140,135,200,25);
        btnreset.setBounds(50,450,80,25);
        btnsave.setBounds(140,450,80,25);
        btnprint.setBounds(230,450,80,25);
        btncancel.setBounds(420,450,80,25);

        add(lblinvoice);
            add(lbldate);
            add(lblparty);
                add(txtparty);
                add(btncancel);
                add(btnprint);
            add(btnreset);
        add(btnsave);

        setVisible(true);

    }
public void actionPerformed(ActionEvent event) //set up actionlistening
      {
          Object source=event.getSource();
          if (source.equals(sub_menu_purchase))
          { clear();
              purchase();
          }
          if (source.equals(sub_menu_sale))
          { clear();
              sale();
          }

     }

But it is not clear the area and override to one another. what code should I write?

Was it helpful?

Solution

There's a lot I would do differently from what you're doing, including

  • Don't get a component's Graphics via getGraphics(). The Graphics object thus obtained will not persist, and so it is not useful for making stable changes to a GUI's appearance.
  • Don't clear the GUI's graphics but rather change the view. Even if your code worked, the components would not have been removed from the GUI with your code. They would still exist and still sit on the GUI -- not good.
  • A CardLayout would work well for allowing you to swap a container's view, and is often used to swap JPanels, each holding its own GUI.
  • Avoid null layout and using setBounds(...) as this will lead to creation of GUI's that are a "witch" to upgrade and maintain and that look bad on all platforms except for one. Better to nest JPanels, each using its own simple layout, to achieve complex, beautiful and easy to maintain and improve GUI's.
  • Read/study the Swing tutorials as all of this is well explained there.

For example:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class UglyGui2 {
   private static final String SALE = "Sale";
   private static final String PURCHASE = "Purchase";

   private JMenuItem sub_menu_sale = new JMenuItem(SALE);
   private JMenuItem sub_menu_purchase = new JMenuItem(PURCHASE);
   private CardLayout cardLayout = new CardLayout();
   private JPanel cardPanel = new JPanel(cardLayout);
   private JPanel mainPanel = new JPanel(new BorderLayout(5, 5));

   public UglyGui2() {
      cardPanel.add(new JLabel(), "");
      cardPanel.add(createSalePanel(), SALE);
      cardPanel.add(createPurchasePanel(), PURCHASE);

      JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      buttonPanel.add(new JButton("Reset"));
      buttonPanel.add(new JButton("Save"));
      buttonPanel.add(new JButton("Print"));
      buttonPanel.add(new JLabel());
      buttonPanel.add(new JButton("Cancel"));

      mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      mainPanel.add(cardPanel, BorderLayout.CENTER);
      mainPanel.add(buttonPanel, BorderLayout.PAGE_END);

   }

   private JComponent createSalePanel() {
      JPanel salePanel = new JPanel(new GridBagLayout());
      salePanel.add(new JLabel("Sales"));
      salePanel.add(new JTextField(10));
      return salePanel;
   }

   private JComponent createPurchasePanel() {
      JPanel topPanel = new JPanel();
      topPanel.add(new JLabel("Purchases"));
      topPanel.add(new JTextField(10));

      JPanel purchasePanel = new JPanel(new BorderLayout());
      purchasePanel.add(topPanel, BorderLayout.PAGE_START);
      purchasePanel.add(new JScrollPane(new JTextArea(30, 60)), BorderLayout.CENTER);
      return purchasePanel;   }

   private Component getMainPanel() {
      return mainPanel;
   }

   private JMenuBar getJMenuBar() {
      ActionListener aListener = new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            cardLayout.show(cardPanel, e.getActionCommand());
         }
      };

      sub_menu_purchase.addActionListener(aListener);
      sub_menu_sale.addActionListener(aListener);

      JMenu menu = new JMenu("Menu");
      menu.add(sub_menu_purchase);
      menu.add(sub_menu_sale);

      JMenuBar menuBar = new JMenuBar();
      menuBar.add(menu);
      return menuBar;
   }

   private static void createAndShowGui() {
      UglyGui2 uglyGui = new UglyGui2();

      JFrame frame = new JFrame("Ugly Gui Example");
      frame.setJMenuBar(uglyGui.getJMenuBar());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(uglyGui.getMainPanel());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top