Java coding help needed

Discussion in 'Computer Games and General Discussion' started by Godamit, Nov 2, 2011.

  1. Godamit
    OP

    Godamit GBAtemp Regular

    Member
    134
    2
    May 5, 2009
    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):

    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.
     
  2. Godamit
    OP

    Godamit GBAtemp Regular

    Member
    134
    2
    May 5, 2009
    Nevermind, I managed to solve the problem by using the method: mouseClicked().
     
  3. FireGrey

    FireGrey Undercover Admin

    Member
    3,920
    909
    Apr 13, 2010
    I'm going to go on with java once I'm done with Actionscript (Flash), I was wondering why this is here:

    public StockTable(Stock stock)
    What does it mean? :S
     
  4. tj_cool

    tj_cool Site dev

    Supervisor
    9,989
    -1
    Jan 7, 2009
    Belgium
    This planet
    It's the constructor of the StockTable class. The constructor is always the first thing to run when you use the "new" keyword.

    If you want to create a new StockTable object, you'd do something like this:
    Stock myStock = new Stock();
    StockTable myTable = new StockTable(myStock);

    By default, if you don't specify a constructor, the JVM will add an empty one for you.
     
  5. Godamit
    OP

    Godamit GBAtemp Regular

    Member
    134
    2
    May 5, 2009
    This, precisely.