So the mission here is to have all the user input data to display to the display panel. I was able to achieve this, however instead of the info display after I click the display button the info will display after clicking on any of the radio button options. Im hoping someone out there can point me in the right direction so I can get this program to display correctly. There are two classes used in this application.
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;
import javax.swing.*;
// this class creates an entry screen for a new hourly worker
public class GBPGUI extends JFrame {
// declare GUI components
private JPanel headerPanel;
private JPanel bigPanel;
private JPanel buttonPanel;
private JPanel radioPanel;
private JPanel radioPanel2;
private JPanel sliderPanel;
private JPanel displayButtonPanel;
private JPanel clearButtonPanel;
private JPanel displayPanel;
private JLabel jlblHeader;
private JLabel jlblAddress;
private JTextField jtfAddress;
private JRadioButton jrbNumBedrooms1;
private JRadioButton jrbNumBedrooms2;
private JRadioButton jrbNumBedrooms3;
private JRadioButton jrbNumBathrooms1;
private JRadioButton jrbNumBathrooms2;
private JRadioButton jrbNumBathrooms3;
private ButtonGroup bg;
private ButtonGroup bg1;
private JButton jbtDisplay;
private JButton jbtClear;
private JSlider jsldSqFeet;
private JTextArea jtaDisplay;
private Color green = new Color(127,255,0);
private Color black = new Color(0,0,0);
// main method
public static void main(String[] args) {
GBPGUI frame = new GBPGUI();
}
// GUI constructor
public GBPGUI() {
// customize appearance
setSize(500, 700);
setTitle("Enter new Hourly Workers");
setLocation(400,400);
// call build panel methods and add panels to frame
buildHeaderPanel();
add(headerPanel, BorderLayout.NORTH);
buildBigPanel();
add(bigPanel, BorderLayout.CENTER);
buildButtonPanel();
add(buttonPanel, BorderLayout.EAST);
buildDisplayPanel();
add(displayPanel, BorderLayout.SOUTH);
// customize behavior
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
} // end constructor
// build header panel
private void buildHeaderPanel() {
headerPanel = new JPanel();
headerPanel.setBackground(green);
jlblHeader = new JLabel( "Gallifrey Beach Properties" );
jlblHeader.setFont( new Font( "SansSerif", Font.ITALIC + Font.BOLD, 24) );
jlblHeader.setIcon( new ImageIcon( "skates.gif" ) );
headerPanel.add(jlblHeader);
}
// build entry panel
private void buildBigPanel(){
// instantiate panel and components
bigPanel = new JPanel();
bigPanel.setLayout( new GridLayout(4,1));
bigPanel.setBackground( black );
jlblAddress = new JLabel("Address: ");
jlblAddress.setFont( new Font( "SansSerif", Font.ITALIC + Font.BOLD, 14) );
jlblAddress.setForeground(green);
jtfAddress = new JTextField(6);
jtfAddress.setBorder(BorderFactory.createLineBorder(green));
bigPanel.add(jlblAddress);
bigPanel.add(jtfAddress);
radioPanel2 = new JPanel();
radioPanel2.setBackground(black);
radioPanel2.setLayout( new GridLayout(3,1));
radioPanel2.setBorder( BorderFactory.createTitledBorder( "Number of bedrooms" ));
jrbNumBedrooms1 = new JRadioButton( "1", false );
jrbNumBedrooms2 = new JRadioButton( "2", false );
jrbNumBedrooms3 = new JRadioButton( "3", false );
// add buttons to a logical group so only one may be selected at a time
bg = new ButtonGroup();
bg.add( jrbNumBedrooms1 );
bg.add( jrbNumBedrooms2 );
bg.add( jrbNumBedrooms3 );
// add radio buttons to panel
radioPanel2.add( jrbNumBedrooms1 );
radioPanel2.add( jrbNumBedrooms2 );
radioPanel2.add( jrbNumBedrooms3 );
jrbNumBedrooms1.addActionListener(new DisplayButtonListener());
jrbNumBedrooms2.addActionListener(new DisplayButtonListener());
jrbNumBedrooms3.addActionListener(new DisplayButtonListener());
bigPanel.add( radioPanel2 );
radioPanel = new JPanel();
radioPanel.setBackground(black);
radioPanel.setLayout( new GridLayout(3,1));
radioPanel.setBorder( BorderFactory.createTitledBorder( "Number of bathrooms" ));
jrbNumBathrooms1 = new JRadioButton( "1", false );
jrbNumBathrooms2 = new JRadioButton( "2", false );
jrbNumBathrooms3 = new JRadioButton( "3", false );
// add buttons to a logical group so only one may be selected at a time
bg1 = new ButtonGroup();
bg1.add( jrbNumBathrooms1 );
bg1.add( jrbNumBathrooms2 );
bg1.add( jrbNumBathrooms3 );
// add radio buttons to panel
radioPanel.add( jrbNumBathrooms1 );
radioPanel.add( jrbNumBathrooms2 );
radioPanel.add( jrbNumBathrooms3 );
jrbNumBathrooms1.addActionListener(new DisplayButtonListener());
jrbNumBathrooms2.addActionListener(new DisplayButtonListener());
jrbNumBathrooms3.addActionListener(new DisplayButtonListener());
bigPanel.add( radioPanel );
sliderPanel = new JPanel();
sliderPanel.setBorder( BorderFactory.createTitledBorder( "How many square feet" ));
jsldSqFeet = new JSlider( 600, 2800, 1200);
jsldSqFeet.setMajorTickSpacing( 1000 );
jsldSqFeet.setMinorTickSpacing( 100 );
jsldSqFeet.setPaintTicks( true );
jsldSqFeet.setPaintLabels( true );
sliderPanel.add( jsldSqFeet );
bigPanel.add( sliderPanel );
} // end buildEntryPanel method
// build button panel
private void buildButtonPanel() {
buttonPanel = new JPanel();
buttonPanel.setBackground(black);
buttonPanel.setLayout( new GridLayout(2,1));
displayButtonPanel = new JPanel();
displayButtonPanel.setBackground(black);
jbtDisplay = new JButton("Display");
jbtDisplay.setBackground(green);
jbtDisplay.addActionListener(new DisplayButtonListener());
displayButtonPanel.add(jbtDisplay);
clearButtonPanel = new JPanel();
clearButtonPanel.setBackground( black);
jbtClear = new JButton(" Clear ");
jbtClear.setBackground(green);
jbtClear.addActionListener(new ClearButtonListener());
clearButtonPanel.add(jbtClear);
buttonPanel.add(displayButtonPanel);
buttonPanel.add(clearButtonPanel);
} // end buildButtonPanel method
// build display panel
private void buildDisplayPanel() {
displayPanel = new JPanel();
displayPanel.setBorder(BorderFactory.createLineBorder(black));
displayPanel.setBackground(black);
jtaDisplay = new JTextArea( 8, 40);
jtaDisplay.setBackground(green);
jtaDisplay.setLineWrap(true);
jtaDisplay.setBorder(BorderFactory.createTitledBorder("Cottage Data"));
jtaDisplay.setEditable(false);
displayPanel.add(jtaDisplay);
} // end buildDisplayPanel method
private class DisplayButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
// retrieve user entered data from GUI
String Address = jtfAddress.getText();
String numBedrooms = "0";
if( jrbNumBedrooms1.isSelected() ) {
numBedrooms = "1";
}else if(jrbNumBedrooms2.isSelected() ) {
numBedrooms = "2";
} else if(jrbNumBedrooms3.isSelected() ){
numBedrooms = "3";
}
String output = "Number of bedrooms: " + numBedrooms + "\n";
String numBathrooms = "0";
if( jrbNumBathrooms1.isSelected() ) {
numBathrooms = "1";
}else if(jrbNumBathrooms2.isSelected() ) {
numBathrooms = "2";
} else if(jrbNumBathrooms3.isSelected() ){
numBathrooms = "3";
}
output += "The number of bathrooms: " + numBathrooms + "\n";
int slideNumber = jsldSqFeet.getValue();
output += "Square feet: " + slideNumber;
jtaDisplay.setText("The address is " + Address + "\n" + output);
//bg.getSelection();
//bg1.getSelection();
jsldSqFeet.setToolTipText(new Integer(jsldSqFeet.getValue()).toString());
String sqFeet = jsldSqFeet.getToolTipText();
Cottage c = new Cottage(Address, numBedrooms, numBathrooms, sqFeet, 5000);
jtaDisplay.setText(c.toString());
}
} // end DisplayButtonListener class
private class ClearButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
jtfAddress.setText("");
jtaDisplay.setText("");
bg.clearSelection();
bg1.clearSelection();
}
} // end ClearButtonListener class
}
the code in which the GUI calss upon is here.
public class Cottage {
// ***** Instance variables *****
private String address;
private String numBedrooms;
private String numBathrooms;
private String sqFeet;
private int askingPrice;
//private Double rent;
public Cottage() {
//call super class
super();
}
public Cottage(String address, String numBedrooms, String numBathrooms,
String sqFeet, int askingPrice) {
super();
//assign subclass variables
this.address = address;
this.numBedrooms = numBedrooms;
this.numBathrooms = numBathrooms;
this.sqFeet = sqFeet;
this.askingPrice = askingPrice;
// this.rent = rent;
}
//getters
public String getAddress() {
return address;
}
public String getNumBathrooms() {
return numBathrooms;
}
public String getNumBedrooms() {
return numBedrooms;
}
public String getSqFeet() {
return sqFeet;
}
public int getAskingPrice() {
return askingPrice;
}
//public Double getRent() {
// return rent;
//}
//setters
public void setAddress(String address) {
this.address = address;
}
public void setNumBathrooms(String numBedrooms) {
this.numBedrooms = numBedrooms;
}
public void setSqFeet(String sqFeet) {
this.sqFeet = sqFeet;
}
public void setAskingPrice(int askingPrice) {
this.askingPrice = askingPrice;
}
//public void setRent(Double rent) {
// this.rent = rent;
//}
//toString method
public String toString() {
return " Cottage address= " + address + "\n" + " Number of bedrooms= " + numBedrooms
+ "\n" + " Number of bathrooms= " + numBathrooms + "\n" + " Square feet= " + sqFeet
+ "\n" + " Asking price= " + askingPrice + "\n" + " rent per day= " + computeRent() + "";
}
//Computes rent.
public double computeRent() {
return askingPrice * .02;
}
}
Thanks in advance.