JAVA LANGUAGE Write a program that displays editable bar gra

 JAVA LANGUAGE  Write a program that displays editable bar graphs. The GUI has two vertical panels.  The left panel contains textfields with numbers. The right panel contains corresponding  horizontal bar graphs that graphically show the value of the numbers on the left. Use a rectangle  with the width equal to the numbers from the textfield.   A screenshot is available at http://wisenet.fau.edu/class/cop4331/barcharts-screenshot.png  Use MVC and the Observer pattern. Store the numbers in the model. Attach the graph view as a  listener. When the user edits one of the numbers, the number controller should update the model and the model should notify the graph view that a changes has occured. Then, the graph  should get the number from the model and repaint itself.  Hints: use method Textfield.getText() to extract the text from a textfield.  Add a key listener to each textfield with method  Textfield.addKeyListener(KeyListener l) Interface KeyListener is documented at http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/KeyListener.html Class Textfield is documented at http://java.sun.com/j2se/1.5.0/docs/api/java/awt/TextField.html 

Solution

import java.awt.*;

import java.awt.event.*;

import java.net.URL;

import javax.swing.*;

/** Test setting Swing\'s JComponents properties and appearances */

@SuppressWarnings(\"serial\")

public class SwingJComponentSetterTest extends JFrame {

   // Image path relative to the project root (i.e., bin)

   private String imgCrossFilename = \"images/cross.gif\";

   private String imgNoughtFilename = \"images/nought.gif\";

   /** Constructor to setup the GUI */

   public SwingJComponentSetterTest() {

      // Prepare ImageIcons to be used with JComponents

      ImageIcon iconCross = null;

      ImageIcon iconNought = null;

      URL imgURL = getClass().getClassLoader().getResource(imgCrossFilename);

      if (imgURL != null) {

         iconCross = new ImageIcon(imgURL);

      } else {

         System.err.println(\"Couldn\'t find file: \" + imgCrossFilename);

      }

      imgURL = getClass().getClassLoader().getResource(imgNoughtFilename);

      if (imgURL != null) {

         iconNought = new ImageIcon(imgURL);

      } else {

         System.err.println(\"Couldn\'t find file: \" + imgNoughtFilename);

      }

      Container cp = getContentPane();

      cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

      // Create a JLabel with text and icon and set its appearances

      JLabel label = new JLabel(\"JLabel\", iconCross, SwingConstants.CENTER);

      label.setFont(new Font(Font.DIALOG, Font.ITALIC, 14));

      label.setOpaque(true); // needed for JLabel to show the background color

      label.setBackground(new Color(204, 238, 241)); // light blue

      label.setForeground(Color.RED);                 // foreground text color

    label.setPreferredSize(new Dimension(120, 80));

      label.setToolTipText(\"This is a JLabel\"); // Tool tip

      cp.add(label);

      // Create a JButton with text and icon and set its appearances

      JButton button = new JButton(); // use setter to set text and icon

      button.setText(\"Button\");

      button.setIcon(iconNought);

      button.setVerticalAlignment(SwingConstants.BOTTOM); // of text and icon

      button.setHorizontalAlignment(SwingConstants.RIGHT); // of text and icon

      button.setHorizontalTextPosition(SwingConstants.LEFT); // of text relative to icon

      button.setVerticalTextPosition(SwingConstants.TOP);    // of text relative to icon

      button.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15));

      button.setBackground(new Color(231, 240, 248));

      button.setForeground(Color.BLUE);

      button.setPreferredSize(new Dimension(150, 80));

      button.setToolTipText(\"This is a JButton\");

      button.setMnemonic(KeyEvent.VK_B); // can activate via Alt-B (buttons only)

      cp.add(button);

      // Create a JTextField with text and icon and set its appearances

      JTextField textField = new JTextField(\"Text Field\", 15);

      textField.setFont(new Font(Font.DIALOG_INPUT, Font.PLAIN, 12));

      textField.setForeground(Color.RED);

      textField.setHorizontalAlignment(JTextField.RIGHT); // Text alignment

      textField.setToolTipText(\"This is a JTextField\");

      cp.add(textField);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      setTitle(\"JComponent Test\");

      setLocationRelativeTo(null); // center window on the screen

      setSize(500, 150); // or pack()

      setVisible(true);

      // Print description of the JComponents via toString()

      System.out.println(label);

    System.out.println(button);

      System.out.println(textField);

   }

   /** The entry main() method */

   public static void main(String[] args) {

      // Run the GUI codes on Event-Dispatching thread for thread safety

      SwingUtilities.invokeLater(new Runnable() {

         @Override

         public void run() {

            new SwingJComponentSetterTest(); // Let the constructor do the job

         }

      });

   }

}

 JAVA LANGUAGE Write a program that displays editable bar graphs. The GUI has two vertical panels. The left panel contains textfields with numbers. The right pa
 JAVA LANGUAGE Write a program that displays editable bar graphs. The GUI has two vertical panels. The left panel contains textfields with numbers. The right pa
 JAVA LANGUAGE Write a program that displays editable bar graphs. The GUI has two vertical panels. The left panel contains textfields with numbers. The right pa

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site