Gaming Java coding help needed

Godamit

Well-Known Member
OP
Member
Joined
May 5, 2009
Messages
137
Trophies
1
Age
31
Website
Visit site
XP
2,088
Country
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.
 

FireGrey

Undercover Admin
Member
Joined
Apr 13, 2010
Messages
3,921
Trophies
1
Website
www.youtube.com
XP
1,281
Country
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
 

tj_cool

Site dev
Supervisor
Joined
Jan 7, 2009
Messages
10,064
Trophies
2
Location
This planet
XP
3,107
Country
Belgium
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
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.
 

Godamit

Well-Known Member
OP
Member
Joined
May 5, 2009
Messages
137
Trophies
1
Age
31
Website
Visit site
XP
2,088
Country
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.
This, precisely.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
  • Psionic Roshambo @ Psionic Roshambo:
    That Dell data breach is worse than people probably realize....
  • Psionic Roshambo @ Psionic Roshambo:
    When I worked for Dell we had access to data about military contracts and addresses for high ranking people.
  • Psionic Roshambo @ Psionic Roshambo:
    I personally handled a call from the second highest person at Raytheon. That call bothered me a lot... The guy was nice and smart what bothered me was the way management basically just blew him off instead of going the extra mile to help him.
  • Psionic Roshambo @ Psionic Roshambo:
    In the end that call ended up costing Dell millions in lost contracts with Raytheon, and really the issue could have been solved for like 450 bucks lol
  • NinStar @ NinStar:
    sometimes I wonder why anyone would ever buy mega man x legacy collection 2
  • NinStar @ NinStar:
    I always thought that capcom shuffled the games in these collection, but apparently they are all in chronological order, which makes legacy collection 2 worthless
  • BakerMan @ BakerMan:
    guys, i want to start singing pirate metal songs and sea shanties if i play sea of thieves
  • The Real Jdbye @ The Real Jdbye:
    find a pirate metal playlist
    +2
  • The Real Jdbye @ The Real Jdbye:
    and sing along
  • BakerMan @ BakerMan:
    nevermind i just learned swearing is against the rules in sea of thieves

    i was about to start singing the song i last put in "what song are you currently listening to" yesterday
  • BakerMan @ BakerMan:
    but yeah ig so
  • The Real Jdbye @ The Real Jdbye:
    swearing not allowed in a pirate game? what has the world come to
  • BakerMan @ BakerMan:
    (here's the song for context)
  • BigOnYa @ BigOnYa:
    Just add -izle to the end of every curse word, you will be fine.
    +2
  • The Real Jdbye @ The Real Jdbye:
    i like alestorm
    +1
  • The Real Jdbye @ The Real Jdbye:
    @BigOnYa too many syllables
    +1
  • BakerMan @ BakerMan:
    same lmao
  • BigOnYa @ BigOnYa:
    hi, welcome to the Temp!
    +1
  • BakerMan @ BakerMan:
    Welcome to the Underground!
    +1
  • BakerMan @ BakerMan:
    the booty boogie from (once again) the donkey kong country cartoon could also be a good song to sing while playing sea of thieves
  • BigOnYa @ BigOnYa:
    Please insert 25 cents for an additional 30 minutes of talk time.
    +2
    Psionic Roshambo @ Psionic Roshambo: https://www.youtube.com/watch?v=tG7fk_DUz5g +1