QSwing vs. Swing

Simple Application Example

QSwing Swing

Code

package yu.ac.bg.etf.javaqx.demo.qsvs.qswing;

import yu.ac.bg.etf.javaqx.qswing.AbstractAction;
import yu.ac.bg.etf.javaqx.qswing.ImageIcon;
import yu.ac.bg.etf.javaqx.qswing.JQFileChooser;
import yu.ac.bg.etf.javaqx.qswing.JQFrame;
import yu.ac.bg.etf.javaqx.qswing.JQMenu;
import yu.ac.bg.etf.javaqx.qswing.JQMenuBar;
import yu.ac.bg.etf.javaqx.qswing.JQMenuItem;
import yu.ac.bg.etf.javaqx.qswing.JQTextArea;
import yu.ac.bg.etf.javaqx.qswing.JQToolBar;
import yu.ac.bg.etf.javaqx.qswing.JQToolButton;
import yu.ac.bg.etf.javaqx.qswing.QSwing;
import yu.ac.bg.etf.javaqx.qswing.events.ActionEvent;
import yu.ac.bg.etf.javaqx.qswing.text.PlainDocument;
import yu.ac.bg.etf.javaqx.qswingx.actions.ExitAction;

/**
 * Simple Application.
 */
public class SimpleApplication extends JQFrame {

  private final JQTextArea textArea;

  public SimpleApplication() {
    super("Simple Text Viewer");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JQMenuBar menuBar = setMenuBar(new JQMenuBar())// <-- SD
    JQMenu fileMenu =
                    menuBar.add(new JQMenu("File"))// <-- SD
    OpenAction openAction = new OpenAction();
    fileMenu.add(new JQMenuItem(openAction));
    fileMenu.addSeparator();
    fileMenu.add(new JQMenuItem(new ExitAction()))// <-- SD
    JQToolBar toolBar = addToolBar(new JQToolBar())// <-- SD
    toolBar.add(new JQToolButton(openAction));
    textArea = new JQTextArea();
    add(textArea)// <-- SD
  }

  void loadFile(String fileName) {
    PlainDocument document = new PlainDocument();
    if (document.readFrom(fileName)) { // <-- SD
      textArea.setDocument(document);
    }
  }

  class OpenAction extends AbstractAction {
    public OpenAction() {
      super("Open",
            new ImageIcon(
              SimpleApplication.class.
                getResource("../resources/OpenedFolder.png")));
      putValue(AbstractAction.SHORT_DESCRIPTION, "Open File");
    }
    public void actionPerformed(ActionEvent e) {
      String fileName =
        JQFileChooser.getOpenFileName(SimpleApplication.this);
      if (fileName != null) {
        loadFile(fileName);
      }
    }
  }

  public static void main(String[] args) {
    SimpleApplication simpleApplication =
                                       new SimpleApplication();
    simpleApplication.setBounds(100100200170);
    simpleApplication.setVisible(true);
    QSwing.startEventLoop()// <-- SD-
  }

}
package yu.ac.bg.etf.javaqx.demo.qsvs.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 * Simple Application.
 */
public class SimpleApplication extends JFrame {

  private final JTextArea textArea;

  public SimpleApplication() {
    super("Simple Text Viewer");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);
    OpenAction openAction = new OpenAction();
    fileMenu.add(new JMenuItem(openAction));
    fileMenu.addSeparator();
    fileMenu.add(new JMenuItem(new ExitAction()));
    JToolBar toolBar = new JToolBar();
    toolBar.setRollover(true);
    getContentPane().add(toolBar, BorderLayout.NORTH);
    JButton openButton = new JButton(openAction);
    openButton.setText(null);
    toolBar.add(openButton);
    textArea = new JTextArea();
    getContentPane().add(new JScrollPane(textArea));
  }

  void loadFile(String fileName) {
    BufferedReader reader = null;
    try {
      PlainDocument document = new PlainDocument();
      FileReader fileReader = new FileReader(fileName);
      reader = new BufferedReader(fileReader);
      char[] buffer = new char[4096];
      int read;
      while ((read = fileReader.read(buffer, 0,
                                     buffer.length)) != -1) {
        document.
            insertString(document.getLength(),
                         new String(buffer, 0, read)null);
      }
      textArea.setDocument(document);
    catch (BadLocationException ble) {
    catch (IOException ioe) {
    finally {
      if (reader != null) {
        try {
          reader.close();
        catch (IOException ex) {
        }
      }
    }
  }

  class OpenAction extends AbstractAction {
    public OpenAction() {
      super("Open",
            new ImageIcon(
              SimpleApplication.class.
                getResource("../resources/OpenedFolder.png")));
      putValue(AbstractAction.SHORT_DESCRIPTION, "Open File");
    }
    public void actionPerformed(ActionEvent e) {
      JFileChooser fileChooser = new JFileChooser();
      if (fileChooser.showOpenDialog(SimpleApplication.this==
          JFileChooser.APPROVE_OPTION) {
        loadFile(fileChooser.getSelectedFile().
                                            getAbsolutePath());
      }
    }
  }

  static class ExitAction extends AbstractAction {
    public ExitAction() {
      super("Exit");
    }
    public void actionPerformed(ActionEvent e) {
      System.exit(0);
    }
  }

  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
    catch (Throwable t) {
    }
    SimpleApplication simpleApplication =
                                       new SimpleApplication();
    simpleApplication.setBounds(100100200170);
    simpleApplication.setVisible(true);
  }

}

Screenshots