Screen not reflecting input/output

I'm a total noob, so be gentle.

I'm trying to read a field from a screen, use the input to hit a database and return a row, then put the data into the screen and rewrite the screen. Simple enough, you'd think.

The problem is, the value I bring in from the screen is simply the default value I assigned in the Panel constructor; the entered data never appears. Nor does the data I read from the database.

I have a class that defines the Panels and loads the components, then another class that defines the Frame and loads the panels. A third class contains the ActionListener that gets driven when the button is pushed. Each Panel class contains getters and setters for each of its fields. Those getters and setters are what the ActionListener class uses to retrieve the screen data and set the new data from the database.

I suspect this has something to do with the way I'm instantiating the Panels.

I coded up a simpler version where I just had a driver class and a Panel class and everything worked fine.

[1047 byte] By [IronJacka] at [2007-10-3 3:39:05]
# 1
> I coded up a simpler version where I just had a driver class and a Panel class and everything > worked fine.Then, what is the difference between the two, simpler version and the production version?
hiwaa at 2007-7-14 21:34:27 > top of Java-index,Desktop,Core GUI APIs...
# 2

> Then, what is the difference between the two, simpler

> version and the production version?

Well, aside from a thousand lines of code, the way the ActionListener is coded. In the simple version, I have it as an inner class of the Frame class. In the more complex version, it is registered in the Frame class, but the actionPerformed method is in yet another class.

I can show you the code, but there's a lot of it.

IronJacka at 2007-7-14 21:34:27 > top of Java-index,Desktop,Core GUI APIs...
# 3

> > Then, what is the difference between the two,

> simpler

> > version and the production version?

>

> Well, aside from a thousand lines of code, the way

> the ActionListener is coded. In the simple version,

> I have it as an inner class of the Frame class. In

> the more complex version, it is registered in the

> Frame class, but the actionPerformed method is in yet

> another class.

>

> I can show you the code, but there's a lot of it.

Then, the scope of the variables that represent input and output is wrong.

Better solution would be to pass them as constructor arguments of your separate action listener class.

hiwaa at 2007-7-14 21:34:27 > top of Java-index,Desktop,Core GUI APIs...
# 4

I think you're right. It's the scope of the JTextField variables. I'm going to post the code, so hang on.

This is the Frame class:

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.*;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.sql.Connection;

import java.sql.DriverManager;

import java.text.NumberFormat;

public class OrderMasterScreen extends JFrame

{

AddOrder listener = new AddOrder();

private OrderDataPanel orderData;

private CustomerDataPanel customerData;

private ItemDataPanel itemData;

private JPanel buttonPanel;

private JButton addButton;

private JButton inqButton;

private JButton updButton;

private JButton canButton;

final int ITEM_MAX = 300;

public OrderMasterScreen()

{

super("Greenberg Fruit Company");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

orderData = new OrderDataPanel();

customerData = new CustomerDataPanel();

itemData = new ItemDataPanel();

buildButtonPanel();

add(orderData, BorderLayout.NORTH);

add(customerData, BorderLayout.WEST);

add(itemData, BorderLayout.SOUTH);

add(buttonPanel, BorderLayout.EAST);

pack();

setVisible(true);

}

private void buildButtonPanel()

{

addButton = new JButton("ADD");

inqButton = new JButton("INQ");

updButton = new JButton("UPD");

canButton = new JButton("CAN");

addButton.addActionListener(listener);

buttonPanel = new JPanel();

buttonPanel.add(addButton);

buttonPanel.add(inqButton);

buttonPanel.add(updButton);

buttonPanel.add(canButton);

}

public static void main(String[] args)

{

OrderMasterScreen oms = new OrderMasterScreen();

}

}

This is one of the Panels

import java.awt.*;

import javax.swing.*;

public class CustomerDataPanel extends JPanel

{

private JPanel CustomerDataPanel;

private JLabel customerNumberLabel;

private JTextField customerNumberText;

private JLabel customerNameLabel;

private JTextField customerNameText;

private JLabel customerStreetLabel;

private JTextField customerStreetText;

private JLabel customerCityLabel;

private JTextField customerCityText;

private JLabel customerStateLabel;

private JTextField customerStateText;

private JLabel customerZipLabel;

private JTextField customerZipText;

public CustomerDataPanel()

{

JPanel CustomerDataPanel = new JPanel();

setLayout(new GridLayout(6, 2, 10, 10));

customerNumberLabel = new JLabel("Customer No.", JLabel.RIGHT);

customerNumberText = new JTextField("0000", JTextField.LEFT);

customerNameLabel = new JLabel("Customer Name", JLabel.RIGHT);

customerNameText = new JTextField("", JTextField.LEFT);

customerNameText.setEditable(false);

customerStreetLabel = new JLabel("Street", JLabel.RIGHT);

customerStreetText = new JTextField("", JTextField.LEFT);

customerStreetText.setEditable(false);

customerCityLabel = new JLabel("City", JLabel.RIGHT);

customerCityText = new JTextField("", JTextField.LEFT);

customerCityText.setEditable(false);

customerStateLabel = new JLabel("State", JLabel.RIGHT);

customerStateText = new JTextField("", JTextField.LEFT);

customerStateText.setEditable(false);

customerZipLabel = new JLabel("Zip code", JLabel.RIGHT);

customerZipText = new JTextField("", JTextField.LEFT);

customerZipText.setEditable(false);

setBorder(BorderFactory.createTitledBorder("Customer Data"));

setVisible(true);

add(customerNumberLabel);

add(customerNumberText);

add(customerNameLabel);

add(customerNameText);

add(customerStreetLabel);

add(customerStreetText);

add(customerCityLabel);

add(customerCityText);

add(customerStateLabel);

add(customerStateText);

add(customerZipLabel);

add(customerZipText);

// setCustomerNo("New value");

}

public String getCustomerNo() throws Exception

{

String custNo = new String();

custNo = customerNumberText.getText();

return custNo;

}

public String getCustomerName()

{

String custName = customerNameText.getText();

return custName;

}

public String getCustomerStreet()

{

String custStreet = customerStreetText.getText();

return custStreet;

}

public String getCustomerCity()

{

String custCity = customerCityText.getText();

return custCity;

}

public String getCustomerState()

{

String custState = customerStateText.getText();

return custState;

}

public String getCustomerZip()

{

String custZip = customerZipText.getText();

return custZip;

}

public void setCustomerNo(String cNo)

{

// customerNumberText = new JTextField();

customerNumberText.setText(cNo);

}

public void setCustomerName(String cName)

{

// customerNameText = new JTextField(cName);

customerNameText.setText(cName);

}

public void setCustomerStreet(String cStreet)

{

// customerStreetText = new JTextField(cStreet);

customerStreetText.setText(cStreet);

}

public void setCustomerCity(String cCity)

{

// customerCityText = new JTextField(cCity);

customerCityText.setText(cCity);

}

public void setCustomerState(String cState)

{

// customerStateText = new JTextField(cState);

customerStateText.setText(cState);

}

public void setCustomerZip(String cZip)

{

// customerZipText = new JTextField(cZip);

customerZipText.setText(cZip);

}

}

This is the other Panel

import java.awt.*;

import javax.swing.*;

public class ItemDataPanel extends JPanel

{

final int ITEM_MAX = 30;

private JTextField[] itemNumberText;

private JTextField[] itemDescriptionText;

private JTextField[] itemPriceText;

private JTextField[] itemQuantityText;

public ItemDataPanel()

{

JPanel ItemDataPanel = new JPanel();

setLayout(new GridLayout(16, 8, 5, 5));

setBorder(BorderFactory.createTitledBorder("Item Data"));

JLabel itemNumberLabel1 = new JLabel("Item No.");

JLabel itemDescriptionLabel1 = new JLabel("Item Description");

JLabel itemPriceLabel1 = new JLabel("Price/Unit");

JLabel itemQuantityLabel1 = new JLabel("Qty");

JLabel itemNumberLabel2 = new JLabel("Item No.");

JLabel itemDescriptionLabel2 = new JLabel("Item Description");

JLabel itemPriceLabel2 = new JLabel("Price/Unit");

JLabel itemQuantityLabel2 = new JLabel("Qty");

itemNumberText = new JTextField[ITEM_MAX];

itemDescriptionText = new JTextField[ITEM_MAX];

itemPriceText = new JTextField[ITEM_MAX];

itemQuantityText = new JTextField[ITEM_MAX];

add(itemNumberLabel1);

add(itemDescriptionLabel1);

add(itemPriceLabel1);

add(itemQuantityLabel1);

add(itemNumberLabel2);

add(itemDescriptionLabel2);

add(itemPriceLabel2);

add(itemQuantityLabel2);

for (int i = 0; i < ITEM_MAX; i++)

{

itemNumberText[i] = new JTextField(5);

itemDescriptionText[i] = new JTextField(25);

itemPriceText[i] = new JTextField(6);

itemQuantityText[i] = new JTextField(3);

itemNumberText[i].setText("00000");

itemDescriptionText[i].setText("");

itemDescriptionText[i].setEditable(false);

itemPriceText[i].setText("");

itemPriceText[i].setEditable(false);

itemQuantityText[i].setText("000");

add(itemNumberText[i]);

add(itemDescriptionText[i]);

add(itemPriceText[i]);

add(itemQuantityText[i]);

}

}

public String[] getItemNo() throws Exception

{

String[] iNumber = new String[ITEM_MAX];

for (int i = 0; i < ITEM_MAX; i++)

{

iNumber[i] = itemNumberText[i].getText();

}

return iNumber;

}

public String[] getItemDesc()

{

String[] iDesc = new String[ITEM_MAX];

for (int i = 0; i < ITEM_MAX; i++)

{

iDesc[i] = itemDescriptionText[i].getText();

}

return iDesc;

}

public String[] getItemPrice()

{

String[] iPrice = new String[ITEM_MAX];

for (int i = 0; i < ITEM_MAX; i++)

{

iPrice[i] = itemPriceText[i].getText();

}

return iPrice;

}

public String[] getItemQty()

{

String[] iQty = new String[ITEM_MAX];

for (int i = 0; i < ITEM_MAX; i++)

{

iQty[i] = itemQuantityText[i].getText();

}

return iQty;

}

public void setItemNo(String[] iNos, int iNoCount)

{

for (int i = 0; i < iNoCount; i++)

{

itemNumberText[i].setText(iNos[i]);

}

}

public void setItemDesc(String[] iDes, int iDescCount)

{

for (int i = 0; i < iDescCount; i++)

{

itemDescriptionText[i].setText(iDes[i]);

}

}

public void setItemPrice(String[] iPri, int iPriCount)

{

for (int i = 0; i < iPriCount; i++)

{

itemPriceText[i].setText(iPri[i]);

}

}

public void setItemQty(String[] iQty, int iQtyCount)

{

for (int i = 0; i < iQtyCount; i++)

{

itemQuantityText[i].setText(iQty[i]);

}

}

}

This is the ActionListener

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.*;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.text.NumberFormat;

public class AddOrder implements ActionListener

{

final int ITEM_MAX = 30;

OrderMasterScreen gui;

CustomerDataPanel customerData;

ItemDataPanel itemData;

/*public AddOrder(OrderMasterScreen in)

{

gui = in;

}*/

public void actionPerformed(ActionEvent e)

{

try

{

getCustList();

}

catch(SQLException f)

{

f.printStackTrace();

}

catch(Exception g)

{

}

try

{

getItemList();

}

catch(SQLException f)

{

f.printStackTrace();

}

catch(Exception g)

{

}

}

public void getCustList() throws Exception, SQLException

{

customerData = new CustomerDataPanel();

Connection dbase = getConnection();

System.out.println("Before getCustomerNo");

String inputCustNo = customerData.getCustomerNo();

int inputCustInt = Integer.parseInt(inputCustNo);

String cquery = "SELECT * FROM Customer_table WHERE customer_id = '" + inputCustInt + "'";

PreparedStatement stat = dbase.prepareStatement(cquery);

ResultSet custList = stat.executeQuery(cquery);

custList = stat.executeQuery(cquery);

custList = stat.executeQuery(cquery);

custList.next();

try

{

customerData.setCustomerName(custList.getString("customer_name"));

customerData.setCustomerStreet(custList.getString("customer_street_address"));

customerData.setCustomerCity(custList.getString("customer_city"));

customerData.setCustomerState(custList.getString("customer_state"));

customerData.setCustomerZip(custList.getString("customer_zip"));

}

catch(SQLException s)

{

System.out.println("Error on row read");

}

System.out.println("customer #: " + customerData.getCustomerNo());

System.out.println(customerData.getCustomerName());

System.out.println(customerData.getCustomerStreet());

System.out.println(customerData.getCustomerCity());

System.out.println(customerData.getCustomerState());

System.out.println(customerData.getCustomerZip());

// }

dbase.close();

}

public void getItemList() throws Exception, SQLException

{

ItemDataPanel itemData = new ItemDataPanel();

Connection dbase = getConnection();

// define a String array to hold the screen item numbers

String[] itemNoArray = new String[ITEM_MAX];

// read the screen item numbers into the String array

itemNoArray = itemData.getItemNo();

// define an integer array to hold the integer-ized item numbers

int[] itemIntArray = new int[ITEM_MAX];

// this loop translates the String item number array into integers

for (int i = 0; i < ITEM_MAX; i++)

{

itemIntArray[i] = Integer.parseInt(itemNoArray[i]);

}

// define the query to be used to read the item data table

int i = 0;

String iquery = "SELECT * FROM Item_table WHERE item_id = '" + itemIntArray[i] + "'";

PreparedStatement stat = dbase.prepareStatement(iquery);

ResultSet itemRow = null;

// define a String array to hold the item descriptions

String[] itemDescArray = new String[ITEM_MAX];

// define a String array to hold the item prices

String[] itemPriceArray = new String[ITEM_MAX];

// see if there is any data in the item table

// boolean retrievedSomething = stat.execute(iquery);

// if there is, read the first row, keying on the first item number entered on the screen

// if (retrievedSomething)

// {

// this loop reads the item data table for each successive item number on the screen, then

// moves the row's DESCRIPTION and PRICE fields to their respective arrays

for (i = 0; i < ITEM_MAX; i++)

{

try

{

itemRow = stat.executeQuery(iquery);

itemRow.next();

itemDescArray[i] = itemRow.getString("item_description");

itemPriceArray[i] = itemRow.getString("item_base_price");

}

catch(SQLException x)

{

System.out.println("Error on item read " + i);

}

}

// }

// Now, with the item arrays all loaded with their table data, call the respective

// setters to put the new data onto the screen

itemData.setItemDesc(itemDescArray, ITEM_MAX);

itemData.setItemPrice(itemPriceArray, ITEM_MAX);

System.out.println(itemRow.getString("item_id"));

System.out.println(itemRow.getString("item_description"));

System.out.println(itemRow.getString("item_base_price"));

dbase.close();

}

public static Connection getConnection() throws Exception

{

final String url = "jdbc:mysql://localhost/greenberg";

final String username = "root";

final String password = null;

Class.forName("com.mysql.jdbc.Driver");

Connection conn = DriverManager.getConnection(url, username, password);

return conn;

}

}

There's some diagnostic println's and stuff in there, but this is it.

IronJacka at 2007-7-14 21:34:28 > top of Java-index,Desktop,Core GUI APIs...
# 5

public void getCustList() throws Exception, SQLException

{

customerData = new CustomerDataPanel();

Wroooong! This panel isn't added onto nowhere! Let alone make it visible.

Never instantiate the GUI in an event handler.

All the basic GUI parts should be prepared in the main initialization code or constructor of the main application class. And, some buttons should simply activate, or visualize, some of them.

Setting up DB connection should also be done in the preparation process above.

Your design is basically wrong as a Swing application.

You should call addActionListener() for each, or a specific, text field and its actionPerformed() method should do the required business logic which sometimes should be put into a separate thread. If you need to update GUI from the separate thread, you should use SwingUtilities.invokeLater() method.

hiwaa at 2007-7-14 21:34:28 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Wroooong! This panel isn't added onto nowhere! Let

> alone make it visible.

What? I don't understand what you're saying.

> All the basic GUI parts should be prepared in the

> main initialization code or constructor of the main

> application class. And, some buttons should simply

> activate, or visualize, some of them.

The JTextFields are where the data is entered. The ADD button then activates the listener to use that input data to read a database. The GUI code exists in the Frame and Panel classes, but I need some way to reference it in the listener because that's where the getter and setter methods are called.

>

> Setting up DB connection should also be done in the

> preparation process above.

Yeah, I know. But the DB connection works fine as it is. I'll worry about breaking it out later.

>

> Your design is basically wrong as a Swing

> application.

It's the same structure I've used successfully on a dozen other projects.

> You should call addActionListener() for each, or a

> specific, text field and its actionPerformed() method

The ActionListener isn't registered to any of the JTextFields. It's registered to a JButton (actually, several of them). Pressing the JButton is supposed to invoke the getter methods from the JPanels, which it does. But the JPanel getText methods only return their initial or default values, not what was entered.

I don't think I have to thread this application. It's nothing more than a glorified input/file-read/output program.

IronJacka at 2007-7-14 21:34:28 > top of Java-index,Desktop,Core GUI APIs...
# 7

Compare the above code with this far simpler version that simply takes input from a text field, puts it into a new field, changes the entered data, then redisplays the screen, all from the push of a button:

The GUI/Frame class:

import java.awt.*;

import javax.swing.*;

public class TestScreen extends JPanel

{

private JTextField enteredData;

private JTextField displayData;

public TestScreen()

{

JLabel enteredLabel = new JLabel("Enter data here");

JLabel displayLabel = new JLabel("You entered");

enteredData = new JTextField("Initial entry");

displayData = new JTextField("Initial display");

setLayout(new GridLayout(3, 2));

add(enteredLabel);

add(enteredData);

add(displayLabel);

add(displayData);

}

public String getEntry()

{

String rEntry = enteredData.getText();

return rEntry;

}

public void setDisplay(String inDisplay)

{

displayData.setText(inDisplay);

}

public void setInput(String inPut)

{

enteredData.setText(inPut);

}

}

The ActionListener class:

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.*;

import javax.swing.*;

public class TestText extends JFrame

{

private TestScreen tScreen;

private JButton enter;

public TestText()

{

super("test screen");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

tScreen = new TestScreen();

add(tScreen, BorderLayout.NORTH);

enter = new JButton("Enter");

add(enter, BorderLayout.SOUTH);

pack();

setVisible(true);

ButtonListener buttonListener = new ButtonListener();

enter.addActionListener(buttonListener);

}

public class ButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String dText = tScreen.getEntry();

tScreen.setDisplay(dText);

tScreen.setInput("Updated");

}

}

public static void main(String[] args)

{

TestText tt = new TestText();

}

}

I coded this up in about five minutes, and at its heart, it does the same thing I'm trying to do in the other code.

I just don't see what I'm doing different.

IronJacka at 2007-7-14 21:34:28 > top of Java-index,Desktop,Core GUI APIs...
# 8

> it does the same thing I'm trying to do in the other code

No. In your simpler version, GUI is prepared in the initialization code which is class constructor.

In your horrible production code, you instantiate a principal GUI in an event handler and it isn't added onto any of main windows. And no event handlers added to its components.

> It's the same structure I've used successfully on a dozen other projects.

You can get any success with such wrong design, I assure you.

hiwaa at 2007-7-14 21:34:28 > top of Java-index,Desktop,Core GUI APIs...
# 9

> In your simpler version, GUI is prepared in the

> initialization code which is class constructor.

No it isn't! The TestScreen class contains all the components and the Panels. The TestText class is the container for all the Panels, but it also contains the Listener.

> In your horrible production code,

Look, if you want to help, great. But I can live without the editorializing.

>you instantiate a

> principal GUI in an event handler and it isn't added

> onto any of main windows.

What is a "principal GUI" and a "main window"? I have a container class (OrderMasterScreen), component classes (ItemDataPanel and CustomerDataPanel), and an ActionListener(AddOrder). I instantiate the CustomerDataPanel and ItemDataPanel in the ActionListener because I need to call their methods to get and set the screen data!

>And no event handlers added

> to its components.

The JTextFields don't NEED event handlers! They are not driving any logic. The JButton(s) are.

> You can get any success with such wrong design, I

> assure you.

I'm sorry. Once again, I can't understand you.

IronJacka at 2007-7-14 21:34:28 > top of Java-index,Desktop,Core GUI APIs...
# 10

>> You can get any success with such wrong design, I

>> assure you.

>

>I'm sorry. Once again, I can't understand you.

You create a new instance of CustomerDataPanel in your ActionListener. But it is not added onto any frame or main window nor isn't made visible. This is the third time I mention that. If you need fourth time or more, you better quit software development for the safety of our society.

hiwaa at 2007-7-14 21:34:28 > top of Java-index,Desktop,Core GUI APIs...
# 11

here's your code, heavily stripped.

it's essentially the same code, but just has a label in both customer and item panels.

these are the panels you are trying to access/modify in your actionListener code.

all this does, when you click the button, is change those label's text.

run it, it doesn't work (for the reasons hiwa has been pointing out).

try to get it to work, then modify your program accordingly

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class OrderMasterScreen extends JFrame

{

AddOrder listener = new AddOrder();

private CustomerDataPanel customerData;

private ItemDataPanel itemData;

private JPanel buttonPanel;

private JButton addButton;

public OrderMasterScreen()

{

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

customerData = new CustomerDataPanel();

itemData = new ItemDataPanel();

buildButtonPanel();

getContentPane().add(customerData, BorderLayout.WEST);

getContentPane().add(itemData, BorderLayout.SOUTH);

getContentPane().add(buttonPanel, BorderLayout.EAST);

pack();

setVisible(true);

}

private void buildButtonPanel()

{

addButton = new JButton("ADD");

addButton.addActionListener(listener);

buttonPanel = new JPanel();

buttonPanel.add(addButton);

}

public static void main(String[] args)

{

OrderMasterScreen oms = new OrderMasterScreen();

}

}

class CustomerDataPanel extends JPanel

{

JLabel label = new JLabel("CustomerDataPanel");

public CustomerDataPanel()

{

add(label);

}

}

class ItemDataPanel extends JPanel

{

JLabel label = new JLabel("ItemDataPanel");

public ItemDataPanel()

{

add(label);

}

}

class AddOrder implements ActionListener

{

CustomerDataPanel customerData;

ItemDataPanel itemData;

public void actionPerformed(ActionEvent e)

{

getCustList();

getItemList();

}

public void getCustList()

{

customerData = new CustomerDataPanel();

customerData.label.setText("in actionperformed");

}

public void getItemList()

{

ItemDataPanel itemData = new ItemDataPanel();

itemData.label.setText("in ACTIONPERFORMED");

}

}

Michael_Dunna at 2007-7-14 21:34:28 > top of Java-index,Desktop,Core GUI APIs...
# 12
And let me add a more important thing.GUI event handler is usually called indeterminate times by user actions in an application session.If you instantiate GUI component in the event handler without proper housekeeping work, the result must be a disaster.
hiwaa at 2007-7-14 21:34:28 > top of Java-index,Desktop,Core GUI APIs...
# 13

Others seem to have your main points covered so I just had a quick skim read.

Instantiating UI components in event handling is fine as long as it's not expensive (and, as noted above, as long as you're not accidentally proliferating instances on multiple calls) - UI components have to be constructed on the event dispatch thread (EDT) anyway, even if not in an "actual" event - so it is where you want to be doing it, as long as the event handling returns quickly.

the one thing you absolutely, definitely, 100% do NOT want to be doing on the EDT, is making potentially time-consuming calls such as obtaining database connections or making use of network connections. These can very easily take long periods of time during which your UI will be locked up. They need to be fired on separate threads.

Doing these sorts of operations on user actions is a non-trivial bit of programming and I'd advise you to look up threading and the EDT in the Java tutorial, and then write yourself some little utility classes - eg I have an AbstractAction subclass which I use to handle jumping on and off of the EDT amongst other things.

itchyscratchya at 2007-7-14 21:34:28 > top of Java-index,Desktop,Core GUI APIs...
# 14

> > import java.awt.*;

> import java.awt.event.*;

> import javax.swing.*;

>

> class OrderMasterScreen extends JFrame

> {

>AddOrder listener = new AddOrder();

> private CustomerDataPanel customerData;

>private ItemDataPanel itemData;

> private JPanel buttonPanel;

>private JButton addButton;

>public OrderMasterScreen()

> {

>setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

> customerData = new CustomerDataPanel();

>itemData = new ItemDataPanel();

> buildButtonPanel();

> getContentPane().add(customerData,

> BorderLayout.WEST);

> getContentPane().add(itemData,

> BorderLayout.SOUTH);

> getContentPane().add(buttonPanel,

> BorderLayout.EAST);

>pack();

> setVisible(true);

> }

> private void buildButtonPanel()

> {

>addButton = new JButton("ADD");

> addButton.addActionListener(listener);

>buttonPanel = new JPanel();

> buttonPanel.add(addButton);

> }

> public static void main(String[] args)

> {

>OrderMasterScreen oms = new OrderMasterScreen();

>

> }

> class CustomerDataPanel extends JPanel

> {

>JLabel label = new JLabel("CustomerDataPanel");

> public CustomerDataPanel()

>{

>add(label);

> }

> }

> class ItemDataPanel extends JPanel

> {

>JLabel label = new JLabel("ItemDataPanel");

> public ItemDataPanel()

>{

>add(label);

> }

> }

> class AddOrder implements ActionListener

> {

>CustomerDataPanel customerData;

> ItemDataPanel itemData;

>public void actionPerformed(ActionEvent e)

> {

>getCustList();

> getItemList();

>}

> public void getCustList()

>{

>customerData = new CustomerDataPanel();

>customerData.label.setText("in actionperformed");

> }

>public void getItemList()

> {

>ItemDataPanel itemData = new ItemDataPanel();

> itemData.label.setText("in ACTIONPERFORMED");

>}

>

>

-

Remove the instantiations of customerData and ItemDataPanel in the respective getXXXX methods and this will work.

-

My getter methods (in their respective Panel classes) don't instantiate the panels. The getList methods in the ActionListener do, but removing those instances has no effect.

current code:

public void getCustList() throws Exception, SQLException

{

// gui = new OrderMasterScreen();

customerData = new CustomerDataPanel();

Connection dbase = getConnection();

System.out.println("Before getCustomerNo");

String inputCustNo = customerData.getCustomerNo();

int inputCustInt = Integer.parseInt(inputCustNo);

String cquery = "SELECT * FROM Customer_table WHERE customer_id = '" + inputCustInt + "'";

PreparedStatement stat = dbase.prepareStatement(cquery);

ResultSet custList = stat.executeQuery(cquery);

custList = stat.executeQuery(cquery);

custList = stat.executeQuery(cquery);

custList.next();

try

{

customerData.setCustomerName(custList.getString("customer_name"));

customerData.setCustomerStreet(custList.getString("customer_street_address"));

customerData.setCustomerCity(custList.getString("customer_city"));

customerData.setCustomerState(custList.getString("customer_state"));

customerData.setCustomerZip(custList.getString("customer_zip"));

}

I can remove the instantiation of CustomerDataPanel:

public void getCustList() throws Exception, SQLException

{

// gui = new OrderMasterScreen();

[b]// customerData = new CustomerDataPanel();[/b]

Connection dbase = getConnection();

System.out.println("Before getCustomerNo");

String inputCustNo = customerData.getCustomerNo();

int inputCustInt = Integer.parseInt(inputCustNo);

String cquery = "SELECT * FROM Customer_table WHERE customer_id = '" + inputCustInt + "'";

PreparedStatement stat = dbase.prepareStatement(cquery);

ResultSet custList = stat.executeQuery(cquery);

custList = stat.executeQuery(cquery);

custList = stat.executeQuery(cquery);

custList.next();

try

{

customerData.setCustomerName(custList.getString("customer_name"));

customerData.setCustomerStreet(custList.getString("customer_street_address"));

customerData.setCustomerCity(custList.getString("customer_city"));

customerData.setCustomerState(custList.getString("customer_state"));

customerData.setCustomerZip(custList.getString("customer_zip"));

}

... and it makes no difference at all.

IronJacka at 2007-7-14 21:34:28 > top of Java-index,Desktop,Core GUI APIs...
# 15

> You can get any success with such wrong design, I

> assure you ...

>... it is not added onto any

> frame or main window nor isn't made visible.

This is what passes for English coming from you, and you have the nerve to tell ME I'm missing something!?!?! I'm sorry I don't comprehend what you're trying to write; your advice might actually be useful if I did.

> This is the third time I mention that.

Maybe if you tried English ...

>If you need fourth

> time or more, you better quit software development

> for the safety of our society.

"Our" society needs protection from rude jerks more than it needs protection from an earnest, if somewhat bumbling, student.Do you make it a habit to troll the beginner forums, dispensing your Yoda-like wisdom (and in much the same fractured syntax) so you can stroke your ego? You're like the sixth-grade bully who hangs around picking on kindergartners because it makes him feel tough.

Get lost.

IronJacka at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 16

> ... and it makes no difference at all

Give the real customerData object to the constructor of your ActionListener -- AddOrder -- from your app main class when you instantiate the listener.

But I think you should learn Java language and Java programming basics before anything else.

Current your design is still horrible.

hiwaa at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 17
Actually, removing those instances just gives me NullPointerExceptions. Let me try something else.
IronJacka at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 18

> But I think you should learn Java language and Java

> programming basics before anything else.

I'll tell you what. You go to an English language forum and I'll stay here and try to learn Java. You need to be able to communicate before anything else.

> Current your design is still horrible.

Almost as bad as your grammar.

IronJacka at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 19

> Maybe if you tried English ...

I doubt any language would help you.

How many times in this thread have you been told/shown what your immediate

problems are.

You are going to do it your way, and that's it - you just want someone to fix

it for you, your way - even though it is totally unfixable that way.

From the code you posted, your non-immediate problems are far more serious,

and you are doing everthing you can to ensure you get 0 help when you strike

those problems.

> I'll tell you what. You go to an English language forum

I'll back hiwa's english against your java skills any day

> and I'll stay here and try to learn Java.

never going to happen

Michael_Dunna at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 20

All right you jerks!

All I asked for was some help. If you didn't want to help, nobody forced you to. This is a forum for posting problems and looking for assistance; I didn't come here to get insulted, lectured to, or patronized.

You smarmy, self-righteous schmucks with 10 years of experience come on to a noob post and throw around a bunch of technical jargon then act like you've done someone a great favor.

If I'm determined to do this my way, ti's because it's MY code! You want me to do it YOUR way because you want me to write YOUR program.

If you're flat out telling me this canNOT work this way, then at least have the courtesy to tell me WHY instead of just making snide remarks about it being "horrible" and a "danger to society."

For pete's sake, none of YOU started out knowing what you do. You all had to learn it someplace.

IronJacka at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 21
> All I asked for was some help.....that was better than "Gone With the Wind", I went through half abox of tissues.
Michael_Dunna at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 22
Your mom read it to you?
IronJacka at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 23

> If you're flat out telling me this canNOT work this way, then at least have the

> courtesy to tell me WHY

when did you ever have the courtesy to ask WHY?

you didn't, because it's not in your nature - you know eveything, you won't be told,

you won't listen - just typical of the current crop of punks.

Michael_Dunna at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 24

> Actually, removing those instances just gives me NullPointerExceptions.

Because your action listener does not have a valid reference of the customer data panel. Give it as a constructor argument of your listener class(AddOrder class). That would be the simplest solution for your immediate problem.

Do like this:

public OrderMasterScreen()

{

super("Greenberg Fruit Company");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

orderData = new OrderDataPanel();

customerData = new CustomerDataPanel();

listener = new AddOrder(customerData);

For you, however, some non-immediate problem is much much more important and critical.

hiwaa at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 25

What are you talking about? All I've DONE is asked 'why?"!!! I was under the impression this was a simple problem, something I'd done wrong in instantiating the panels. I've had comments about starting separate threads, defining different classes, breaking out the SQL, and on and on and on.

If the hole in my structure is conceptual, all right. I admitted at the very outset that I am a rank amateur. But as I understand it, I should be able to build a "main" class that registers my button listener. That listener should get invoked when the action event is triggered. The panels that were present should be available to that listener, with their associated data. It's that part I'm hazy on. I thought I was accessing the panels with the new data. But I'm not. I'm only seeing the defaults.

That tells me I must be doing something wrong when I instantiate the panels with the data components in them. But shouldn't the getter retrieve the data from the Panel object IT has? And if that object is the old, unchanged (default) panel, then I'm unclear as to how to get the new, fresh version.

This may seem like something so elementary it's beneath your notice. But to someone just starting out, it is imperative that I understand this concept or I'll never go any further.

IronJacka at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 26
Thank you!!!!!!I will try that. And I assure you, I WILL work on the other, non-immediate problems too! I just need to take one (baby) step at a time.I will get right back to you.
IronJacka at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 27
I'm just going to have to walk away from this one. None of this makes any sense to me, so maybe it's God's way of telling me I'm in over my head. All I do is fix one thing and break two more.
IronJacka at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 28
> None of this makes any sense to meThen, your non-immediate problem completely overrides your 'self'.What a pitiful sight.There's no royal road for learning. Go from basics, step by step.
hiwaa at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 29

> There's no royal road for learning. Go from basics,

> step by step.

I thought I was. Seriously!

I taught myself the component logic, and started with some simple applications that gathered data and displayed it.

Then I learned MySQL enough to install the database and build the tables.

Then I built the Frame class and added some panels to it, and eventually added the SQL calls, which, again, I taught myself.

And everything works ... right up till I add the Listener.

IronJacka at 2007-7-21 10:14:53 > top of Java-index,Desktop,Core GUI APIs...
# 30
Would I be able to make the Listener an inner class of the OrderMasterScreen, but put the SQL calls into a separate class, then return the queried row as an object from the latter class to the Listener and process the data there?I mean structurally, would that work?
IronJacka at 2007-7-21 10:14:58 > top of Java-index,Desktop,Core GUI APIs...
# 31
Thanks. That's what I thought.
IronJacka at 2007-7-21 10:14:58 > top of Java-index,Desktop,Core GUI APIs...
# 32

I wouldn't expect any answers to your questions after you insulted pretty much everyone who has tried to help you (and you don't have to start looking for mistakes in grammar, English isn't my native language).

One thing that you have to know if you decide to stick with Swing - for every UI design that you have in your mind there are 20 different ways to code them in Swing. While some may seem easier to you at first, they quickly lead you into a nightmare of unmaintainable and unstable code. This is why people have recommended to you rethink the basic building blocks to your application, especially since it's something not that big.

Start from smaller applications, and read sample code found at Sun's Swing tutorials. I don't think that anybody expects you to code their way just because they like their way. It's just that when you ask people for help and give 1000 line application, it's much easier for experienced people to understand it if it's written according to the basic Swing design principles. This way, it's much easier to understand and fix the small (immediate) problems, since the basic design is according to the "Swing way".

Suppose you're building a house made of wood in the middle of Nevada desert. From the very beginning, you have this small fires all over the house. You ask people - how do i put them out. People may point you to the fact that the basic building block of your house should have been stone and not wood. This is your case - instead of continuously putting out small fires, you should concentrate on the basics.

And the fact that you built tens of applications doesn't mean that they can be maintained by someone else. "Your way" is not necessarily the right way.

kirillga at 2007-7-21 10:14:58 > top of Java-index,Desktop,Core GUI APIs...
# 33

I've learned not to expect much from this forum. It seems to be more a place for Melvins to show off their programming muscle than to offer any useful information. But that's okay. I thought I'd give it a shot and it didn't turn out very well.

I didn't start the insults, but I'm not going to grovel at anyone's feet just to get some free assistance. I offered what I thought was a legitimate question and the responses weren't particularly enlightening.

I've taught myself whatever Java I know. I love the language. It's a lot of fun to code, and it's incredibly powerful. I can see its appeal in the commercial world.

But I've been made to feel like the village idiot just because I asked some of the Jedi's here a question or two.

It's going to take a lot more than this to discourage me from trying to learn. And some day, maybe I'll be able to come onto this forum and actually provide something useful to someone else just starting out.

IronJacka at 2007-7-21 10:14:58 > top of Java-index,Desktop,Core GUI APIs...
# 34
Oh, by the way, Obi-Wan. You know that code that absolutely, positively COULDN'T work? I just got it working.Thanks for all your help guys. I don't know what I would have done without you.Oh, wait. Yes I do.
IronJacka at 2007-7-21 10:14:58 > top of Java-index,Desktop,Core GUI APIs...
# 35

> and the responses weren't particularly enlightening.

exactly the point.

you won't listen to anything anyone says, unless it 'fits' your code.

forget this thread, it's dead.

convert your current program so that it reads a handful of cutomers/items from text files

i.e. forget the db stuff for the moment - it makes it almost impossible for us to

copy/paste/compile/run and see the problem.

then start a new thread, and post any problems you have in getting the program

to do what you want.

if you are prepared to listen to constructive criticism, you'll find plenty of help

available.

Michael_Dunna at 2007-7-21 10:14:58 > top of Java-index,Desktop,Core GUI APIs...
# 36

Well, it's kind of little and kind of late, but I appreciate the gesture.

Fact is, I got the database read to work, and I can use the database fields to populate the screen.

I did just what I said I was going to do: moved the Listener into an inner class in the MasterScreen, then coded getters in an SQL class to read the row and return the fields to that class. Then I called the panels' setters to put the data on the screen.

I need to move on to the much more complicated issue of the Item fields, which are in an array that will require much the same construct.

Then, once that's done, I have to learn how to do INSERTS, UPDATES, and whatever passes for a DELETE in the database world. And I have to pretty up the GUI. Then I'll start worrying about all this threading and stuff.

IronJacka at 2007-7-21 10:14:58 > top of Java-index,Desktop,Core GUI APIs...
# 37
For anyone who might still be interested, I also got the Item matrix working. It now hits a data table and returns prices and descriptions for each item entered.Man, this language can be FUN!... as long as it's working ...
IronJacka at 2007-7-21 10:14:58 > top of Java-index,Desktop,Core GUI APIs...