• Create Account

## some help connecting different components in java's SWING framework.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Enerjak  Members

277
Like
0Likes
Like

Posted 20 October 2012 - 03:06 PM

Ok, this is something that's been bothering me for awhile now. I'm trying to use swing to make a hardware store program. I'm using swing and i got a GUI all designed with multiple forms and everything but I can't seem to connect them together....Like I had a button that says "add item", clicking on it brings up the frame to add a new item but when it's filled it doesn't do crap......the code is too long to post in thread so i'm going to upload everything into a zip file and just tell you where to look. The problem seems to be in:

@Override
public void actionPerformed(ActionEvent arg0)
{
if(arg0.getSource() == this.OpenMI)
{
try
{
{
this.printData();
}
}
catch(IOException e)
{

}
}

if(arg0.getSource() == this.newItemB)
{
newItem.setVisible(true);
stock = new HardwareStock(40);
newItem.setStore(this);

this.HardwareDataTA.setText(stock.elementAt(1).toString());

}
}
}


(the newM if statement works).

either the problem is in the AddItem's actionHandler or something on my part......I don't know.

### #2rip-off  Moderators

10730
Like
0Likes
Like

Posted 20 October 2012 - 04:30 PM

What have you tried?

I don't know what "newM if statement" you are referring to.

The first thing I notice is that you have a point in your code that silently swallows exceptions. Never, ever do this. At the very least, print something. In an interactive GUI application, you should inform the user there is a problem with the file.

Another thing I notice is that the logic for creating a new item doesn't appear to save the idem anywhere - though possibly the call to setStore() is doing something funky. You seem to be creating a new stock object, and then setting the text to the second (first?) element, which may not be populated.

It is idiomatic in Swing to attach the event listeners directly to the component they are listening too. This is often done via an anonymous inner class. Doing it this way means that you don't have to determine the source of an event - it is implied.

For example:
// In the setup method:

@Override
public void actionPerformed(ActionEvent arg0)
{
try
{
{
this.printData();
}
}
catch(IOException e)
{
// TODO: improve me!
e.printStackTrace();
}
}
});

@Override
public void actionPerformed(ActionEvent arg0)
{
newItem.setVisible(true);
stock = new HardwareStock(40);
newItem.setStore(this);
this.HardwareDataTA.setText(stock.elementAt(1).toString());
}
});

Moving the logic performed into a separate method or class is common, otherwise the method creating these inner classes starts getting very long:
this.OpenMI.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0)
{
handleOpenMI();
}
});

@Override
public void actionPerformed(ActionEvent arg0)
{
handleNewItemB();
}
});


### #3ppgamedev  Members

311
Like
0Likes
Like

Posted 20 October 2012 - 04:44 PM

I'm really sorry cos I'd like to say it in a different way but I can't: that code is a BIG pile of SHITE.

Try reading a book like "Clean Code" by Robert C. Martin or "The Art of Readable Code" by Boswell & Foucher.

Anyway, the new item is inserted. It can be checked with the code in HardwareStock class:
[source lang="java"]public void insertEnd(HardwareItem item) { if (numOfElements < m_items.length) { m_items[numOfElements++] = item; System.out.println("Item " + item.getItemName() + " added"); } else { System.out.println("Cannot add to full list"); }}[/source]
that is called from HWStore class:
[source lang="java"]public void addItem(HardwareItem i) { stock.insertEnd(i);}[/source]

### #4Enerjak  Members

277
Like
0Likes
Like

Posted 20 October 2012 - 04:46 PM

the "newM" is a menu item to open the file dialog to get the file to load the data and print it. as you can see i create the stock after i load the file, but i suppose i could add a resize method for easy resizing.... like create a stock of 5 items but then resize it when necessary. Hmm,

### #5Enerjak  Members

277
Like
0Likes
Like

Posted 20 October 2012 - 04:49 PM

I'm really sorry cos I'd like to say it in a different way but I can't: that code is a BIG pile of SHITE.

Try reading a book like "Clean Code" by Robert C. Martin or "The Art of Readable Code" by Boswell & Foucher.

Anyway, the new item is inserted. It can be checked with the code in HardwareStock class:
[source lang="java"]public void insertEnd(HardwareItem item) { if (numOfElements < m_items.length) { m_items[numOfElements++] = item; System.out.println("Item " + item.getItemName() + " added"); } else { System.out.println("Cannot add to full list"); }}[/source]
that is called from HWStore class:
[source lang="java"]public void addItem(HardwareItem i) { stock.insertEnd(i);}[/source]

hey man, it's ok, i mostly write like that because MOST of the time i can understand it but yea i guess it's shit and not very professional.......I'm just used to doing this a certain way i guess. guess i need to change if i want to make it

### #6Enerjak  Members

277
Like
0Likes
Like

Posted 20 October 2012 - 05:46 PM

What have you tried?

I don't know what "newM if statement" you are referring to.

The first thing I notice is that you have a point in your code that silently swallows exceptions. Never, ever do this. At the very least, print something. In an interactive GUI application, you should inform the user there is a problem with the file.

Another thing I notice is that the logic for creating a new item doesn't appear to save the idem anywhere - though possibly the call to setStore() is doing something funky. You seem to be creating a new stock object, and then setting the text to the second (first?) element, which may not be populated.

It is idiomatic in Swing to attach the event listeners directly to the component they are listening too. This is often done via an anonymous inner class. Doing it this way means that you don't have to determine the source of an event - it is implied.

For example:

// In the setup method:

@Override
public void actionPerformed(ActionEvent arg0)
{
try
{
{
this.printData();
}
}
catch(IOException e)
{
// TODO: improve me!
e.printStackTrace();
}
}
});

@Override
public void actionPerformed(ActionEvent arg0)
{
newItem.setVisible(true);
stock = new HardwareStock(40);
newItem.setStore(this);
this.HardwareDataTA.setText(stock.elementAt(1).toString());
}
});

Moving the logic performed into a separate method or class is common, otherwise the method creating these inner classes starts getting very long:
this.OpenMI.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0)
{
handleOpenMI();
}
});

@Override
public void actionPerformed(ActionEvent arg0)
{
handleNewItemB();
}
});