DevDavid DevDavid - 15 days ago 8
Java Question

Update a JTabbedPane when item added to JList

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:

Data goes in

And data should show here:

Data out

Answer

You need to add the JScrollPane containing JList to your GUI. Your code doesn't do that, and so the list never shows. Yes, you've got code to do this, but its within an if block, a block whose code is called only once at a time when the if condition is guaranteed to be false. So either get rid of the if block and call that code regardless of the model's size, or else have the swapping code within your action listener so that it is called more than once. Note also that it's better and easier to swap using a CardLayout.

Also you'll want to add your model once to the JList at the program start.

Comments