i trying to make a program that gives a list of tv manufacturers and their price range. I have another box that says in stock and out of stock and gives the stores to get it from. I want to be able to click on a tv type and the stock box will randomly say in stock or out of stock how do i do this? Here is my code so far:
Java: | import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TvManufacturer implements ActionListener {
JFrame frame;
JPanel contentPane;
JComboBox tvNames;
JComboBox Stock;
JLabel tvListPrompt, Price,Store;
public TvManufacturer (){
/* Create and set up the frame */
frame = new JFrame("TV Manufacturer (EDITION 10.0)");
frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
/* Create a content pane with a BoxLayout and empty borders */
contentPane = new JPanel();
contentPane. setLayout(new BoxLayout(contentPane, BoxLayout. PAGE_AXIS));
contentPane. setBorder(BorderFactory. createEmptyBorder(9, 0, 100, 100));
/* Create a combo box and a descriptive label */
tvListPrompt = new JLabel("Select a Manufacturer:");
tvListPrompt. setAlignmentX(JLabel. RIGHT_ALIGNMENT);
contentPane. add(tvListPrompt );
/* Create a combo box and a descriptive label */
tvListPrompt = new JLabel("Stock Check: ");
tvListPrompt. setAlignmentX(JLabel. LEFT_ALIGNMENT);
contentPane. add(tvListPrompt );
String[] names = {"Sony", "Sharp", "Panasonic", "Samsung", "LG", "Toshiba"};
tvNames = new JComboBox(names );
tvNames. setAlignmentX(JComboBox. RIGHT_ALIGNMENT);
tvNames. setSelectedIndex(0);
tvNames. addActionListener(this);
contentPane. add(tvNames );
String[] stock = {"IN STOCK AT:", "OUT OF STOCK AT:", };
Stock = new JComboBox(stock );
Stock. setAlignmentX(JComboBox. LEFT_ALIGNMENT);
Stock. setSelectedIndex(0);
Stock. addActionListener(this);
contentPane. add(Stock );
/* Create and add a label that will display the Tv price range */
Price = new JLabel("$499- $19,999");
Price. setBorder(BorderFactory. createEmptyBorder(0, 0, 10, 10));
Price. setAlignmentX(JComboBox. RIGHT_ALIGNMENT);
contentPane. add(Price );
----> /* Create and add a label that will display the Tv price range */
Store = new JLabel("Walmart, Best Buy, Future Shop");
Store. setBorder(BorderFactory. createEmptyBorder(0, 10, 10, 10));
Store. setAlignmentX(JComboBox. LEFT_ALIGNMENT);
contentPane. add(Store ); <----------
/* Add content pane to frame */
frame. setContentPane(contentPane );
/* Size and then display the frame. */
frame. pack();
frame. setVisible(true);
}
/**
* Handle a selection from the combo box
* post: The TV price range for the selected TV Manufacturer has been displayed.
*/
public void actionPerformed (ActionEvent event ) {
JComboBox comboBox = (JComboBox)event. getSource();
String tvName = (String)comboBox. getSelectedItem();
String Stock = (String)comboBox. getSelectedItem();
if (tvName == "Sony") {
Price. setText("$499 - $19,999");
} else if (tvName == "Sharp") {
Price. setText("$349.99 - $5,999.98");
} else if (tvName == "Panasonic") {
Price. setText("$699.99 - $6,999.98");
} else if (tvName == "Samsung") {
Price. setText("$379.99 - $5,299.98");
} else if (tvName == "LG") {
Price. setText("$399.99 - $3,799.98");
} else if (tvName == "Toshiba") {
Price. setText("$289.99 - $2,599.99");
}
-------> if (Stock == "IN STOCK AT:") {
Store. setText("Walmart, Best Buy, Future Shop");
}else if (Stock == "OUT OF STOCK AT:"); {
Store. setText("Walmart, Best Buy, Future Shop"); {
}}} <--------
/**
* Main method
* Create and show the GUI.
*/
public static void runGUI () {
JFrame. setDefaultLookAndFeelDecorated(true);
TvManufacturer prices = new TvManufacturer ();
}
public static void main (String[] args ) {
javax. swing. SwingUtilities. invokeLater(new Runnable() {
public void run () {
runGUI ();
}
});
}
} |
The code with the arrow is what i want to randomly show when a tv brand is selected
Mod Edit: Remember to use syntax tags! Thanks code: | [syntax="java"]Code Here[/syntax] |
|