Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Convert working program to display in GUI

Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a working program that outputs the result, I now need to get the output to display in a GUI window.

I'm using a inv,printInventory(); and a inv.printTotalInventory(); to display the result.

// display the inventory on the screen

What I need help with is getting the results to display in a GUI (JTextField??) after pressing the CardCollecion button (JButton??). Any sugesstion to get me started would be greatly appreciated.

I guess I just need to understand how to connect the inv.printCardCollection(); to a button and display it in the text window, or would it make more sense to use a JTable

The output looks like this in my current program.

PlayerName CardBrand Year CardNumber Value
Todd Helton Topps 2005 123 $1
Barry Bonds Fleer 1998 23 $12

What I have created so far for my Java Window.


public class NewJPanel extends javax.swing.JPanel {

/** Creates new form NewJPanel */
public NewJPanel() {

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();

jTextField1.setText("Card Collcetion Results");

jButton1.setText("Card Collection");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(95, 95, 95)
.addContainerGap(19, Short.MAX_VALUE))
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)
.addContainerGap(35, Short.MAX_VALUE))

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
}// </editor-fold>

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
Posts: 74780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.
I am afraid I shall make one unwelcome suggestion: stop using the GUI builder on NetBeans until you understand the syntax of adding Components.
Go into the Swing part of the Java Tutorial. Find the bit about "how to use buttons" and about "how to use text fields" or text areas.

Just beware: the Java tutorials have a tendency to put lots in the main method, which is convenient for writing short examples but doesn't count as object-oriented programming. The format they use in this first example in the Java Tutorial, however, is excellent. Only you don't need to say "frame.getContentPane().add(xyz);" in Java 5 or Java 6, you just say "frame.add(xyz);".

The Java Tutorials also tend to use the syntax "addActionListener(this)" which I look on as an abomination. I wrote about it here, last year, and still think that is the correct way to do it. You might be able to persuade NetBeans to write an anonymous actionListener for you by filling in a blank template.

I hope you are able to work out what to do with your Swing app. All the best.

sam spadeco
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you I will look at the Java Tutorial on buttons.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic