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. - public String toString()
-
{
-
return ("Artist: " + artist +
-
" CD Name: " + name +
-
" Item Number: " + itemno +
-
" Price: $" +price +
-
" In Stock: " + nstock +
-
" Stock Value: $" + value +
-
" Restocking Fee: $" + restock +
-
" Total Inventory Value: $" + total);
-
}
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.
9 2462
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. - public String toString()
-
{
-
return ("Artist: " + artist +
-
" CD Name: " + name +
-
" Item Number: " + itemno +
-
" Price: $" +price +
-
" In Stock: " + nstock +
-
" Stock Value: $" + value +
-
" Restocking Fee: $" + restock +
-
" Total Inventory Value: $" + total);
-
}
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 -
public String toString()
-
{
-
return ("Artist: " + artist +
-
" CD Name: " + name + "\n" +
-
" Item Number: " + itemno +
-
" Price: $" +price + "\n" +
-
" In Stock: " + nstock +
-
" Stock Value: $" + value + "\n" +
-
" Restocking Fee: $" + restock +
-
" Total Inventory Value: $" + total);
-
}
-
Hope this will help you.
Thanks and regards,
madhoriya
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. - public String toString()
-
{
-
return ("Artist: " + artist +
-
" CD Name: " + name +
-
" Item Number: " + itemno +
-
" Price: $" +price +
-
" In Stock: " + nstock +
-
" Stock Value: $" + value +
-
" Restocking Fee: $" + restock +
-
" Total Inventory Value: $" + total);
-
}
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
Here is my entire class: Maybe this will help: - import java.util.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
import javax.swing.*;
-
import javax.swing.event.*;
-
import java.text.*;
-
import java.lang.*;
-
-
public class Inventory2 extends JFrame
-
{
-
private JLabel cdNameLabel; // name label
-
private JLabel artistLabel; // item number label
-
private JLabel nstockLabel; // units in stock label
-
private JLabel priceLabel; // price each label
-
private JLabel itemLabel; // item number label
-
private JTextField cdNameField; // name display
-
private JTextField artistField; // artist display
-
private JFormattedTextField nstockField; // units in stock display
-
private JFormattedTextField priceField; // price each display
-
private JTextField itemField; // item number display
-
private NumberFormat nstockFormat; // format field and parse numbers
-
private NumberFormat priceFormat; // format field and parse numbers
-
private JButton btnAdd; // first button
-
private JButton btnPrev; // previous button
-
private JButton btnNext; // next button
-
private JButton btnDel; // last button
-
private JPanel buttonJPanel; // JPanle to hold buttons
-
private JPanel fieldJPanel; // JPanel to hold labels and displays
-
private JPanel fontJPanel; // JPanel to display logo
-
private int currCD;
-
private double total = 0; // variable for total inventory
-
private JList Inventorylist; // JList to take place of old array
-
private DefaultListModel listModel;
-
private JScrollPane jScrollPanel;
-
-
public Inventory2() // create class and method to perform GUI build
-
{
-
initComponents();
-
}
-
-
private void initComponents()
-
{
-
// create label names
-
cdNameLabel = new JLabel("CD Name:");
-
artistLabel = new JLabel("Artist:");
-
nstockLabel = new JLabel("In Stock:");
-
priceLabel = new JLabel("Each Item Cost:$");
-
itemLabel = new JLabel("Item Number:");
-
-
-
// initial fields
-
cdNameField = new JTextField(25);
-
cdNameField.setEditable(true);
-
artistField = new JTextField(15);
-
artistField.setEditable(true);
-
nstockField = new JFormattedTextField(nstockFormat);
-
nstockField.setEditable(true);
-
nstockField.setColumns(5);
-
priceField = new JFormattedTextField(priceFormat);
-
priceField.setEditable(true);
-
priceField.setColumns(5);
-
itemField = new JTextField(4);
-
itemField.setEditable(true);
-
-
// JList
-
jScrollPanel = new JScrollPane();
-
Inventorylist = new JList();
-
currCD = 0;
-
-
-
// buttons
-
btnAdd = new JButton();
-
btnNext = new JButton();
-
btnPrev = new JButton();
-
btnDel = new JButton();
-
-
getContentPane().setLayout(new FlowLayout());
-
-
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
-
-
// place textFields and labels
-
-
//artist
-
artistLabel.setText("Artist");
-
getContentPane().add(artistLabel);
-
-
artistField.setMinimumSize(new Dimension(70,20));
-
artistField.setPreferredSize(new Dimension(70,20));
-
getContentPane().add(artistField);
-
-
// cd name
-
cdNameLabel.setText("CD Name");
-
getContentPane().add(cdNameLabel);
-
-
cdNameField.setMinimumSize(new Dimension(70,20));
-
cdNameField.setPreferredSize(new Dimension(70,20));
-
getContentPane().add(cdNameField);
-
-
// copies in stock
-
nstockLabel.setText("Copies In Stock");
-
getContentPane().add(nstockLabel);
-
-
nstockField.setMinimumSize(new Dimension(5,20));
-
nstockField.setPreferredSize(new Dimension(5,20));
-
getContentPane().add(nstockField);
-
-
//price of cd
-
priceLabel.setText("Price");
-
getContentPane().add(priceLabel);
-
-
priceField.setMinimumSize(new Dimension(20,20));
-
priceField.setPreferredSize(new Dimension(20,20));
-
getContentPane().add(priceField);
-
-
//item number of cd
-
itemLabel.setText("Item Number");
-
getContentPane().add(itemLabel);
-
-
itemField.setMinimumSize(new Dimension(5,20));
-
itemField.setPreferredSize(new Dimension(5,20));
-
getContentPane().add(itemField);
-
-
-
// add listeners
-
-
btnAdd.setText("Add");
-
btnAdd.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnAddActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnAdd);
-
-
// PREVIOUS
-
btnPrev.setText("Previous");
-
btnPrev.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnAddActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnPrev);
-
-
// NEXT
-
btnNext.setText("Next"); btnNext.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnAddActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnNext);
-
-
// DELETE
-
btnDel.setText("Delete");
-
btnDel.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent evt)
-
{
-
btnAddActionPerformed(evt);
-
}
-
});
-
getContentPane().add(btnDel);
-
-
// new Jlist model
-
listModel = new DefaultListModel();
-
Inventorylist.setModel(listModel);
-
-
jScrollPanel.setViewportView(Inventorylist);
-
-
getContentPane().add(jScrollPanel);
-
-
pack();
-
}// close
-
-
-
private void btnAddActionPerformed(ActionEvent evt)
-
{
-
// Create cd to add
-
CdwArtist newCD = new CdwArtist();
-
newCD.setArtist(artistField.getText());
-
newCD.setName(cdNameField.getText());
-
newCD.setItemno(Integer.parseInt(itemField.getText()));
-
newCD.setNstock(Integer.parseInt(nstockField.getText()));
-
newCD.setPrice(Float.parseFloat(priceField.getText()));
-
-
// Add cd to list
-
-
-
listModel.addElement(newCD);
-
currCD = listModel.size()-1; // sets currCD to added index
-
-
// Clear the text fields after add
-
artistField.setText(null);
-
cdNameField.setText(null);
-
itemField.setText(null);
-
nstockField.setText(null);
-
priceField.setText(null);
-
-
}// end ADD
-
-
-
// run it
-
public static void main(String args[])
-
{
-
java.awt.EventQueue.invokeLater(new Runnable()
-
{
-
public void run()
-
{
-
new Inventory2().setVisible(true);
-
-
}
-
});
-
}
-
-
} // close class
Have you tried the suggestions given so far?
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.
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
Hi Jos, thanks for the reply.
I did try it, implimented as so: - return <html>("Artist: " + artist +
-
" CD Name: " + name +
-
" Item Number: " + itemno +
-
" Price: $" + formatter.format(getPrice()) +
-
" In Stock: " + nstock +
-
" Stock Value: $" + formatter.format(getValue()) +
-
" Restocking Fee: $" + restock +
-
" 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.
Hi Jos, thanks for the reply.
I did try it, implimented as so: - return <html>("Artist: " + artist +
-
" CD Name: " + name +
-
" Item Number: " + itemno +
-
" Price: $" + formatter.format(getPrice()) +
-
" In Stock: " + nstock +
-
" Stock Value: $" + formatter.format(getValue()) +
-
" Restocking Fee: $" + restock +
-
" 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: -
return "<html>Artist: " + artist +"<br>"+
-
" CD Name: " + name + "<br>"+
-
...
-
+"<html>";
-
kind regards,
Jos
That is neat. It works when I do it the correct way!
Thanks for the tip guys.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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(),...
|
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...
|
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
...
|
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;
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |