Home > Blockchain >  Is using file handling in GUI different from console?
Is using file handling in GUI different from console?

Time:03-07

I've been thinking about this for a while but I really can't figure out on how to make file handling work in GUI because as a beginner, I only used console up until now and GUI is pretty messy for me.

Can anyone explain it to me please?

CodePudding user response:

You only do error handling on your code side and you can use "try catch" or "if" statements

CodePudding user response:

This is an example of programming a GUI with AWT I pulled from here:

import java.awt.*;        // Using AWT container and component classes
import java.awt.event.*;  // Using AWT event classes and listener interfaces

// An AWT program inherits from the top-level container java.awt.Frame
public class AWTCounter extends Frame {
   private Label lblCount;    // Declare a Label component
   private TextField tfCount; // Declare a TextField component
   private Button btnCount;   // Declare a Button component
   private int count = 0;

   public AWTCounter () {
      setLayout(new FlowLayout());
      lblCount = new Label("Counter");  
      add(lblCount);                    

      tfCount = new TextField(count   "", 10); // TextField component with initial text
      tfCount.setEditable(false);       // read-only
      add(tfCount);                     // container (frame) adds TextField component

      btnCount = new Button("Count");   // Button component
      add(btnCount);                    // container (frame) adds Button component

      BtnCountListener listener = new BtnCountListener();
      btnCount.addActionListener(listener);
         // "btnCount" is the source object that fires an ActionEvent when clicked.

      setTitle("AWT Counter");  // container title
      setSize(300, 100);        // container initial window size
      setVisible(true);         // container shows
   }



   public static void main(String[] args) {
      // Invoke the constructor to setup the GUI
      AWTCounter app = new AWTCounter();
   }

   // class to handle the "Count" button-click
   private class BtnCountListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent evt) {
           count;
         tfCount.setText(count   "");
         // or invoke file-handling here
      }
   }
}

Your file-handling just happens at another point in your code when working with a GUI: eg when clicking a button or when the GUI is initialized, but the backend working is the same.

  • Related