LoadSave: Button action : Button « Swing JFC « Java






LoadSave: Button action

LoadSave: Button action
  
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

public class LoadSave {

  public static void main(String args[]) {
    final String filename = "text.out";
    JFrame frame = new JFrame("Loading/Saving Example");
    Container content = frame.getContentPane();

    final JTextField textField = new JTextField();
    content.add(textField, BorderLayout.NORTH);

    JPanel panel = new JPanel();

    Action loadAction = new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        try {
          doLoadCommand(textField, filename);
        } catch (Exception e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }
      }
    };
    loadAction.putValue(Action.NAME, "Load");
    JButton loadButton = new JButton(loadAction);
    panel.add(loadButton);

    Action saveAction = new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        try {
          doSaveCommand(textField, filename);
        } catch (Exception e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }
      }
    };
    saveAction.putValue(Action.NAME, "Save");
    JButton saveButton = new JButton(saveAction);
    panel.add(saveButton);

    Action clearAction = new AbstractAction() {
      {
        putValue(Action.NAME, "Clear");
      }

      public void actionPerformed(ActionEvent e) {
        textField.setText("");
      }
    };
    JButton clearButton = new JButton(clearAction);
    panel.add(clearButton);

    content.add(panel, BorderLayout.SOUTH);

    frame.setSize(250, 150);
    frame.setVisible(true);
  }

  public static void doSaveCommand(JTextComponent textComponent, String filename) throws Exception {
    FileWriter writer = null;
    writer = new FileWriter(filename);
    textComponent.write(writer);
    writer.close();
  }

  public static void doLoadCommand(JTextComponent textComponent, String filename) throws Exception {
    FileReader reader = null;
    reader = new FileReader(filename);
    textComponent.read(reader, filename);
    reader.close();
  }
}

           
         
    
  








Related examples in the same category

1.Creating a JButton Component from Action
2.Various Swing buttonsVarious Swing buttons
3.Responding to button pressesResponding to button presses
4.Putting buttons on an appletPutting buttons on an applet
5.Uses reflection to create groups of different types of AbstractButtonUses reflection to create groups of different types of AbstractButton
6.The Swing-ified button exampleThe Swing-ified button example
7.Create a JButton that does not show focusCreate a JButton that does not show focus
8.Demonstration of button events including Action, Item and Change event typesDemonstration of button events including Action, Item and Change event types
9.The event demonstration program for JToggleButtonThe event demonstration program for JToggleButton
10.An example of radio button menu items in actionAn example of radio button menu items in action
11.JToggleButton DemoJToggleButton Demo
12.A Selected ButtonA Selected Button
13.Button demo: Mnemonic, alignment and action command Button demo: Mnemonic, alignment and action command
14.Button action to change the panel backgroundButton action to change the panel background
15.HTML buttonHTML button
16.Creating a Multiline Label for a JButton Component
17.Lines are left justified. This label text will center the lines
18.Label text italicizes the second line
19.Changing the Label of a JButton Component
20.Ploygon ButtonPloygon Button
21.Simple Button UISimple Button UI
22.Displaying a Button with an Icon LabelDisplaying a Button with an Icon Label
23.Displaying a Button with a BorderDisplaying a Button with a Border
24.Displaying a Button with Varying IconsDisplaying a Button with Varying Icons
25.Displaying a Button with Various Label AlignmentsDisplaying a Button with Various Label Alignments
26.Working with Tooltip TextWorking with Tooltip Text
27.Sharing Models between Buttons
28.Sharing an Action between JButton ComponentsSharing an Action between JButton Components
29.Display Message
30.Button Html DemoButton Html Demo
31.Swing Button DemoSwing Button Demo
32.Button with ICON and Multiline Button with ICON and Multiline
33.Action Button SampleAction Button Sample
34.Mnemonic SampleMnemonic Sample
35.Swing Default ButtonSwing Default Button
36.Button Action SampleButton Action Sample
37.Button icons, a default button, HTML in a button,and button mnemonics.
38.Adding a Rollover and Pressed Icon to a JButton Component
39.Adding a Disabled Icon to a JButton Component
40.Setting the Gap Size Between the Label and Icon in a JButton Component
41.Moving the Icon in a JButton Component
42.Adding an Icon to a JButton Component
43.Moving the Label/Icon Pair in a JButton Component
44.If the action does not have an icon or a different icon must be used, add or change the icon using setIcon():
45.Dynamically update the appearance of a component
46.Simple ToggleButton SampleSimple ToggleButton Sample