473,395 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

toString as output. Is there a way to format?

38
I have a program whose output goes to a simple jpanel.
I am using a toString to get it there, but as it is it shows up as one super long string of data.
Expand|Select|Wrap|Line Numbers
  1.     public String toString()
  2.     {
  3.     return ("Artist: " + artist + 
  4.     " CD Name: " + name + 
  5.     " Item Number: " + itemno + 
  6.     " Price: $" +price + 
  7.     " In Stock: " + nstock + 
  8.     " Stock Value: $" + value + 
  9.     " Restocking Fee: $" + restock + 
  10.     " Total Inventory Value: $"  + total);
  11.     }    
Is there a way to enter newline commands, carriage returns, or anything that may break this up in to two or three lines of data for simple readability?
I can find nothing.
Jul 27 '07 #1
9 2462
madhoriya22
252 100+
I have a program whose output goes to a simple jpanel.
I am using a toString to get it there, but as it is it shows up as one super long string of data.
Expand|Select|Wrap|Line Numbers
  1.     public String toString()
  2.     {
  3.     return ("Artist: " + artist + 
  4.     " CD Name: " + name + 
  5.     " Item Number: " + itemno + 
  6.     " Price: $" +price + 
  7.     " In Stock: " + nstock + 
  8.     " Stock Value: $" + value + 
  9.     " Restocking Fee: $" + restock + 
  10.     " Total Inventory Value: $"  + total);
  11.     }    
Is there a way to enter newline commands, carriage returns, or anything that may break this up in to two or three lines of data for simple readability?
I can find nothing.
Hi,
Put a newline character at the place from where u want to break ur string like this
Expand|Select|Wrap|Line Numbers
  1. public String toString()
  2. {
  3.       return ("Artist: " + artist + 
  4.       " CD Name: " + name + "\n" +
  5.       " Item Number: " + itemno + 
  6.       " Price: $" +price + "\n" +
  7.       " In Stock: " + nstock + 
  8.       " Stock Value: $" + value + "\n" +
  9.       " Restocking Fee: $" + restock + 
  10.       " Total Inventory Value: $"  + total);
  11. }
  12.  
Hope this will help you.
Thanks and regards,
madhoriya
Jul 27 '07 #2
JosAH
11,448 Expert 8TB
I have a program whose output goes to a simple jpanel.
I am using a toString to get it there, but as it is it shows up as one super long string of data.
Expand|Select|Wrap|Line Numbers
  1.     public String toString()
  2.     {
  3.     return ("Artist: " + artist + 
  4.     " CD Name: " + name + 
  5.     " Item Number: " + itemno + 
  6.     " Price: $" +price + 
  7.     " In Stock: " + nstock + 
  8.     " Stock Value: $" + value + 
  9.     " Restocking Fee: $" + restock + 
  10.     " Total Inventory Value: $"  + total);
  11.     }    
Is there a way to enter newline commands, carriage returns, or anything that may break this up in to two or three lines of data for simple readability?
I can find nothing.
I guess you're putting that String in a JLabel, right? If so, JLabels also accept
html formatted Strings, e.g."<html>Hello<br>world</html>" will display two
lines of text.

kind regards,

Jos
Jul 27 '07 #3
no1zson
38
Here is my entire class: Maybe this will help:
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6. import java.text.*;
  7. import java.lang.*;
  8.  
  9. public class Inventory2 extends JFrame
  10. {
  11.     private JLabel cdNameLabel; // name label 
  12.     private JLabel artistLabel; // item number label 
  13.     private JLabel nstockLabel; // units in stock label 
  14.     private JLabel priceLabel; // price each label 
  15.     private JLabel itemLabel; // item number label 
  16.     private JTextField cdNameField; // name display 
  17.     private JTextField artistField; // artist display 
  18.     private JFormattedTextField nstockField; // units in stock display 
  19.     private JFormattedTextField priceField; // price each display 
  20.     private JTextField itemField; // item number display 
  21.     private NumberFormat nstockFormat; // format field and parse numbers 
  22.     private NumberFormat priceFormat; // format field and parse numbers 
  23.     private JButton btnAdd; // first button 
  24.     private JButton btnPrev; // previous button 
  25.     private JButton btnNext; // next button 
  26.     private JButton btnDel; // last button 
  27.     private JPanel buttonJPanel; // JPanle to hold buttons 
  28.     private JPanel fieldJPanel; // JPanel to hold labels and displays 
  29.     private JPanel fontJPanel; // JPanel to display logo 
  30.     private int currCD; 
  31.     private double total = 0; // variable for total inventory 
  32.     private JList Inventorylist; // JList to take place of old array
  33.     private DefaultListModel listModel;
  34.     private JScrollPane jScrollPanel;  
  35.  
  36.     public Inventory2() // create class and method to perform GUI build 
  37.     { 
  38.         initComponents(); 
  39.     } 
  40.  
  41.     private void initComponents() 
  42.     { 
  43.         // create label names 
  44.         cdNameLabel = new JLabel("CD Name:"); 
  45.         artistLabel = new JLabel("Artist:");
  46.         nstockLabel = new JLabel("In Stock:"); 
  47.         priceLabel = new JLabel("Each Item Cost:$"); 
  48.         itemLabel = new JLabel("Item Number:"); 
  49.  
  50.  
  51.         // initial fields
  52.         cdNameField = new JTextField(25);
  53.         cdNameField.setEditable(true);
  54.         artistField = new JTextField(15);
  55.         artistField.setEditable(true);
  56.         nstockField = new JFormattedTextField(nstockFormat);
  57.         nstockField.setEditable(true);
  58.         nstockField.setColumns(5);
  59.         priceField = new JFormattedTextField(priceFormat);
  60.         priceField.setEditable(true);
  61.         priceField.setColumns(5);
  62.         itemField = new JTextField(4);
  63.         itemField.setEditable(true);
  64.  
  65.         // JList
  66.         jScrollPanel = new JScrollPane();
  67.         Inventorylist = new JList(); 
  68.         currCD = 0;
  69.  
  70.  
  71.         // buttons 
  72.         btnAdd = new JButton(); 
  73.         btnNext = new JButton(); 
  74.         btnPrev = new JButton(); 
  75.         btnDel = new JButton(); 
  76.  
  77.         getContentPane().setLayout(new FlowLayout());
  78.  
  79.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  80.  
  81.         // place textFields and labels
  82.  
  83.         //artist
  84.         artistLabel.setText("Artist"); 
  85.         getContentPane().add(artistLabel); 
  86.  
  87.         artistField.setMinimumSize(new Dimension(70,20)); 
  88.         artistField.setPreferredSize(new Dimension(70,20));
  89.         getContentPane().add(artistField); 
  90.  
  91.         // cd name
  92.         cdNameLabel.setText("CD Name"); 
  93.         getContentPane().add(cdNameLabel); 
  94.  
  95.         cdNameField.setMinimumSize(new Dimension(70,20)); 
  96.         cdNameField.setPreferredSize(new Dimension(70,20));
  97.         getContentPane().add(cdNameField);
  98.  
  99.         // copies in stock
  100.         nstockLabel.setText("Copies In Stock"); 
  101.         getContentPane().add(nstockLabel); 
  102.  
  103.         nstockField.setMinimumSize(new Dimension(5,20)); 
  104.         nstockField.setPreferredSize(new Dimension(5,20));
  105.         getContentPane().add(nstockField); 
  106.  
  107.         //price of cd
  108.         priceLabel.setText("Price"); 
  109.         getContentPane().add(priceLabel); 
  110.  
  111.         priceField.setMinimumSize(new Dimension(20,20)); 
  112.         priceField.setPreferredSize(new Dimension(20,20));
  113.         getContentPane().add(priceField); 
  114.  
  115.         //item number of cd
  116.         itemLabel.setText("Item Number"); 
  117.         getContentPane().add(itemLabel); 
  118.  
  119.         itemField.setMinimumSize(new Dimension(5,20)); 
  120.         itemField.setPreferredSize(new Dimension(5,20));
  121.         getContentPane().add(itemField); 
  122.  
  123.  
  124.         // add listeners
  125.  
  126.         btnAdd.setText("Add");
  127.         btnAdd.addActionListener(new ActionListener()
  128.         {
  129.             public void actionPerformed(ActionEvent evt)
  130.             {
  131.                 btnAddActionPerformed(evt);
  132.             }
  133.         });
  134.         getContentPane().add(btnAdd);
  135.  
  136.         // PREVIOUS
  137.         btnPrev.setText("Previous");
  138.         btnPrev.addActionListener(new ActionListener()
  139.         {
  140.             public void actionPerformed(ActionEvent evt)
  141.             {
  142.                 btnAddActionPerformed(evt);
  143.             }
  144.         });
  145.         getContentPane().add(btnPrev);
  146.  
  147.         // NEXT
  148.         btnNext.setText("Next");        btnNext.addActionListener(new ActionListener()
  149.         {
  150.             public void actionPerformed(ActionEvent evt)
  151.             {
  152.                 btnAddActionPerformed(evt);
  153.             }
  154.         });
  155.         getContentPane().add(btnNext);
  156.  
  157.         // DELETE
  158.         btnDel.setText("Delete");
  159.         btnDel.addActionListener(new ActionListener()
  160.         {
  161.             public void actionPerformed(ActionEvent evt)
  162.             {
  163.                 btnAddActionPerformed(evt);
  164.             }
  165.         });
  166.         getContentPane().add(btnDel);
  167.  
  168.         // new Jlist model
  169.         listModel = new DefaultListModel();
  170.         Inventorylist.setModel(listModel);
  171.  
  172.         jScrollPanel.setViewportView(Inventorylist);
  173.  
  174.         getContentPane().add(jScrollPanel);
  175.  
  176.         pack();
  177.     }// close
  178.  
  179.  
  180.         private void btnAddActionPerformed(ActionEvent evt)
  181.         {
  182.             // Create cd to add
  183.             CdwArtist newCD = new CdwArtist();
  184.             newCD.setArtist(artistField.getText());
  185.             newCD.setName(cdNameField.getText());    
  186.             newCD.setItemno(Integer.parseInt(itemField.getText()));
  187.             newCD.setNstock(Integer.parseInt(nstockField.getText()));
  188.             newCD.setPrice(Float.parseFloat(priceField.getText()));
  189.  
  190.             // Add cd to list
  191.  
  192.  
  193.             listModel.addElement(newCD);
  194.             currCD = listModel.size()-1;  // sets currCD to added index
  195.  
  196.             // Clear the text fields after add
  197.             artistField.setText(null);
  198.             cdNameField.setText(null);    
  199.             itemField.setText(null);
  200.             nstockField.setText(null);
  201.          priceField.setText(null);
  202.  
  203.             }// end ADD
  204.  
  205.  
  206.         // run it
  207.         public static void main(String args[])
  208.         {
  209.         java.awt.EventQueue.invokeLater(new Runnable()
  210.             {
  211.             public void run()
  212.                 {
  213.                 new Inventory2().setVisible(true);
  214.  
  215.                 }
  216.             });
  217.         }
  218.  
  219. } // close class
Jul 27 '07 #4
r035198x
13,262 8TB
Have you tried the suggestions given so far?
Jul 27 '07 #5
no1zson
38
Yes. I tried both and sent them a "report" of the results.

Neither worked, I am not sure if I misdescribed my code or what I am trying to do so I posted the entire class.
Jul 27 '07 #6
JosAH
11,448 Expert 8TB
Yes. I tried both and sent them a "report" of the results.

Neither worked, I am not sure if I misdescribed my code or what I am trying to do so I posted the entire class.
Have you tried that little html trick I described?

kind regards,

Jos
Jul 27 '07 #7
no1zson
38
Hi Jos, thanks for the reply.

I did try it, implimented as so:
Expand|Select|Wrap|Line Numbers
  1. return <html>("Artist: " + artist +
  2.     " CD Name: " + name + 
  3.     " Item Number: " + itemno + 
  4.     " Price: $" + formatter.format(getPrice()) + 
  5.     " In Stock: " + nstock + 
  6.     " Stock Value: $" + formatter.format(getValue())  + 
  7.     " Restocking Fee: $" + restock + 
  8.     " Total Inventory Value: $"  + formatter.format(getTotal()));</html>
but all I get are "illegal start of expression errors. I put the tags inside the parenthsis, removed the parenthesis, tried all kinds of variations with them, but no matter where I put them I get errors.
Jul 27 '07 #8
JosAH
11,448 Expert 8TB
Hi Jos, thanks for the reply.

I did try it, implimented as so:
Expand|Select|Wrap|Line Numbers
  1. return <html>("Artist: " + artist +
  2.     " CD Name: " + name + 
  3.     " Item Number: " + itemno + 
  4.     " Price: $" + formatter.format(getPrice()) + 
  5.     " In Stock: " + nstock + 
  6.     " Stock Value: $" + formatter.format(getValue())  + 
  7.     " Restocking Fee: $" + restock + 
  8.     " Total Inventory Value: $"  + formatter.format(getTotal()));</html>
but all I get are "illegal start of expression errors. I put the tags inside the parenthsis, removed the parenthesis, tried all kinds of variations with them, but no matter where I put them I get errors.
Nonono, not like that; read my reply again. It should be something like this:

Expand|Select|Wrap|Line Numbers
  1. return "<html>Artist: " + artist +"<br>"+
  2. " CD Name: " + name + "<br>"+
  3. ...
  4. +"<html>";
  5.  
kind regards,

Jos
Jul 28 '07 #9
no1zson
38
That is neat. It works when I do it the correct way!
Thanks for the tip guys.
Jul 28 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
2
by: Rod Brick | last post by:
I'm trying to print a Double in straight decimal form, not exponential. I can't seem to accomplish this. This seems like it should be simple enough. The output I'm looking for is "0.00001", not...
0
by: Frank | last post by:
Hello, following problem: I have written a custom format provider MyFormatInfo (which implements IFormatProvider and ICustomProvider). When I write String.Format(new MyFormatInfo(),...
8
by: G.Ashok | last post by:
Hi, I have created CultureInfo object and specified required digit grouping in it. The one of the overloaded ToString methods of Decimal type has parameters to format the value with required...
7
by: mirandacascade | last post by:
O/S: Windows XP Home Vsn of Python: 2.4 Copy/paste of interactive window is immediately below; the text/questions toward the bottom of this post will refer to the content of the copy/paste ...
4
by: J Miro | last post by:
When I use ToString method to format the value of a nullable numeric variable, I get "No overload for method 'ToString' takes '1' arguments" error message. Example: Int32? myNum = 12345; ...
5
by: Nick Gilbert | last post by:
Hi, One of our servers is behaving differently to all the others when printing dates. They seem to be outputting in American format (12 hour) when the default for a UK machine should be UK...
11
by: Gary James | last post by:
I'm using an object data type variable to pass a numeric value (Int, Float, Double, etc) to a function that returns a formatted string. However, nullable types do not provide an overridden...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.