Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:27:19+00:00 2026-05-30T02:27:19+00:00

here are my classes, as it stands the text area only gets the last

  • 0

here are my classes, as it stands the text area only gets the last object in the array. I need the JTextArea to display all the objects in the array. I have fixed the array problem,however, I cannot figure out how to make my button retrieve the array. I would like to implement an actionlistener in my driver class but I am getting static errors and java would like me to make stuff final but that never fixes the problem. I have updated to code to communicate my problem.

public class drink
{
private String name;
private String carb;
private String desc;

public drink(String drinkName, String carbDetail, String descDetail)
{
    name = drinkName;
    carb = carbDetail;
    desc = descDetail;

}
public String toString()
{
    return(name + carb + desc);
}

}


public class drinkViewer
{
private String string;
private String name;
private drink[] theDrinks;
private int drinkCount=0;



public drinkViewer(String Name)
{
    name = Name;
    theDrinks = new drink[5];

}
public void addDrink(String drinkName, String carbDetail, String descDetail)
{
    theDrinks[drinkCount] = new drink(drinkName, carbDetail, descDetail);
    drinkCount++;

} 
public void getdrinkArray()
{
    for(int i=0;i<drinkCount;i++)
        {
           string += (theDrinks[i].toString()+"\n");
            //System.out.println(string);   
        }
}


}//end

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JTextArea;

public class driver
{
private JTextArea area;


public static void main(String args[])
{
    drinkViewer newViewer = new drinkViewer("test");
    newViewer.addDrink("orange", "fruit", "noncarb");
    newViewer.addDrink("beer", "5%","carb");
    newViewer.loop();
    //JTextArea area = new JTextArea(newViewer.getString());
    JFrame frame = new JFrame("Drink Descriptions");
    frame.setVisible(true);
    frame.setSize(400,400);
    JButton button = new JButton("Button");
    JPanel panel = new JPanel();
    frame.add(panel,BorderLayout.NORTH);
    panel.add(button,BorderLayout.NORTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel.add(area,BorderLayout.SOUTH);

      class ButtonListener implements ActionListener
     {
         public void actionPerformed (ActionEvent event)
        {
            JTextArea area = new JTextArea(newViewer.getdrinkArray());
        }
     }

     listener = new ButtonListener();

      }

      }//end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:27:21+00:00Added an answer on May 30, 2026 at 2:27 am

    Change this line in loop() method string = (theDrinks[i].toString()+"\n");

    to string += (theDrinks[i].toString()+"\n");

    And also initialize your string variable inside drinkViewer constructor with string = ""; Otherwise you will see null... in your output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to export big amount of data from database. Here is classes that
I have a problem with this code. You can find all classes here .
I have 4 persistent classes which all have the same fields (exactly) the only
Having used optional parameters in a few classes here and there, I'm starting to
Here are my classes: http://pastebin.com/3dc5Vb1t When I try to run BookStore b = new
I have a question, here are two classes below: class Base{ public: virtual void
I write a simple OLAP viewer for my web-site. Here are the classes (abstract
I have the following script $(function(){ $('.input1_class, .input2_class, <many other input classes go here>').bind('change',
Here is the two classes that I'm use to express a bidirectional OneToMany relationship.
Here's the domain classes I'm working with: class Foo { String name, type static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.