1

Im making a program that outputs, the numbers 1-10 in one column, the square of this in another and in the third the number in cube.

How can i make the program a little nicer so the columns really differ. And for every column i like to add a Title(Number, number squared, number cube).

//Sorry for my bad English

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Del2upp3 extends JFrame implements ActionListener 
{
    int i;
    JLabel label2 = new JLabel();
    JPanel panel = new JPanel();
    JButton button = new JButton("Press");
    Del2upp3()
    {
        super ("Panel"); setSize (200,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container con = this.getContentPane();
        con.add(panel);
        button.addActionListener(this);
        panel.add(label2);
        panel.add(button);
        setVisible(true);

    } 
    public void actionPerformed(ActionEvent event)
    {        
            Object source = event.getSource();
            if (source == button)  
        {

            StringBuilder string = new StringBuilder();
                for( i = 1; i< 11; i++){

                string.append("\n     ").append(i);
                string.append(", ").append(i*i);
                string.append(", ").append(i*i*i);

        }
               JOptionPane.showMessageDialog(null, string,"Data",
               JOptionPane.PLAIN_MESSAGE);
               setSize (200,200);
               setVisible(true); 
                 i = 0;
         }}
        public static void main(String[] args){new Del2upp3();} 
   }
  • 2
    For a Swing GUI, it would be best to display the results in a JTable. You could put the results in a 2D Integer array and then use that array as part of your JTable's model. – Hovercraft Full Of Eels May 08 '13 at 21:15
  • yes - the JTable is a more proper GUI structure - not sure if OP is ready for more Swing or just looking to print some simple stuff.. – Randy May 08 '13 at 21:19

2 Answers2

4

It's better to use JTable in your case.

But you can use HTML Tags and do something like this:

builder.append("<html><table border=1><tr><td>column1</td><td>column2</td><td></tr>");
string.append("<tr><td>");
string.append(.....);
string.append("</td><td>");
...
string.append("</td></tr>");
string.append("</table></html>");

Or:

textArea.setText("column1\t\tcolumn2\n");  
textArea.append("column1\t\tcolumn2\n");  
textArea.append(...);  

But it's not recommended since your text can go out of the column.

Maroun
  • 94,125
  • 30
  • 188
  • 241
3

Formatting a String using HTML is an excellent and often overlooked idea (+1 to Maroun)

Something else that often gets over looked is the ability for JOptionPane to use JComponents

enter image description here

import java.awt.EventQueue;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TableOptionPane {

    public static void main(String[] args) {
        new TableOptionPane();
    }

    public TableOptionPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Integer[][] values = new Integer[10][3];
                StringBuilder string = new StringBuilder();
                for (int i = 1; i < 11; i++) {
                    values[i - 1][0] = i;
                    values[i - 1][1] = i * i;
                    values[i - 1][2] = i * i * i;
                }
                DefaultTableModel model = new DefaultTableModel(values, new Object[]{"i", "i * i", "i * i * i"});
                JTable table = new JTable(model);
                JOptionPane.showMessageDialog(null, new JScrollPane(table), "Data", JOptionPane.PLAIN_MESSAGE);
            }
        });
    }
}

The has the added benifit of providing a bases from which you could support such operations as "copy" or even edibility of the data, if such things were of use to you...

You may find more details at How to use Dialogs

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366