Hello there, I'm trying on Gui exercises. One of them requires a simple GUI class for Vehicle.
The GUI should have buttons for creating new SportsCar and new Van objects. and then should display the values of the instance variables of these variables.
The GUI should also calculate and display the fuel consumption and acceleration of the vehicle objects that have been added by the user.

For Vehicle class I, this is the code

public class Vehicle
{
     // instance variables - replace the example below with your own
    private double horsepower, aerodynamics, weight;

    public Vehicle(double initialHorsepower, double initialAerodynamics, double initialWeight)
    {
        // initialise instance variables
        horsepower = initialHorsepower;
        aerodynamics = initialAerodynamics;
        weight = initialWeight;
    }
    
    public double getHorsepower()
    {
        return horsepower;
    }
    
    public double getAerodynamics()
    {
       return aerodynamics;
    }   
    
    public double getWeight()
    {
        return weight;
    }
    
    public double getConsumption()
    {
        return (1000+(this.getWeight())/5)*(this.getAerodynamics())*(this.getHorsepower())/10000;
    }
    
    public double acceleration()
    {
        return ((100/this.horsepower)*this.aerodynamics*this.weight/100);
    }
    
}

Basically in this code there are methods to calculate Acceleration and Consumption

For the GUI,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FVehicle extends JFrame
{

public static void main()
{
  FVehicle driver = new FVehicle(); 

}

public FVehicle()
{
setTitle("Vehicle");
setVisible(true);

setSize(500,500);
setLayout(new GridLayout(2, 2));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

fire();
}

public void fire() 
{

JPanel panel = new JPanel();
add(panel);

JButton button = new JButton("Add Sports Car");
JButton button1 = new JButton("Add Van");
panel.add(button);
panel.add(button1);

button.addActionListener(new buttonlistener());
button1.addActionListener(new button1listener());

}



class buttonlistener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        JFrame frame2 = new JFrame("SportsCar");
        frame2.setVisible(true);
        frame2.setSize(300,300);

        JPanel panel = new JPanel();
        frame2.add(panel);
        JButton setSC = new JButton("Set Sports Car");
        panel.add(setSC);
        
        setSC.addActionListener(new setSportsCarListener());
    }
}



class button1listener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {

        JFrame frame3 = new JFrame("Van");
        frame3.setVisible(true);
        frame3.setSize(300,300);
        JLabel label1 = new JLabel("New Van");
        JPanel panel1 = new JPanel();
        frame3.add(panel1);
        panel1.add(label1);



    }
}


class setSportsCarListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        JFrame setSCFrame = new JFrame("SportsCar");
        setSCFrame.setVisible(true);
        setSCFrame.setSize(300,300);
        JPanel panelSC = new JPanel();
        setSCFrame.add(panelSC);
        
        
        TextField horsepowerField = new TextField();
        TextField weightField = new TextField();
        TextField topspeedField = new TextField();

        add(new Label("Weight"));
        add(weightField);
        weightField.addActionListener(new setSportsCarListener());
   
        add(new Label("Horsepower"));
        add(horsepowerField);
        horsepowerField.addActionListener(new setSportsCarListener());

   
        add(new Label("Topspeed"));
        add(topspeedField);
        topspeedField.addActionListener(new setSportsCarListener());
    }
}



}

This is what I have written so far. But I'm not sure whether this is correct. It's rather incomplete but it compiled.


My problems are:

I want to create a main frame which displays 2 buttons: Add New SportsCar and Add New Van
which I have already created.

And then when the user clicks Add New Sports Car, a new frame will appear with another button : Set New Sports Car and when the user clicks that, another frame will appear which will display several text fields.

These are for the user to input the horsepower, toppeed and weight of their desired new sports car ; which I have written but somehow doesn't appear like I thought it would. It comes out with several frames when I click add Set Sports Car

How do I link the new sports cars/vans created with the Vehicle class and then implement the formulas(consumption and acceleration) I have written in the class?

Recommended Answers

All 8 Replies

welcome on this forum

public static void main(String argv[]) {
        FVehicle driver = new FVehicle();
    }

I have changed the code for class FVehicle to this

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
public class FVehicle 
{

public static void main(String[] args)
{
  JFrame frame = new JFrame("Vehicle");
  frame.setVisible(true);
  frame.setTitle("Vehicle");
  frame.setSize(500,500);
  frame.setLayout(new GridLayout(2, 2));
  
  JPanel panel = new JPanel();
  frame.add(panel);
  JButton button = new JButton("Add Sports Car");
  JButton button1 = new JButton("Add Van");
  panel.add(button);
  panel.add(button1);
  button.addActionListener(new buttonlistener());
  button1.addActionListener(new button1listener());
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  

}

  static class buttonlistener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
        JFrame frame2 = new JFrame("SportsCar");
        frame2.setVisible(true);
        frame2.setSize(300,300);

        JPanel panel = new JPanel();
        frame2.add(panel);
        JButton setSC = new JButton("Set Sports Car");
        panel.add(setSC);
        
        setSC.addActionListener(new setSportsCarListener());
    }
  }



  static class button1listener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {

        JFrame frame3 = new JFrame("Van");
        frame3.setVisible(true);
        frame3.setSize(300,300);
        JLabel label1 = new JLabel("New Van");
        JPanel panel1 = new JPanel();
        frame3.add(panel1);
        panel1.add(label1);



    }
 }


static class setSportsCarListener implements ActionListener
{
    
   
    public void actionPerformed(ActionEvent e)
    {
        JFrame setSCFrame = new JFrame("SportsCar");
        setSCFrame.setVisible(true);
        setSCFrame.setSize(300,300);
        JPanel panelSC = new JPanel();
        setSCFrame.add(panelSC);
        
        
        TextField horsepowerField = new TextField();
        TextField weightField = new TextField();
        TextField topspeedField = new TextField();
        
        add(new Label("Weight"));
        add(weightField);
        weightField.addActionListener(new setSportsCarListener());
   
        add(new Label("Horsepower"));
        add(horsepowerField);
        horsepowerField.addActionListener(new setSportsCarListener());

   
        add(new Label("Topspeed"));
        add(topspeedField);
        topspeedField.addActionListener(new setSportsCarListener());
    }
}



}

But then I got this error saying
cannot find symbol - method add(java.awt.Label)

have to add panelSC.add...., lines at 87,88,91,92,96,97

change your main method to

public static void main(String[] args)
{
  JPanel panel = new JPanel();
  JButton button = new JButton("Add Sports Car");
  JButton button1 = new JButton("Add Van");
  panel.add(button);
  panel.add(button1);
  button.addActionListener(new buttonlistener());
  button1.addActionListener(new button1listener());
  JFrame frame = new JFrame("Vehicle");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //frame.setTitle("Vehicle");
  frame.setLayout(new BorderLayout(2, 2));
  frame.add(panel, BorderLayout.CENTER);
  frame.setPreferredSize(new Dimension(500,500));
  frame.pack();
  frame.setVisible(true);
  

}

sorry but first code was better than second, its ... and bad, my 2cent for that

every click create a new JFrame and without DefaultCloseOperation, that road to the hell, remove each of JFrame (inside ActionListener) and replace only with one JDialog for whole class, be sure that with myDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE), if you want to show that then call myDialog.setVisible(true);

What do you mean by this? I'm not quite sure

remove each of JFrame (inside ActionListener) and replace only with one JDialog for whole class, be sure that with myDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE)

this is what my code currently looks like

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FVehicle 
{

public static void main(String[] args)
{
  JFrame frame = new JFrame("Vehicle");
  frame.setVisible(true);
  frame.setTitle("Vehicle");
  frame.setSize(500,500);
  frame.setLayout(new GridLayout(2, 2));
  
  JPanel panel = new JPanel();
  frame.add(panel);
  JButton button = new JButton("Add Sports Car");
  JButton button1 = new JButton("Add Van");
  panel.add(button);
  panel.add(button1);
  button.addActionListener(new buttonlistener());
  button1.addActionListener(new button1listener());
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  

}

  static class buttonlistener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
        JFrame frame2 = new JFrame("SportsCar");
        frame2.setVisible(true);
        frame2.setSize(300,300);
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JPanel panel = new JPanel();
        frame2.add(panel);
        JButton setSC = new JButton("Set Sports Car");
        panel.add(setSC);
        
        setSC.addActionListener(new setSportsCarListener());
    }
  }



  static class button1listener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {

        JFrame frame3 = new JFrame("Van");
        frame3.setVisible(true);
        frame3.setSize(300,300);
        frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label1 = new JLabel("New Van");
        JPanel panel1 = new JPanel();
        frame3.add(panel1);
        panel1.add(label1);



    }
 }


static class setSportsCarListener implements ActionListener
{
    
   
    public void actionPerformed(ActionEvent e)
    {
        JFrame setSCFrame = new JFrame("SportsCar");
        setSCFrame.setVisible(true);
        setSCFrame.setSize(300,300);
        setSCFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panelSC = new JPanel();
        setSCFrame.add(panelSC);
        

        TextField horsepowerField = new TextField();
        TextField weightField = new TextField();
        TextField topspeedField = new TextField();
        
       // setLayout(new GridLayout(2, 2));
        panelSC.add(new Label("Weight"));
        panelSC.add(weightField);
        weightField.addActionListener(new setSportsCarListener());
   
        panelSC.add(new Label("Horsepower"));
        panelSC.add(horsepowerField);
        horsepowerField.addActionListener(new setSportsCarListener());

   
        panelSC.add(new Label("Topspeed"));
        panelSC.add(topspeedField);
        topspeedField.addActionListener(new setSportsCarListener());
    }
}



}

SO BASICALLY I REMODELED EVERYTHING AND NOW THE Final Code is this

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class FVehicle2 {
  public static void main(String[] args) {
    JFrame frame = 
      new JFrame("Vehicle");
    frame.setSize(500, 500);
    frame.setVisible(true);
    
    JPanel panel = new JPanel();
    frame.add(panel);
    
    JButton addSC = new JButton("Add Sports Car");
    JButton addV = new JButton("Add Van");
    panel.add(addSC);
    panel.add(addV);
    addSC.addActionListener(new SCButtonlistener());
    addV.addActionListener(new VanButtonlistener());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}


class SportsCar
{
    public static void main(String[] args) {
    JFrame frame1 = 
      new SportsCarFrame("Sports Car");
    frame1.setSize(500, 500);
    frame1.setVisible(true);
  }
}
class SportsCarFrame extends JFrame {
  private TextField horsepowerField = new TextField();
  private TextField aerodynamicsField = new TextField();
  private TextField weightField = new TextField();
  // Constructor
  public SportsCarFrame() {
   
    setLayout(new GridLayout(2, 2));

    add(new Label("Horsepower"));
    add(horsepowerField);
    horsepowerField.addActionListener(new AccelerationListener());
    horsepowerField.addActionListener(new ConsumptionListener()); 

    add(new Label("Aerodynamics"));
    add(aerodynamicsField);
    aerodynamicsField.addActionListener(new AccelerationListener()); 
    aerodynamicsField.addActionListener(new ConsumptionListener()); 
    
    add(new Label("Weight"));
    add(weightField);
    weightField.addActionListener(new AccelerationListener());
    weightField.addActionListener(new ConsumptionListener()); 
    // Attach window listener
    addWindowListener(new WindowCloser());
  }

class Van
{
    public static void main(String[] args) {
    JFrame frame2 = 
      new VanFrame("Van");
    frame1.setSize(500, 500);
    frame1.setVisible(true);
  }
}

class VanFrame 
{
  private TextField horsepowerField = new TextField();
  private TextField aerodynamicsField = new TextField();
  private TextField weightField = new TextField();
  // Constructor
  public VanFrame() {
   
    setLayout(new GridLayout(2, 2));

    add(new Label("Horsepower"));
    add(horsepowerField);
    horsepowerField.addActionListener(new AccelerationListener());
    horsepowerField.addActionListener(new ConsumptionListener()); 

    add(new Label("Aerodynamics"));
    add(aerodynamicsField);
    aerodynamicsField.addActionListener(new AccelerationListener()); 
    aerodynamicsField.addActionListener(new ConsumptionListener()); 
    
    add(new Label("Weight"));
    add(weightField);
    weightField.addActionListener(new AccelerationListener());
    weightField.addActionListener(new ConsumptionListener()); 
    // Attach window listener
    addWindowListener(new WindowCloser());
}
  
  class AccelerationListener implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
      String HPString = horsepowerField.getText();
      double horsepower = Double.parseDouble(HPString);
      
      String AEString = aerodynamicsField.getText();
      double aerod = Double.parseDouble(AEString);
      
      String WString = weightField.getText();
      double weight = Double.parseDouble(WString);
      
      double calculate = ((100/horsepower)*aerod*weight/100);
    }
  }

 
  class ConsumptionListener implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
      String HPString = horsepowerField.getText();
      double horsepower = Double.parseDouble(HPString);
      
      String AEString = aerodynamicsField.getText();
      double aerod = Double.parseDouble(AEString);
      
      String WString = weightField.getText();
      double weight = Double.parseDouble(WString);
      
      double calculate = (1000+(weight)/5)*(aerodynamics)*(horsepower)/10000;
    }
  }

  // Listener for window
  class WindowCloser extends WindowAdapter 
  {
    public void windowClosing(WindowEvent evt) 
    {
      System.exit(0);
    }
  }
}

}

but now I am confused how do I insert the new SCButtonlistener
and new VanButtonlistener without causing anything to crash. I somehow missed those 2 listeners

huuuh, what are you tried, I'm not your debuger, again your fist post contains good code, other are very bad, terrible, against all nature rules

with that you can start

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class NewClass {

    private JFrame myFrame;
    private JPanel myFramePanel;
    private JPanel myFrameSouthPanel;
    private JButton southPanelButton1 = new JButton(" southPanelButton1 ");
    private JButton southPanelButton2 = new JButton(" southPanelButton2 ");
    private JButton southPanelHideButton3 = new JButton(" southPanelHideButton3 ");
    private JButton southPanelHideButton4 = new JButton(" southPanelHideButton4 ");
//
    private JDialog myDialog;
    private JPanel myDialogPanel;
    private JPanel myDialogSouthPanel;
    private JButton hideDialogSouthPanelButton = new JButton("Hide Details ");
    private JButton hideDialogSouthPanelButton2 = new JButton("Hide Details ");
//    
    private JPanel myDialogFirstCenterPanel;
    private final JLabel firstValueLabel = new JLabel(" Amount : ");
    private JFormattedTextField firstFormattedTextField;
    private final double firstValue = 0.00;
    private NumberFormat firstNumberFormat;
    private final JLabel secondValueLabel = new JLabel(" Another Amount : ");
    private JFormattedTextField secondFormattedTextField;
    private final double secondValue = 0.00;
    private NumberFormat secondNumberFormat;
    private final JLabel thirdValueLabel = new JLabel(" Adjust Amount : ");
    private JFormattedTextField thirdFormattedTextField;
    private final double thirdValue = 0.00;
    private NumberFormat thirdNumberFormat;
    private final JLabel fourthValueLabel = new JLabel(" Round Amount : ");
    private JFormattedTextField fourthFormattedTextField;
    private final double fourthValue = 0.00;
    private NumberFormat fourthNumberFormat;
    private final JLabel sumValueLabel = new JLabel(" Sum : ");
    private JFormattedTextField sumFormattedTextField;
    private final double sumValue = 0.00;
    private NumberFormat sumNumberFormat;
//    
    private JPanel myDialogSecondCenterPanel;
//
    private Point location;

    public NewClass() {
        southPanelButton1.setFont(new Font("Serif", Font.BOLD, 14));
        southPanelButton1.setForeground(Color.RED);
        southPanelButton1.addActionListener(new ButtonListener());
        southPanelButton2.setFont(new Font("Serif", Font.BOLD, 14));
        southPanelButton2.setForeground(Color.RED);
        southPanelButton2.addActionListener(new ButtonListener());
        southPanelHideButton3.setFont(new Font("Serif", Font.BOLD, 14));
        southPanelHideButton3.setForeground(Color.RED);
        southPanelHideButton4.setFont(new Font("Serif", Font.BOLD, 14));
        southPanelHideButton4.setForeground(Color.RED);
        myFrameSouthPanel = new JPanel();
        myFrameSouthPanel.setBackground(Color.GRAY);
        myFrameSouthPanel.setLayout(new GridLayout(0, 4));
        myFrameSouthPanel.setPreferredSize(new Dimension(800, 40));
        myFrameSouthPanel.setName("myFrameSouthPanel");
        myFrameSouthPanel.add(southPanelHideButton3);
        myFrameSouthPanel.add(southPanelButton1);
        myFrameSouthPanel.add(southPanelHideButton4);
        myFrameSouthPanel.add(southPanelButton2);
        southPanelHideButton3.setVisible(false);
        southPanelHideButton4.setVisible(false);
        myFramePanel = new JPanel();
        myFramePanel.setBackground(Color.LIGHT_GRAY);
        myFramePanel.setLayout(new BorderLayout(10, 10));
        myFramePanel.setPreferredSize(new Dimension(800, 600));
        myFramePanel.setName("myFramePanel");
        myFramePanel.add(myFrameSouthPanel, BorderLayout.SOUTH);
        createDialog();
        JPanel northpanel = new JPanel();
        JPanel southpanel = new JPanel();
        JPanel westpanel = new JPanel();
        JPanel eastpanel = new JPanel();
        myFrame = new JFrame("myFrame");
        myFrame.setLayout(new BorderLayout(3, 3));
        myFrame.add(northpanel, BorderLayout.NORTH);
        myFrame.add(southpanel, BorderLayout.SOUTH);
        myFrame.add(westpanel, BorderLayout.WEST);
        myFrame.add(eastpanel, BorderLayout.EAST);
        myFrame.add(myFramePanel, BorderLayout.CENTER);
        myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        myFrame.setPreferredSize(new Dimension(520, 240));
        myFrame.setLocation(250, 250);
        myFrame.pack();
        visibleFrame();
    }

    private void createDialog() {
        Border line, raisedbevel, loweredbevel, title, empty;
        line = BorderFactory.createLineBorder(Color.black);
        raisedbevel = BorderFactory.createRaisedBevelBorder();
        loweredbevel = BorderFactory.createLoweredBevelBorder();
        title = BorderFactory.createTitledBorder("");
        empty = BorderFactory.createEmptyBorder(4, 4, 4, 4);
        Border compound;
        Color borderColor = Color.blue;
        compound = BorderFactory.createCompoundBorder(empty, new RoundedBorderLineBlue(borderColor));
        setUpFormats();
//
        myDialogFirstCenterPanel = new JPanel();
        myDialogFirstCenterPanel.setPreferredSize(new Dimension(300, 200));
        myDialogFirstCenterPanel.setBackground(Color.MAGENTA);
        myDialogFirstCenterPanel.setBorder(compound);
        myDialogFirstCenterPanel.setLayout(new GridLayout(0, 4));
        myDialogFirstCenterPanel.setName("myDialogPanel");
        myDialogFirstCenterPanel.add(hideDialogSouthPanelButton2);
        myDialogFirstCenterPanel.add(hideDialogSouthPanelButton);
//
        firstValueLabel.setFont(new Font("Serif", Font.BOLD, 12));
        firstValueLabel.setForeground(Color.black);
        firstValueLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        firstFormattedTextField = new JFormattedTextField(firstNumberFormat);
        firstFormattedTextField.setValue(firstValue);
        firstFormattedTextField.setFont(new Font("Serif", Font.BOLD, 14));
        firstFormattedTextField.setForeground(Color.DARK_GRAY);
        firstFormattedTextField.setBackground(Color.orange);
        firstFormattedTextField.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        firstFormattedTextField.addFocusListener(fcsListener);
        secondValueLabel.setFont(new Font("Serif", Font.BOLD, 12));
        secondValueLabel.setForeground(Color.black);
        secondValueLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        secondFormattedTextField = new JFormattedTextField(secondNumberFormat);
        secondFormattedTextField.setValue(secondValue);
        secondFormattedTextField.setFont(new Font("Serif", Font.BOLD, 14));
        secondFormattedTextField.setForeground(Color.DARK_GRAY);
        secondFormattedTextField.setBackground(Color.orange);
        secondFormattedTextField.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        secondFormattedTextField.addFocusListener(fcsListener);
        thirdValueLabel.setFont(new Font("Serif", Font.BOLD, 12));
        thirdValueLabel.setForeground(Color.black);
        thirdValueLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        thirdFormattedTextField = new JFormattedTextField(thirdNumberFormat);
        thirdFormattedTextField.setValue(thirdValue);
        thirdFormattedTextField.setFont(new Font("Serif", Font.BOLD, 14));
        thirdFormattedTextField.setForeground(Color.DARK_GRAY);
        thirdFormattedTextField.setBackground(Color.orange);
        thirdFormattedTextField.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        thirdFormattedTextField.addFocusListener(fcsListener);
        fourthValueLabel.setFont(new Font("Serif", Font.BOLD, 12));
        fourthValueLabel.setForeground(Color.black);
        fourthValueLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        fourthFormattedTextField = new JFormattedTextField(fourthNumberFormat);
        fourthFormattedTextField.setValue(fourthValue);
        fourthFormattedTextField.setFont(new Font("Serif", Font.BOLD, 14));
        fourthFormattedTextField.setForeground(Color.DARK_GRAY);
        fourthFormattedTextField.setBackground(Color.orange);
        fourthFormattedTextField.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        fourthFormattedTextField.addFocusListener(fcsListener);
        sumValueLabel.setFont(new Font("Serif", Font.BOLD, 12));
        sumValueLabel.setForeground(Color.black);
        sumValueLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        sumFormattedTextField = new JFormattedTextField(sumNumberFormat);
        sumFormattedTextField.setValue(sumValue);
        sumFormattedTextField.setFont(new Font("Serif", Font.BOLD, 14));
        sumFormattedTextField.setForeground(Color.DARK_GRAY);
        sumFormattedTextField.setBackground(Color.orange);
        sumFormattedTextField.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        sumFormattedTextField.addFocusListener(fcsListener);
        sumFormattedTextField.setDisabledTextColor(Color.RED);
        sumFormattedTextField.setEnabled(false);
        firstFormattedTextField.getDocument().addDocumentListener(documentListener);
        secondFormattedTextField.getDocument().addDocumentListener(documentListener);
        thirdFormattedTextField.getDocument().addDocumentListener(documentListener);
        fourthFormattedTextField.getDocument().addDocumentListener(documentListener);
        myDialogSecondCenterPanel = new JPanel();
        myDialogSecondCenterPanel.setPreferredSize(new Dimension(800, 600));
        myDialogSecondCenterPanel.setBackground(Color.CYAN);
        myDialogSecondCenterPanel.setBorder(compound);
        myDialogSecondCenterPanel.setLayout(new GridLayout(5, 2));
        myDialogSecondCenterPanel.setName("myDialogPanel");
        myDialogSecondCenterPanel.add(firstValueLabel);
        myDialogSecondCenterPanel.add(firstFormattedTextField);        
        myDialogSecondCenterPanel.add(secondValueLabel);
        myDialogSecondCenterPanel.add(secondFormattedTextField);
        myDialogSecondCenterPanel.add(thirdValueLabel);
        myDialogSecondCenterPanel.add(thirdFormattedTextField);
        myDialogSecondCenterPanel.add(fourthValueLabel);
        myDialogSecondCenterPanel.add(fourthFormattedTextField);
        myDialogSecondCenterPanel.add(sumValueLabel);
        myDialogSecondCenterPanel.add(sumFormattedTextField);
//
        hideDialogSouthPanelButton.setFont(new Font("Serif", Font.BOLD, 14));
        hideDialogSouthPanelButton.setForeground(Color.BLUE);
        hideDialogSouthPanelButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hideDialogSouthPanelButtonActionPerformed(evt);
            }

            private void hideDialogSouthPanelButtonActionPerformed(ActionEvent evt) {
                myDialog.setVisible(false);
            }
        });
        hideDialogSouthPanelButton2.setFont(new Font("Serif", Font.BOLD, 14));
        hideDialogSouthPanelButton2.setForeground(Color.BLUE);
        myDialogSouthPanel = new JPanel();
        myDialogSouthPanel.setPreferredSize(new Dimension(685, 45));
        myDialogSouthPanel.setBackground(Color.YELLOW);
        myDialogSouthPanel.setBorder(compound);
        myDialogSouthPanel.setLayout(new GridLayout(0, 4));
        myDialogSouthPanel.setName("myDialogPanel");
        myDialogSouthPanel.add(hideDialogSouthPanelButton2);
        myDialogSouthPanel.add(hideDialogSouthPanelButton);
        hideDialogSouthPanelButton2.setVisible(false);
        myDialogPanel = new JPanel();
        myDialogPanel.setBackground(Color.ORANGE);
        myDialogPanel.setBorder(compound);
        myDialogPanel.setLayout(new BorderLayout(0, 3));
        myDialogPanel.setName("myDialogPanel");
        myDialogPanel.add(myDialogSouthPanel, BorderLayout.SOUTH);
        myDialogPanel.add(myDialogFirstCenterPanel, BorderLayout.CENTER);
        myDialogPanel.add(myDialogSecondCenterPanel, BorderLayout.CENTER);
        myDialog = new JDialog();
        myDialog.setName("myDialog");
        myDialog.setLayout(new BorderLayout());
        myDialog.add(myDialogPanel, BorderLayout.CENTER);
        myDialog.setPreferredSize(new Dimension(685, 250));
        myDialog.setAlwaysOnTop(true);
        myDialog.setUndecorated(true);
        myDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        myDialog.pack();
        myDialog.setVisible(false);
    }

    private void visibleFrame() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                myFrame.setVisible(true);
            }
        });
    }

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent evt) {
            Object obj = evt.getSource();
            nullValue();
            myDialog.setVisible(false);
            myDialogFirstCenterPanel.setVisible(false);
            myDialogSecondCenterPanel.setVisible(false);
            if (obj == southPanelButton1) {
                myDialogFirstCenterPanel.setPreferredSize(new Dimension(300, 200));
                myDialogPanel.add(myDialogFirstCenterPanel, BorderLayout.CENTER);
                myDialogFirstCenterPanel.setVisible(true);
                myDialogPanel.validate();
                myDialogPanel.repaint();
                myDialog.setPreferredSize(new Dimension(500, 300));
                visibleDialog();
            } else if (obj == southPanelButton2) {
                myDialogSecondCenterPanel.setPreferredSize(new Dimension(800, 600));
                myDialogPanel.add(myDialogSecondCenterPanel, BorderLayout.CENTER);
                myDialogSecondCenterPanel.setVisible(true);
                myDialogPanel.validate();
                myDialogPanel.repaint();
                myDialog.setPreferredSize(new Dimension(600, 400));
                visibleDialog();
            }
        }
    }

    private void visibleDialog() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                myDialog.pack();
                location = myFrame.getLocationOnScreen();
                int x = location.x;
                int y = location.y;
                myDialog.setLocation(x + 5, y + 25);
                myDialog.setVisible(true);
            }
        });
    }

    private class RoundedBorderLineBlue extends AbstractBorder {

        private final static int MARGIN = 5;
        private static final long serialVersionUID = 1L;
        private Color color;

        public RoundedBorderLineBlue(Color clr) {
            color = clr;
        }

        public void setColor(Color clr) {
            color = clr;
        }

        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setColor(color);
            g.drawRoundRect(x, y, width, height, MARGIN, MARGIN);
        }

        @Override
        public Insets getBorderInsets(Component c) {
            return new Insets(MARGIN, MARGIN, MARGIN, MARGIN);
        }

        @Override
        public Insets getBorderInsets(Component c, Insets insets) {
            insets.left = MARGIN;
            insets.top = MARGIN;
            insets.right = MARGIN;
            insets.bottom = MARGIN;
            return insets;
        }
    }
    //
    private FocusListener fcsListener = new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            dumpInfo(e);
        }

        @Override
        public void focusLost(FocusEvent e) {
            //dumpInfo(e);
        }

        private void dumpInfo(FocusEvent e) {
            //System.out.println("Source  : " + name(e.getComponent()));
            //System.out.println("Opposite : " + name(e.getOppositeComponent()));
            //System.out.println("Temporary: " + e.isTemporary());
            Component c = e.getComponent();
            if (c instanceof JFormattedTextField) {
                ((JFormattedTextField) c).requestFocus();
                ((JFormattedTextField) c).setText(((JFormattedTextField) c).getText());
                ((JFormattedTextField) c).selectAll();
            } else if (c instanceof JTextField) {
                ((JTextField) c).requestFocus();
                ((JTextField) c).setText(((JTextField) c).getText());
                ((JTextField) c).selectAll();
            }
        }

        private String name(Component c) {
            return (c == null) ? null : c.getName();
        }
    };
//
    private DocumentListener documentListener = new DocumentListener() {

        public void changedUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        public void insertUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        public void removeUpdate(DocumentEvent documentEvent) {
            printIt(documentEvent);
        }

        private void printIt(DocumentEvent documentEvent) {
            DocumentEvent.EventType type = documentEvent.getType();
            String typeString = null;
            if (type.equals(DocumentEvent.EventType.CHANGE)) {
                typeString = "Change";
            } else if (type.equals(DocumentEvent.EventType.INSERT)) {
                typeString = "Insert";
            } else if (type.equals(DocumentEvent.EventType.REMOVE)) {
                typeString = "Remove";
            }
            if ((typeString.equals("Error"))) {
                //System.out.print("Type  :   " + typeString + " / ");
            }
            Document source = documentEvent.getDocument();
            int length = source.getLength();
            try {
                if (((firstFormattedTextField.getText().length()) > 0)) {
                    if (((secondFormattedTextField.getText().length()) > 0)) {
                        if (((thirdFormattedTextField.getText().length()) > 0)) {
                            if (((fourthFormattedTextField.getText().length()) > 0)) {
                                double first = (((Number) firstFormattedTextField.getValue()).doubleValue());
                                double second = (((Number) secondFormattedTextField.getValue()).doubleValue());
                                double third = (((Number) thirdFormattedTextField.getValue()).doubleValue());
                                double fourth = (((Number) fourthFormattedTextField.getValue()).doubleValue());
                                double sumDouble = (((first + second) * third) / fourth);
                                sumFormattedTextField.setValue(sumDouble);
                            } else {
                                sumFormattedTextField.setValue(0.0000);
                            }
                        } else {
                            sumFormattedTextField.setValue(0.0000);
                        }
                    } else {
                        sumFormattedTextField.setValue(0.0000);
                    }
                } else {
                    sumFormattedTextField.setValue(0.0000);
                }
                String someString = "";
                someString = ("Contents: " + source.getText(0, length));
            } catch (BadLocationException badLocationException) {
                Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, badLocationException);
            }
        }
    };

    private void nullValue() {
        firstFormattedTextField.setValue(firstValue);
        secondFormattedTextField.setValue(secondValue);
        thirdFormattedTextField.setValue(thirdValue);
        fourthFormattedTextField.setValue(fourthValue);
    }

    private void setUpFormats() {
        firstNumberFormat = NumberFormat.getNumberInstance();
        firstNumberFormat.setMinimumFractionDigits(2);
        firstNumberFormat.setMaximumFractionDigits(2);
        firstNumberFormat.setRoundingMode(RoundingMode.HALF_UP);
        secondNumberFormat = NumberFormat.getNumberInstance();
        secondNumberFormat.setMinimumFractionDigits(2);
        secondNumberFormat.setMaximumFractionDigits(3);
        secondNumberFormat.setRoundingMode(RoundingMode.HALF_UP);
        thirdNumberFormat = NumberFormat.getNumberInstance();
        thirdNumberFormat.setMinimumFractionDigits(2);
        thirdNumberFormat.setMaximumFractionDigits(5);
        thirdNumberFormat.setRoundingMode(RoundingMode.HALF_UP);
        fourthNumberFormat = NumberFormat.getNumberInstance();
        fourthNumberFormat.setMinimumFractionDigits(2);
        fourthNumberFormat.setMaximumFractionDigits(2);
        fourthNumberFormat.setRoundingMode(RoundingMode.HALF_UP);
        sumNumberFormat = NumberFormat.getNumberInstance();
        sumNumberFormat.setMinimumFractionDigits(2);
        sumNumberFormat.setMaximumFractionDigits(4);
        sumNumberFormat.setRoundingMode(RoundingMode.HALF_UP);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                NewClass nC = new NewClass();
            }
        });
    }
}

don't f*** this code, 'cos I'll found you :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.