I have two tabs in a Java program. One to add a stock and another to list stocks that I have created. I have tried to find away to update my list of items in the first tab when I a new item in my second tab. Any Ideas?
I want the first tab to show a list of items i am creating in the second tab...
public class StocksGUI extends javax.swing.JFrame {
private JTextField stock, qty, purchasePrice, currentPrice;
private JButton addStockButton;
private JList<StockClass> stockList;
private DefaultListModel<StockClass> stockModel;
public StocksGUI()
{
super("Portfolio Management");
stock = new JTextField();
qty = new JTextField();
purchasePrice = new JTextField();
currentPrice = new JTextField();
addStockButton = new JButton("Add Stock");
stockList = new JList<>();
stockModel = new DefaultListModel<StockClass>();
JTabbedPane tab =new JTabbedPane();
JPanel p2 = new JPanel();
if(stockModel.size() > 1)
{
stockList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
p2.add(new JScrollPane(stockList));
}
else
{
p2.add(new JLabel("Currently No Stocks"));
tab.addTab("List",null, p2, " Panel #2");
}
JPanel p1 = new JPanel(new GridLayout(5,2));
p1.add(new JLabel("Stock"));
p1.add(stock);
p1.add(new JLabel("Quantity"));
p1.add(qty);
p1.add(new JLabel("Purchase Price"));
p1.add(purchasePrice);
p1.add(new JLabel("Current price"));
p1.add(currentPrice);
p1.add(addStockButton);
tab.addTab("Add Stock",null, p1," Panel #1");
add(tab);
TickerAdd ta = new TickerAdd();
TickerAdd.StockADD stad = ta.new StockADD();
addStockButton.addActionListener(stad);
}
public class TickerAdd
{
public class StockADD implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
StockClass passing = new StockClass();
passing.stock = stock.getText();
passing.qty = Integer.parseInt(qty.getText());
passing.pp = Double.parseDouble(purchasePrice.getText());
passing.cp = Double.parseDouble(currentPrice.getText());
stockModel.addElement(passing);
stockList.setModel(stockModel);
StockBackEnd sh = new StockBackEnd();
sh.ClearInput();
stock.setText(sh.GetStockName());
qty.setText(String.valueOf(sh.GetQTY()));
purchasePrice.setText(String.valueOf(sh.GetPP()));
currentPrice.setText(String.valueOf(sh.GetCP()));
}
}
}
Enter the data here:
And data should show here: