In order to make the program work I need to have a drop down menu (combo box) that shows all of the possible units to convert to for Stoichiometry.
I know that you must assign an array to a combo box, but I am not sure how. If somebody could help it would be great.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Conversion extends JFrame
{
private JLabel molarMass1;
private JLabel molarMass2;
private JLabel moles1;
private JLabel moles2;
private JLabel amount1;
private JLabel amount2;
private JTextField M1M2;
private JTextField M2M1;
private JTextField moles1moles2;
private JTextField moles2moles1;
private JTextField amount1amount2;
private JTextField amount2amount1;
private JComboBox UnitsOne;
private JComboBox UnitsTwo;
private static final int WIDTH = 500;
private static final int HEIGHT = 50;
Conversion()
{}
public void burst()
{
setTitle("Stoichiometry");
Container z = getContentPane();
z.setLayout(new GridLayout(1, 4));
molarMass1 = new JLabel("Molar Mass 1: ", SwingConstants.LEFT);
molarMass2 = new JLabel("Molar Mass 2: ", SwingConstants.RIGHT);
moles1 = new JLabel("Number of Moles 1: ", SwingConstants.LEFT);
moles2 = new JLabel("Number of Moles 2: ", SwingConstants.RIGHT);
amount1 = new JLabel("Amount of substance 1: ", SwingConstants.LEFT);
amount2 = new JLabel("Amount of substance 2: ", SwingConstants.RIGHT);
M1M2 = new JTextField(7);
M2M1 = new JTextField(7);
moles1moles2 = new JTextField(6);
moles2moles1 = new JTextField(6);
amount1amount2 = new JTextField(5);
amount2amount1 = new JTextField(5);
UnitsOne = new JComboBox(10);
UnitsTwo = new JComboBox(10);
z.add(molarMass1);
z.add(M1M2);
z.add(molarMass2);
z.add(M2M1);
z.add(moles1);
z.add(moles1moles2);
z.add(moles2);
z.add(moles2moles1);
z.add(amount1);
z.add(amount1amount2);
z.add(amount2);
z.add(amount2amount1);
z.add(UnitsOne);
z.add(UnitsTwo);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
I don't see where the array you want to assign to the combo box is in your code, but there is a constructor which do exactly what you need: http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html#JComboBox(E[])
Alternatively you can add items to the JComboBox later using the addItem() method
Also have a look here: https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
Related
I've been trying to make this Tax Return Program to work but im having some difficulties. It runs sure, but the "Calculation Button" does not work.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Tax extends JFrame
{
private int NewNetPay = 0;
private JPanel panelN;
private JPanel panelS;
private JPanel panelE;
private JPanel panelW;
private JPanel panelC;
private JButton calcButton;
private JButton resetButton;
private JButton exitButton;
private JRadioButton hohRB;
private JRadioButton marriedJRB;
private JRadioButton marriedSRB;
private JRadioButton singleRB;
private JCheckBox mortgageCB;
private JCheckBox charitableCB;
private JCheckBox childCB;
private JCheckBox educationCB;
private JTextField childTF;
private JTextField mortgageTF;
private JTextField charitableTF;
private JTextField educationTF;
private JLabel firstName;
private JTextField name1TF;
private JLabel lastName;
private JTextField name2TF;
private JLabel gross;
private JTextField grossTF;
private JLabel netPay;
private JTextField netPayTF;
final int WIN_WIDTH = 500;
final int WIN_HEIGHT = 300;
public static void main (String[] args)
{
new Tax();
}
public Tax()
{
setTitle("CSC142 Tax Calculator");
setSize(WIN_WIDTH,WIN_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(20, 20));
buildPanel();
//add(panel);
setVisible(true);
}
private void buildPanel()
{
JFrame frame = new JFrame();
hohRB = new JRadioButton("Head of Household");
marriedJRB = new JRadioButton("Married, Jointly");
marriedSRB = new JRadioButton("Married, Seperately");
singleRB = new JRadioButton("Single");
ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(hohRB);
radioGroup.add(marriedJRB);
radioGroup.add(marriedSRB);
radioGroup.add(singleRB);
mortgageCB = new JCheckBox("Mortgage");
charitableCB = new JCheckBox("Charitable Donation");
childCB = new JCheckBox("Child Deduction");
educationCB = new JCheckBox("Education Expenses");
ButtonGroup checkGroup = new ButtonGroup();
checkGroup.add(mortgageCB);
checkGroup.add(charitableCB);
checkGroup.add(childCB);
checkGroup.add(educationCB);
calcButton = new JButton(" Calculate Taxes ");
resetButton = new JButton(" Reset Values ");
exitButton = new JButton (" Exit Application ");
panelN = new JPanel();
panelS = new JPanel();
panelE = new JPanel();
panelW = new JPanel();
panelC = new JPanel();
add(panelN, BorderLayout.NORTH);
add(panelS, BorderLayout.SOUTH);
add(panelE, BorderLayout.EAST);
add(panelW, BorderLayout.WEST);
add(panelC, BorderLayout.CENTER);
panelN.setLayout(new BoxLayout(panelN, BoxLayout.X_AXIS));
panelS.setLayout(new BoxLayout(panelS, BoxLayout.X_AXIS));
panelE.setLayout(new BoxLayout(panelE, BoxLayout.Y_AXIS));
panelW.setLayout(new BoxLayout(panelW, BoxLayout.Y_AXIS));
panelC.setLayout(new BoxLayout(panelC, BoxLayout.Y_AXIS));
panelW.add(hohRB);
panelW.add(marriedJRB);
panelW.add(marriedSRB);
panelW.add(singleRB);
JTextField childTF = new JTextField();
childTF.setPreferredSize( new Dimension (10,10));
JTextField mortgageTF = new JTextField();
mortgageTF.setPreferredSize( new Dimension (10,10));
JTextField charitableTF = new JTextField();
charitableTF.setPreferredSize( new Dimension (10,10));
JTextField educationTF = new JTextField();
educationTF.setPreferredSize( new Dimension (10,10));
childTF.setEnabled(false);
mortgageTF.setEnabled(false);
charitableTF.setEnabled(false);
educationTF.setEnabled(false);
panelC.add(mortgageCB);
panelC.add(mortgageTF);
panelC.add(charitableCB);
panelC.add(charitableTF);
panelC.add(childCB);
panelC.add(childTF);
panelC.add(educationCB);
panelC.add(educationTF);
panelS.add(Box.createRigidArea(new Dimension(35,0)));
panelS.add(calcButton);
panelS.add(Box.createRigidArea(new Dimension(20,0)));
panelS.add(resetButton);
panelS.add(Box.createRigidArea(new Dimension(20,0)));
panelS.add(exitButton);
panelW.setBorder(BorderFactory.createTitledBorder("Filing Status"));
panelC.setBorder(BorderFactory.createTitledBorder("Deductions"));
JTextField name1TF = new JTextField();
JTextField name2TF = new JTextField();
JTextField grossTF = new JTextField();
JLabel firstName = new JLabel(" First Name ");
JLabel lastName = new JLabel(" Last Name ");
JLabel gross = new JLabel(" Gross Income ");
panelN.add(firstName);
panelN.add(name1TF);
panelN.add(lastName);
panelN.add(name2TF);
panelN.add(gross);
panelN.add(grossTF);
JLabel netPay = new JLabel("Net Pay");
JTextField netPayTF = new JTextField(1);
panelE.add(netPay);
panelE.add(netPayTF);
calcButton.addActionListener(new CalcButtonListener());
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String inputo;
inputo = grossTF.getText();
if (mortgageCB.isSelected())
NewNetPay = Integer.parseInt(inputo) - 10000;
else if (charitableCB.isSelected())
NewNetPay = Integer.parseInt(inputo) - 5000 ;
else if (childCB.isSelected())
NewNetPay = Integer.parseInt(inputo) - 1000;
else if (educationCB.isSelected())
NewNetPay = Integer.parseInt(inputo) - 20000;
netPayTF.setText(" " + NewNetPay);
}
}
} `enter code here`
I am trying to make it so that a user enters a salary in the gross income text field, then depending on what checkbox they click they will get a deduction. Then finally, the amount will be displayed in the net pay text field.
You are adding JTextField grossTF = new JTextField(); where grossTF is a local Variable to the frame, and trying to get data from inputo = grossTF.getText(); where this.grossTF which is a class member that would be initialized null when getting the data, hence you'd get a NPE.
Also you've done the same for netPayTF, change them to..
netPayTF = new JTextField(1); & grossTF = new JTextField();
im making a currency calculator and i have an issue with not being able to set the result of the calculation into the result JTextField, which is the object otherTextField.
Here is my class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ValutaKalkulator implements ActionListener {
private double nokValue;
private double otherValue;
private JButton buttonRemoveNok;
private JButton removeOther;
private JButton removeBoth;
private JButton exitButton;
private JButton usdButton;
private JButton sekButton;
private JButton gbpButton;
private JButton eurButton;
private JLabel nokLabel;
private JTextField nokTextField;
private JLabel otherLabel;
private JTextField otherTextField;
public ValutaKalkulator()
{
JFrame frame = new JFrame();
frame.setTitle("VALUTAKALKULATOR");
buttonRemoveNok = new JButton("Fjern NOK");
removeOther = new JButton("Fjern annen valuta");
removeBoth = new JButton("Fjern begge");
exitButton = new JButton("Avslutt");
usdButton = new JButton("USD");
sekButton = new JButton("SEK");
gbpButton = new JButton("GBP");
eurButton = new JButton("EUR");
nokLabel = new JLabel("NOK");
JTextField nokTextField = new JTextField();
otherLabel = new JLabel("Annen valuta");
otherTextField = new JTextField();
buttonRemoveNok.addActionListener(this);
removeOther.addActionListener(this);
removeBoth.addActionListener(this);
exitButton.addActionListener(this);
usdButton.addActionListener(this);
sekButton.addActionListener(this);
gbpButton.addActionListener(this);
eurButton.addActionListener(this);
JPanel pnlSouth = new JPanel(new GridLayout(1, 4));
JPanel pnlCenter = new JPanel(new GridLayout(2, 2));
JPanel pnlNorth = new JPanel(new GridLayout(1, 4));
pnlNorth.add(nokLabel);
pnlNorth.add(nokTextField);
pnlNorth.add(otherLabel);
pnlNorth.add(otherTextField);
pnlCenter.add(gbpButton);
pnlCenter.add(eurButton);
pnlCenter.add(usdButton);
pnlCenter.add(sekButton);
pnlSouth.add(buttonRemoveNok);
pnlSouth.add(removeOther);
pnlSouth.add(removeBoth);
pnlSouth.add(exitButton);
frame.add(pnlNorth, BorderLayout.NORTH);
frame.add(pnlSouth, BorderLayout.SOUTH);
frame.add(pnlCenter, BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
}
public String calculateFromNok(String nokValueString, String toValue)
{
double result = 0;
double nokValue = Double.valueOf(nokValueString);
switch(toValue)
{
case "GBP":
result = nokValue * 10.8980 / 100;
break;
case "EUR":
result = nokValue * 9.2450 / 100;
break;
case "USD":
result = nokValue * 8.5223 / 100;
break;
case "SEK":
result = nokValue * 96.48 / 100;
break;
}
String resultString = Double.toString(result);
return resultString;
}
public void actionPerformed(ActionEvent event)
{
String text = event.getActionCommand();
switch(text)
{
case "USD":
String txt = nokTextField.getText();
String txt2 = calculateFromNok(txt, "USD");
otherTextField.setText(txt2);
break;
}
}
}
I know the currencies are not correct and there are other logical flaws and bad variable names, but my question for now is why can i not put the result from the method calculateFromNok into my JTextField otherTextField?
Help appreciated thx!
You are creating an initialize local variable JTextField nokTextField.
So private JTextField nokTextField; is not initializing. That's why it's giving you nullpointerexception.
Just Change At Line Number 37:
JTextField nokTextField = new JTextField();
To
nokTextField = new JTextField();
Your issue is with the line:
JTextField nokTextField = new JTextField();
Change it to:
nokTextField = new JTextField();
The reason this happens is that you previously defined nokTextField at the top, and then you initialized a separate variable with the same name in the constructor. Using an IDE such as Eclipse makes these types of errors very easy to catch. I suggest you use one.
I have been trying to run this code which takes the user's assessed home value, the county and school taxes and then outputs them to a text field. This code compiles and runs but there is a logical error somewhere that doesn't let it output any text. I'm just starting with Java so any help would be appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PropertyTax3 extends JFrame
{
// set parameters to define extent of the window
private static final int WIDTH = 500, HEIGHT = 300;
private ExitHandler ebHandler;
private CalculateHandler cbHandler;
private JTextField assessTF, schoolRateTF, countyRateTF, schoolTaxTF, countyTaxTF, totalTaxTF;
public PropertyTax3()
{
// set title, size and visibility aspects of window
setTitle("Calculation of Property Taxes");
//Label Definitions
JLabel assessL = new JLabel("Assesment Home Value:", SwingConstants.RIGHT);
JLabel schoolRateL = new JLabel("Decimal Value of School Tax Rate:", SwingConstants.RIGHT);
JLabel countyRateL = new JLabel("Decimal Value of County Tax Rate:", SwingConstants.RIGHT);
JLabel schoolTaxL = new JLabel("School Taxes:", SwingConstants.RIGHT);
JLabel countyTaxL = new JLabel("County Taxes:", SwingConstants.RIGHT);
JLabel totalTaxL = new JLabel("Total Taxes:", SwingConstants.RIGHT);
//Text Field Definitions
JTextField assessTF = new JTextField(10);
JTextField schoolRateTF = new JTextField(10);
JTextField countyRateTF = new JTextField(10);
JTextField schoolTaxTF = new JTextField(10);
JTextField countyTaxTF = new JTextField(10);
JTextField totalTaxTF = new JTextField(10);
//Exit Button
JButton exit = new JButton("Exit");
ebHandler = new ExitHandler();
exit.addActionListener(ebHandler);
//Calculate Button
JButton calculate = new JButton("Calculate");
cbHandler = new CalculateHandler();
calculate.addActionListener(cbHandler);
//Container format
Container pane = getContentPane();
pane.setLayout(new GridLayout(7,2));
pane.add(assessL);
pane.add(assessTF);
pane.add(schoolRateL);
pane.add(schoolRateTF);
pane.add(countyRateL);
pane.add(countyRateTF);
pane.add(schoolTaxL);
pane.add(schoolTaxTF);
pane.add(countyTaxL);
pane.add(countyTaxTF);
pane.add(totalTaxL);
pane.add(totalTaxTF);
pane.add(exit);
pane.add(calculate);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class ExitHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
private class CalculateHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double countyRate, schoolRate, assessment, schoolTax, countyTax, totalTax;
assessment = Double.parseDouble(assessTF.getText());
schoolRate = Double.parseDouble(schoolRateTF.getText());
countyRate = Double.parseDouble(countyRateTF.getText());
schoolTax = assessment * schoolRate * .01;
countyTax = assessment * countyRate * .01;
totalTax = schoolTax + countyTax;
schoolTaxTF.setText(""+ String.format("%.2f", schoolTax));
countyTaxTF.setText(""+ String.format("%.2f", countyTax));
totalTaxTF.setText(""+ String.format("%.2f", totalTax));
}
}
public static void main(String[] args)
{
// main program to invoke constructor
PropertyTax3 proptax = new PropertyTax3();
}
}
Change this
JTextField assessTF = new JTextField(10);
JTextField schoolRateTF = new JTextField(10);
JTextField countyRateTF = new JTextField(10);
JTextField schoolTaxTF = new JTextField(10);
JTextField countyTaxTF = new JTextField(10);
JTextField totalTaxTF = new JTextField(10);
to this
assessTF = new JTextField(10);
schoolRateTF = new JTextField(10);
countyRateTF = new JTextField(10);
schoolTaxTF = new JTextField(10);
countyTaxTF = new JTextField(10);
totalTaxTF = new JTextField(10);
That way, you aren't using the variables you defined as a class field, but creating new ones. And that's why you couldn't access them later with getText and setText.
That's because there are no values for the program to run with...
I've only been working with Java for about a year now and have just gotten into the basics of GUI. I'm writing a program that will calculate the center of mass between 2, 3, 4, or 5 points (user's option). Depending on the number of points the user wants to enter, a number of editable JTextFields appear to get input for coordinates and masses of those coordinates. Unfortunately, when asked to calculate the center of mass, the button won't display what I want. The way it's written now is the only way I've gotten it to compile/run without an empty string error. I believe it has to do with how I've initialized the variables/where they're initialized between constructors, but I can't for the life of me figure out where that problem lies exactly. Any help will be greatly appreciated! I've attached the code - I know it's long, but you never know what can be useful. Thanks!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class ComboBox extends JFrame
/*************VARIABLES*******************************************************/
{
private final int WIDTH = 1500;
private final int HEIGHT = 1500;
private JPanel northPanel;
private JPanel centerPanel;
private JPanel blankPanel;
private JPanel pointsPanel;
private JPanel selectedPointsPanel;
private JComboBox pointsBox;
private JTextField selectedPoints;
private JLabel selection;
private String choose = "Choose an option...";
private String twoPoints = "2 points";
private String threePoints = "3 points";
private String fourPoints = "4 points";
private String fivePoints = "5 points";
private String[] points = {choose, twoPoints, threePoints, fourPoints, fivePoints};
private JPanel coordinatesPanel;
private JPanel cPanel;
private JTextField xField1;
private JTextField xField2;
private JTextField xField3;
private JTextField xField4;
private JTextField xField5;
private JTextField yField1;
private JTextField yField2;
private JTextField yField3;
private JTextField yField4;
private JTextField yField5;
private JLabel instructions;
private JLabel X;
private JLabel Y;
private JLabel blankLabel;
private JPanel massPanel;
private JTextField mass1 = new JTextField(10);
private JTextField mass2 = new JTextField(10);
private JTextField mass3 = new JTextField(10);
private JTextField mass4 = new JTextField(10);
private JTextField mass5 = new JTextField(10);
private JLabel instructions2;
Boolean calcBool = false;
private JButton calcButton = new JButton("Calculate");
private JButton resetButton = new JButton("Reset");
private JPanel displayPanel;
private double centerX;
private double centerY;
private JLabel display;
private double x1, x2, x3, x4, x5;
private double y1, y2, y3, y4, y5;
private double m1, m2, m3, m4, m5;
/**********************************WINDOW************************************/
public ComboBox()
{
super("Choose an option");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3,2));
setLocationRelativeTo(null);
setResizable(true);
setSize(WIDTH, HEIGHT);
buildPointsPanel();
buildCoordinatesPanel();
buildMassPanel();
buildDisplayPanel();
buildBlankPanel();
add(pointsPanel);
add(coordinatesPanel);
add(massPanel);
add(blankPanel);
add(calcButton);
add(blankPanel);
add(resetButton);
add(blankPanel);
add(displayPanel);
calcButton.addActionListener(new CalcButtonListener());
pointsBox.addActionListener(new ComboBoxListener());
//resetButton.addActionListener(new ResetButtonListener());
pack();
setVisible(true);
}
/************************BUILD ALL THE PANELS**********************/
private void buildBlankPanel()
{
blankPanel = new JPanel();
}
private void buildPointsPanel()
{
pointsPanel = new JPanel();
pointsPanel.setLayout(new GridLayout(3,1));
pointsBox = new JComboBox(points);
pointsBox.addActionListener(new ComboBoxListener());
pointsPanel.add(pointsBox);
selection = new JLabel("You selected: ");
selectedPoints = new JTextField(10);
selectedPoints.setEditable(false);
pointsPanel.add(selection);
pointsPanel.add(selectedPoints);
}
private void buildCoordinatesPanel()
{
coordinatesPanel = new JPanel();
coordinatesPanel.setLayout(new GridLayout(6,2));
instructions = new JLabel("Please enter the X and Y values of your points below.");
JLabel blank = new JLabel("");
X = new JLabel("X values");
Y = new JLabel("Y values");
blankLabel = new JLabel("");
coordinatesPanel.add(instructions);
coordinatesPanel.add(blankLabel);
coordinatesPanel.add(X);
coordinatesPanel.add(Y);
xField1 = new JTextField(10);
xField1.setEditable(true);
yField1 = new JTextField(10);
yField1.setEditable(true);
xField2 = new JTextField(10);
xField2.setEditable(true);
yField2 = new JTextField(10);
yField2.setEditable(true);
xField3 = new JTextField(10);
xField3.setEditable(true);
yField3 = new JTextField(10);
yField3.setEditable(true);
xField4 = new JTextField(10);
xField4.setEditable(true);
yField4 = new JTextField(10);
yField4.setEditable(true);
xField5 = new JTextField(10);
xField5.setEditable(true);
yField5 = new JTextField(10);
yField5.setEditable(true);
}
private void buildMassPanel()
{
massPanel = new JPanel();
instructions2 = new JLabel("Please enter the masses of your points");
massPanel.add(instructions2);
mass1.setEditable(true);
mass2.setEditable(true);
mass3.setEditable(true);
mass4.setEditable(true);
mass5.setEditable(true);
}
private void buildDisplayPanel()
{
displayPanel = new JPanel();
//display = new JLabel("The center of mass is located at (" + centerX + "," + centerY
+").");
//displayPanel.add(display);
}
/********************************COMBOBOX LISTENER****************************/
private class ComboBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{ //The following asks the user to select the number of points they want and stores
it
String select =(String) pointsBox.getSelectedItem();
selectedPoints.setText(select);
//The following determines how many text fields to display depending on how many
points the user wants
if (select==twoPoints)
{
coordinatesPanel.add(xField1);
coordinatesPanel.add(yField1);
coordinatesPanel.add(xField2);
coordinatesPanel.add(yField2);
massPanel.add(mass1);
massPanel.add(mass2);
centerX = ((m1*x1)+(m2*x2)/(m1+m2));
centerY = ((m1*y1)+(m2*y2)/(m1+m2));
}
if (select==threePoints)
{
coordinatesPanel.add(xField1);
coordinatesPanel.add(yField1);
coordinatesPanel.add(xField2);
coordinatesPanel.add(yField2);
coordinatesPanel.add(xField3);
coordinatesPanel.add(yField3);
massPanel.add(mass1);
massPanel.add(mass2);
massPanel.add(mass3);
centerX = ((m1*x1)+(m2*x2)+(m3*x3)/(m1+m2+m3));
centerY = ((m1*y1)+(m2*y2)+(m3*y3)/(m1+m2+m3));
}
if (select==fourPoints)
{
coordinatesPanel.add(xField1);
coordinatesPanel.add(yField1);
coordinatesPanel.add(xField2);
coordinatesPanel.add(yField2);
coordinatesPanel.add(xField3);
coordinatesPanel.add(yField3);
coordinatesPanel.add(xField4);
coordinatesPanel.add(yField4);
massPanel.add(mass1);
massPanel.add(mass2);
massPanel.add(mass3);
massPanel.add(mass4);
centerX = ((m1*x1)+(m2*x2)+(m3*x3)+(m4*x4)/(m1+m2+m3+m4));
centerY = ((m1*y1)+(m2*y2)+(m3*y3)+(m4*y4)/(m1+m2+m3+m4));
}
if (select==fivePoints)
{
coordinatesPanel.add(xField1);
coordinatesPanel.add(yField1);
coordinatesPanel.add(xField2);
coordinatesPanel.add(yField2);
coordinatesPanel.add(xField3);
coordinatesPanel.add(yField3);
coordinatesPanel.add(xField4);
coordinatesPanel.add(yField4);
coordinatesPanel.add(xField5);
coordinatesPanel.add(yField5);
massPanel.add(mass1);
massPanel.add(mass2);
massPanel.add(mass3);
massPanel.add(mass4);
massPanel.add(mass5);
centerX = ((m1*x1)+(m2*x2)+(m3*x3)+(m4*x4)+(m5*x5)/(m1+m2+m3+m4+m5));
centerY = ((m1*y1)+(m2*y2)+(m3*y3)+(m4*y4)+(m5*y5)/(m1+m2+m3+m4+m5));
}
if (select==choose)
{
JOptionPane.showMessageDialog(null, "Please select a valid option");
}
}
}
/********************************CALC BUTTON LISTENER******************************/
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
display = new JLabel("The center of mass is located at (" + centerX + "," + centerY
+").");
displayPanel.add(display);
}
}
/******************************MAIN METHOD***************************/
public static void main(String[] args)
{
new ComboBox();
}
What you should do is instantiate the display, where you have it declared.
JLabel display = new JLabel(" ");
Then add it to the GUI, wherever in the code you're adding all the other components. Than in your listener class, just set the text
public void actionPerformed(ActionEvent e){
display.setText("some text");
}
The way you're doing it might mess up your desired GUI formatting. If you were to do it though, you need to revalidate() and repaint() after adding any new components. I'd recommend using the former.
Don't know where is your specific problem, but looking at your code i see some potential bugs. In java you compare objects equality with equals() not with == .== is just for references.
So changes all lines where you compare Strings with == replace with equals.
For example change
select==fivePoints
to
select.equals(fivePoints)
Having attributes like
private JTextField mass1 = new JTextField(10);
private JTextField mass2 = new JTextField(10);
private JTextField mass3 = new JTextField(10);
private JTextField mass4 = new JTextField(10);
private JTextField mass5 = new JTextField(10);
is not the best way, you can store them in a Collection or in array.
private List<JTextField> masses;
Always when you add a component call revalidate and repaint.
display = new JLabel("The center of mass is located at (" + centerX + "," + centerY
+").");
displayPanel.add(display);
displayPanel.revalidate();
displayPanel.repaint();
I am a newbee so advise and help is always greatly appreciated.
Cannot seem to get my container contentPane to display the title.
My code:
class CreateStockCodeDetails extends JFrame implements ActionListener
{
OptraderSA parent;
OptraderGlobalParameters GV = new OptraderGlobalParameters();
private boolean DEBUG = true; //Set DEBUG = true for Debugging
JButton SAVE_BUTTON = new JButton("SAVE");
JButton CANCEL_BUTTON = new JButton("CANCEL");
Font MyFont = new Font("Helvetica",Font.BOLD,24);
JLabel PriceBidLabel = new JLabel(" Bid Price",JLabel.LEFT);
JLabel PriceAskLabel = new JLabel(" Ask Price",JLabel.LEFT);
JLabel PriceMidLabel = new JLabel(" Mid Price",JLabel.LEFT);
JLabel DividendLabel = new JLabel(" Dividend",JLabel.LEFT);
JTextField PriceBid = new JTextField(5);
JTextField PriceAsk = new JTextField(5);
JTextField PriceMid = new JTextField(5);
JTextField Dividend = new JTextField(5);
JTextField NewUnderlyingCode = new JTextField(10);
String NewCode;
public void CreateStockDetails(String StockCode)
{
**super("Hallo All");**
Container contentPane = getContentPane();
setSize(400,500);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Centre Screen To Right Of Main
Dimension sd=Toolkit.getDefaultToolkit().getScreenSize();
super.setLocation(sd.width/2-100/2, sd.height/2-300/2);
Thanks
Kind Regards Stephen
First, note that you can only call super("Hallo All") in your constructor.
As I see it you have two problems:
A constructor has no return type. Thus remove void
The constructor must have the same name as the class.
That is, change from
public void CreateStockDetails(String StockCode)
{
**super("Hallo All");**
to this
public CreateStockCodeDetails(String StockCode)
{
super("Hallo All");
Also, as a side-note: According to Java conventions, you should use lower case initial letters for variable identifiers, that is, write stockCode instead of StockCode and you should not have a new-line before opening braces.