Hey, I'd just like some help with this project I've got. Right now I've created a JList on a single tab on a JTabbedPane and the aim is once a selection is clicked on the JList it will open a new Window with certain information. I have come up with code which does so, however the problem is that 2 new Windows (instead of 1) are opened as a result. Quick help would be greatly appreciated.
The following is the code for the 2 classes involved (Just adding that the code portion listening for a click on the JList is at the end of the first piece of code):
This is linked with the following:
The code is most likely bad, but please excuse it since this is my first time building a GUI system.
The following is the code for the 2 classes involved (Just adding that the code portion listening for a click on the JList is at the end of the first piece of code):
Code:
import model.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Trading extends JPanel implements View
{
private JTextField yourId = new JTextField(20);
private JTextField code = new JTextField(20);
private JTextField number = new JTextField(20);
private JTextField price = new JTextField(20);
private JRadioButton buy = new JRadioButton("Buy", false);
private JRadioButton sell = new JRadioButton("Sell", false);
private String[] codes = {"BHP", "CTX", "LYC", "RIO", "VBA", "VAB", "VBB"};
private JList list = new JList(codes);
private Trader trader;
private Customers customers;
private Stocks stocks;
public Trading(Trader trader)
{
this.trader = trader;
customers = trader.customers();
stocks = trader.stocks();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setup();
}
public void setup()
{
add(pair("Your id", yourId));
add(pair("Code", code));
add(pair("Number", number));
add(pair("Price", price));
add(buttonPair(buy, sell));
buy.addActionListener(new BuyListener());
sell.addActionListener(new SellListener());
add(list);
list.addListSelectionListener(new CodeListener());
}
public Box pair(String label, JTextField field)
{
Box box = Box.createHorizontalBox();
box.add(new JLabel(label));
box.add(field);
return box;
}
public Box buttonPair(JRadioButton buttonOne, JRadioButton buttonTwo)
{
Box box = Box.createHorizontalBox();
box.add(buttonOne);
box.add(buttonTwo);
return box;
}
public Customer customer()
{
return customers.customer(id());
}
public Stock stock()
{
return stocks.stock(code());
}
public int id()
{
return Integer.parseInt(yourId.getText());
}
public String code()
{
return code.getText().toUpperCase();
}
public int number()
{
return Integer.parseInt(number.getText());
}
public double price()
{
return Double.parseDouble(price.getText());
}
public void update()
{
yourId.setText("");
code.setText("");
number.setText("");
price.setText("");
}
private class BuyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
customer().buy(stock(), number(), price());
buy.setSelected(false);
update();
}
}
private class SellListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
customer().sell(stock(), number(), price());
sell.setSelected(false);
update();
}
}
private class CodeListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
String selected = (String) list.getSelectedValue();
new StockTable(stocks.stock(selected));
}
}
}
This is linked with the following:
Code:
import model.*;
import java.text.DecimalFormat;
import javax.swing.*;
public class StockTable extends JFrame
{
private Stock stock;
public StockTable(Stock stock)
{
this.stock = stock;
setSize(400, 90);
setLocation(485, 500);
add(new TablePanel());
setVisible(true);
}
private class TablePanel extends JPanel
{
private final String[] headers = {"Code", "Value", "Min", "Max", "Number"};
private final String[][] data = {{stock.code(),
decimalPlaces(stock.price()),
decimalPlaces(stock.min()),
decimalPlaces(stock.max()),
Integer.toString(stock.number())}};
private JTable table = new JTable(data, headers);
public TablePanel()
{
add(table.getTableHeader());
add(table);
}
public String decimalPlaces(double number)
{
DecimalFormat df = new DecimalFormat("0.00");
return df.format(number);
}
}
}
The code is most likely bad, but please excuse it since this is my first time building a GUI system.