Make a simple 4 row by 4 column scratch pad.

  • Follow


Hi All,
I am using Windows XP and jdk1.6.0_16
On a JPanel, I want to make a simple 4 rows by 4
columns: a grid/ "scratch pad"  that I can list two digit
numbers i.e. 10,12,...45..  one number each of the 16 cells.
I have tried a TextArea to do that. But using tabs is
a problem, to make it appear to be rows, columns.
Yet in a further search for a suitable demo,
<http://forums.sun.com/thread.jspa?threadID=226322&forumID=57>
this demo was written to demonstrate a TextPane. But while
testing/reading there was coding error of
"int tabWidth = charWidth charactersPerTab;"
In reading for further discovery the
<http://java.sun.com/javase/6/docs/api/java/awt/FontMetrics.html>
This read:
"Note that the implementations of these methods are
inefficient, so they are usually overridden with more
efficient toolkit-specific implementations."

I am looking for further help with this problem
Your help is appreciated.
bH
0
Reply bH 2/25/2010 4:18:58 PM

bH wrote:
> On a JPanel, I want to make a simple 4 rows by 4
> columns: a grid/ "scratch pad"  that I can list two digit
> numbers i.e. 10,12,...45..  one number each of the 16 cells.
> I have tried a TextArea to do that. But using tabs is
> a problem, to make it appear to be rows, columns.
> Yet in a further search for a suitable demo,
> <http://forums.sun.com/thread.jspa?threadID=226322&forumID=57>
> this demo was written to demonstrate a TextPane. But while
> testing/reading there was coding error of
> "int tabWidth = charWidth charactersPerTab;"
> In reading for further discovery the
> <http://java.sun.com/javase/6/docs/api/java/awt/FontMetrics.html>
> This read:
> "Note that the implementations of these methods are
> inefficient, so they are usually overridden with more
> efficient toolkit-specific implementations."

Perhaps instead of a 'TextArea' (I wonder why you didn't use a Swing component 
there - it seems really weird to mix Swing and AWT components), use
<http://java.sun.com/javase/6/docs/api/javax/swing/JTable.html>

-- 
Lew
0
Reply Lew 2/25/2010 5:09:58 PM


On 2/25/2010 8:18 AM, bH wrote:
> Hi All,
> I am using Windows XP and jdk1.6.0_16
> On a JPanel, I want to make a simple 4 rows by 4
> columns: a grid/ "scratch pad"  that I can list two digit
> numbers i.e. 10,12,...45..  one number each of the 16 cells.
> I have tried a TextArea to do that. But using tabs is
> a problem, to make it appear to be rows, columns.
> Yet in a further search for a suitable demo,
> <http://forums.sun.com/thread.jspa?threadID=226322&forumID=57>
> this demo was written to demonstrate a TextPane. But while
> testing/reading there was coding error of
> "int tabWidth = charWidth charactersPerTab;"

This line of the example is missing the * to multiply the character 
width by the number of characters per tab.

> In reading for further discovery the
> <http://java.sun.com/javase/6/docs/api/java/awt/FontMetrics.html>
> This read:
> "Note that the implementations of these methods are
> inefficient, so they are usually overridden with more
> efficient toolkit-specific implementations."
>
> I am looking for further help with this problem
> Your help is appreciated.
> bH

I'm not sure I would tackle the problem this way.  I would probably just 
use a grid of JLabels and post my numbers in those.  Or I would draw 
them directly onto a JPanel if I needed more complicated formatting.

-- 

Knute Johnson
email s/nospam/knute2010/

0
Reply Knute 2/25/2010 5:17:48 PM

In article <0hyhn.32168$e%2.5225@newsfe08.iad>,
 Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:

> On 2/25/2010 8:18 AM, bH wrote:
> > Hi All,
> > I am using Windows XP and jdk1.6.0_16
> > On a JPanel, I want to make a simple 4 rows by 4
> > columns: a grid/ "scratch pad"  that I can list two digit
> > numbers i.e. 10,12,...45..  one number each of the 16 cells.
> > I have tried a TextArea to do that. But using tabs is
> > a problem, to make it appear to be rows, columns.
> > Yet in a further search for a suitable demo,
> > <http://forums.sun.com/thread.jspa?threadID=226322&forumID=57>
> > this demo was written to demonstrate a TextPane. But while
> > testing/reading there was coding error of
> > "int tabWidth = charWidth charactersPerTab;"
> 
> This line of the example is missing the * to multiply the character 
> width by the number of characters per tab.
> 
> > In reading for further discovery the
> > <http://java.sun.com/javase/6/docs/api/java/awt/FontMetrics.html>
> > This read:
> > "Note that the implementations of these methods are
> > inefficient, so they are usually overridden with more
> > efficient toolkit-specific implementations."
> >
> > I am looking for further help with this problem
> > Your help is appreciated.
> 
> I'm not sure I would tackle the problem this way.  I would probably just 
> use a grid of JLabels and post my numbers in those.  Or I would draw 
> them directly onto a JPanel if I needed more complicated formatting.


bH: Knute & Lew are both right. A grid of JComponent subclasses works 
well. This example uses JToggleButton:

<http://sites.google.com/site/drjohnbmatthews/buttons>

Additionally, you might look at this example that uses custom drawing to 
scale the number in a grid of JButton instances:

<http://sites.google.com/site/trashgod/celltest>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
0
Reply John 2/25/2010 6:06:13 PM

On Feb 25, 1:06=A0pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article <0hyhn.32168$e%2.5...@newsfe08.iad>,
> =A0Knute Johnson <nos...@rabbitbrush.frazmtn.com> wrote:
>
>
>
>
>
> > On 2/25/2010 8:18 AM, bH wrote:
> > > Hi All,
> > > I am using Windows XP and jdk1.6.0_16
> > > On a JPanel, I want to make a simple 4 rows by 4
> > > columns: a grid/ "scratch pad" =A0that I can list two digit
> > > numbers i.e. 10,12,...45.. =A0one number each of the 16 cells.
> > > I have tried a TextArea to do that. But using tabs is
> > > a problem, to make it appear to be rows, columns.
> > > Yet in a further search for a suitable demo,
> > > <http://forums.sun.com/thread.jspa?threadID=3D226322&forumID=3D57>
> > > this demo was written to demonstrate a TextPane. But while
> > > testing/reading there was coding error of
> > > "int tabWidth =3D charWidth charactersPerTab;"
>
> > This line of the example is missing the * to multiply the character
> > width by the number of characters per tab.
>
> > > In reading for further discovery the
> > > <http://java.sun.com/javase/6/docs/api/java/awt/FontMetrics.html>
> > > This read:
> > > "Note that the implementations of these methods are
> > > inefficient, so they are usually overridden with more
> > > efficient toolkit-specific implementations."
>
> > > I am looking for further help with this problem
> > > Your help is appreciated.
>
> > I'm not sure I would tackle the problem this way. =A0I would probably j=
ust
> > use a grid of JLabels and post my numbers in those. =A0Or I would draw
> > them directly onto a JPanel if I needed more complicated formatting.
>
> bH: Knute & Lew are both right. A grid of JComponent subclasses works
> well. This example uses JToggleButton:
>
> <http://sites.google.com/site/drjohnbmatthews/buttons>
>
> Additionally, you might look at this example that uses custom drawing to
> scale the number in a grid of JButton instances:
>
> <http://sites.google.com/site/trashgod/celltest>
>
> --
> John B. Matthews
> trashgod at gmail dot com
> <http://sites.google.com/site/drjohnbmatthews>- Hide quoted text -
>
> - Show quoted text -

Hi All,
Thanks for your help and suggestions.
Lew: Your reminders are really great!
Nothing less than my sloppy thinking
not to have placed the "J" in front
of TextArea. Also see below for JTable
Knute: I want to be able to "write/edit"
numbers in a grid cell, while the program
is running. Its use will be a simple reminder
of what is happening so that I can refer
to it as I move through using the main program.
Hence, a desire for simple "scratch pad".
I was not aware that I could edit a
JLabel while it was actively running in the program.
I could trail each label in a 4 by 4 with a JTextField
for each cell.
As for a drawing of a grid I would have to
make some provision to make  "g.drawString(...)
available for each of the 16 blocks by entering
that number into a JTextField first? and
another JTextField  for its location
it into which cell of the 16.  Next a JButton to click to
set the number in place on the drawing.
John: Beautiful looking examples but I can't see
my way of using them, not yet anyway.

I looked at Lew's comment again and went to
the JTable tutorial.
I think that this might do it for the features explained
how to edit while main program is running.
<http://java.sun.com/docs/books/tutorial/uiswing/components/
table.html#simple>
But I was hoping for something simple than what is
in the link above.
I know, I probably should draw 10 lines on a piece using
paper and pencil, making a four by four grid,
fill in the blocks with my numbers.  I did that
before this present adventure.
Thanks again to all of you,
bH
0
Reply bH 2/25/2010 8:00:59 PM

On Thu, 25 Feb 2010 08:18:58 -0800 (PST), bH <bherbst65@hotmail.com>
wrote, quoted or indirectly quoted someone who said :

>I am using Windows XP and jdk1.6.0_16
>On a JPanel, I want to make a simple 4 rows by 4
>columns: a grid/ "scratch pad"  that I can list two digit
>numbers i.e. 10,12,...45..  one number each of the 16 cells.

There several ways you could skin that cat.

1, low level. Use a Canvas or JPanel. Use paintComponent and
drawString This is how you would do it if you had 64 x 64.
see http://mindprod.com/jgloss/paintcomponent.html


2. GridBag of JLabel[][] or JTextField[][] see
http://mindprod.com/jgloss/layout.html

3. JTable see http://mindprod.com/jgloss/jtable.html


-- 
Roedy Green Canadian Mind Products
http://mindprod.com

The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong it usually turns out to be impossible to get at or repair. 
~ Douglas Adams (born: 1952-03-11 died: 2001-05-11 at age: 49)
0
Reply Roedy 2/26/2010 12:56:34 AM

In article 
<eb326c81-48d8-4e2c-9420-20b92402c135@d27g2000yqf.googlegroups.com>,
 bH <bherbst65@hotmail.com> wrote:

> I looked at Lew's comment again and went to the JTable tutorial. I 
> think that this might do it for the features explained how to edit 
> while main program is running. ... But I was hoping for something 
> simple than what is in the link above.

Yes, I think you need at least a renderer and editor to make it usable:

<http://java.sun.com/docs/books/tutorial/uiswing/components/table.html>

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.text.DecimalFormat;
import java.util.Arrays;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

public class TableGrid extends JPanel {

    private DecimalFormat df = new DecimalFormat("#0.00");
    private JTextField tf = new JTextField();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("TableGrid");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.add(new TableGrid());
                f.pack();
                f.setVisible(true);
            }
        });
    }

    public TableGrid() {
        TableModel dataModel = new TableModel();
        JTable table = new JTable(dataModel);
        table.setCellSelectionEnabled(true);
        table.setRowHeight(32);
        table.setDefaultRenderer(Double.class, new DecRenderer(df));
        table.setDefaultEditor(Double.class, new DecEditor(tf, df));
        this.add(table);
    }

    private static class TableModel extends AbstractTableModel {

        private static final int SIZE = 4;
        private Double[][] matrix = new Double[SIZE][SIZE];

        public TableModel() {
            for (Object[] row : matrix) {
                Arrays.fill(row, Double.valueOf(0));
            }
        }

        @Override
        public int getRowCount() {
            return SIZE;
        }

        @Override
        public int getColumnCount() {
            return SIZE;
        }

        @Override
        public Object getValueAt(int row, int col) {
            return matrix[row][col];
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            matrix[row][col] = (Double) value;
        }

        @Override
        public Class<?> getColumnClass(int col) {
            return Double.class;
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            return true;
        }
    }

    private static class DecRenderer extends DefaultTableCellRenderer {

        DecimalFormat df;

        public DecRenderer(DecimalFormat df) {
            this.df = df;
            this.setHorizontalAlignment(JLabel.CENTER);
            this.setBackground(Color.lightGray);
        }

        @Override
        protected void setValue(Object value) {
            setText((value == null) ? "" : df.format(value));
        }
    }

    private static class DecEditor extends DefaultCellEditor {

        private JTextField tf;
        private DecimalFormat df;

        public DecEditor(JTextField tf, DecimalFormat df) {
            super(tf);
            this.tf = tf;
            this.df = df;
            tf.setHorizontalAlignment(JTextField.CENTER);
        }

        @Override
        public Object getCellEditorValue() {
            try {
                return new Double(tf.getText());
            } catch (NumberFormatException e) {
                return Double.valueOf(0);
            }
        }

        @Override
        public Component getTableCellEditorComponent(JTable table,
            Object value, boolean isSelected, int row, int column) {
            tf.setText((value == null) ? "" : df.format((Double) value));
            if (isSelected) tf.selectAll();
            return tf;
        }
    }
}

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
0
Reply John 2/26/2010 2:59:59 AM

On Feb 25, 9:59=A0pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <eb326c81-48d8-4e2c-9420-20b92402c...@d27g2000yqf.googlegroups.com>,
>
> =A0bH <bherbs...@hotmail.com> wrote:
> > I looked at Lew's comment again and went to the JTable tutorial. I
> > think that this might do it for the features explained how to edit
> > while main program is running. ... But I was hoping for something
> > simple than what is in the link above.
>
> Yes, I think you need at least a renderer and editor to make it usable:
>
> <http://java.sun.com/docs/books/tutorial/uiswing/components/table.html>
>
> import java.awt.Color;
> import java.awt.Component;
> import java.awt.EventQueue;
> import java.text.DecimalFormat;
> import java.util.Arrays;
> import javax.swing.DefaultCellEditor;
> import javax.swing.JFrame;
> import javax.swing.JLabel;
> import javax.swing.JPanel;
> import javax.swing.JTable;
> import javax.swing.JTextField;
> import javax.swing.table.AbstractTableModel;
> import javax.swing.table.DefaultTableCellRenderer;
>
> public class TableGrid extends JPanel {
>
> =A0 =A0 private DecimalFormat df =3D new DecimalFormat("#0.00");
> =A0 =A0 private JTextField tf =3D new JTextField();
>
> =A0 =A0 public static void main(String[] args) {
> =A0 =A0 =A0 =A0 EventQueue.invokeLater(new Runnable() {
>
> =A0 =A0 =A0 =A0 =A0 =A0 @Override
> =A0 =A0 =A0 =A0 =A0 =A0 public void run() {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 JFrame f =3D new JFrame("TableGrid");
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 f.setDefaultCloseOperation(JFrame.DISPOSE=
_ON_CLOSE);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 f.add(new TableGrid());
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 f.pack();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 f.setVisible(true);
> =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 });
> =A0 =A0 }
>
> =A0 =A0 public TableGrid() {
> =A0 =A0 =A0 =A0 TableModel dataModel =3D new TableModel();
> =A0 =A0 =A0 =A0 JTable table =3D new JTable(dataModel);
> =A0 =A0 =A0 =A0 table.setCellSelectionEnabled(true);
> =A0 =A0 =A0 =A0 table.setRowHeight(32);
> =A0 =A0 =A0 =A0 table.setDefaultRenderer(Double.class, new DecRenderer(df=
));
> =A0 =A0 =A0 =A0 table.setDefaultEditor(Double.class, new DecEditor(tf, df=
));
> =A0 =A0 =A0 =A0 this.add(table);
> =A0 =A0 }
>
> =A0 =A0 private static class TableModel extends AbstractTableModel {
>
> =A0 =A0 =A0 =A0 private static final int SIZE =3D 4;
> =A0 =A0 =A0 =A0 private Double[][] matrix =3D new Double[SIZE][SIZE];
>
> =A0 =A0 =A0 =A0 public TableModel() {
> =A0 =A0 =A0 =A0 =A0 =A0 for (Object[] row : matrix) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Arrays.fill(row, Double.valueOf(0));
> =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 @Override
> =A0 =A0 =A0 =A0 public int getRowCount() {
> =A0 =A0 =A0 =A0 =A0 =A0 return SIZE;
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 @Override
> =A0 =A0 =A0 =A0 public int getColumnCount() {
> =A0 =A0 =A0 =A0 =A0 =A0 return SIZE;
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 @Override
> =A0 =A0 =A0 =A0 public Object getValueAt(int row, int col) {
> =A0 =A0 =A0 =A0 =A0 =A0 return matrix[row][col];
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 @Override
> =A0 =A0 =A0 =A0 public void setValueAt(Object value, int row, int col) {
> =A0 =A0 =A0 =A0 =A0 =A0 matrix[row][col] =3D (Double) value;
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 @Override
> =A0 =A0 =A0 =A0 public Class<?> getColumnClass(int col) {
> =A0 =A0 =A0 =A0 =A0 =A0 return Double.class;
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 @Override
> =A0 =A0 =A0 =A0 public boolean isCellEditable(int row, int col) {
> =A0 =A0 =A0 =A0 =A0 =A0 return true;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 }
>
> =A0 =A0 private static class DecRenderer extends DefaultTableCellRenderer=
 {
>
> =A0 =A0 =A0 =A0 DecimalFormat df;
>
> =A0 =A0 =A0 =A0 public DecRenderer(DecimalFormat df) {
> =A0 =A0 =A0 =A0 =A0 =A0 this.df =3D df;
> =A0 =A0 =A0 =A0 =A0 =A0 this.setHorizontalAlignment(JLabel.CENTER);
> =A0 =A0 =A0 =A0 =A0 =A0 this.setBackground(Color.lightGray);
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 @Override
> =A0 =A0 =A0 =A0 protected void setValue(Object value) {
> =A0 =A0 =A0 =A0 =A0 =A0 setText((value =3D=3D null) ? "" : df.format(valu=
e));
> =A0 =A0 =A0 =A0 }
> =A0 =A0 }
>
> =A0 =A0 private static class DecEditor extends DefaultCellEditor {
>
> =A0 =A0 =A0 =A0 private JTextField tf;
> =A0 =A0 =A0 =A0 private DecimalFormat df;
>
> =A0 =A0 =A0 =A0 public DecEditor(JTextField tf, DecimalFormat df) {
> =A0 =A0 =A0 =A0 =A0 =A0 super(tf);
> =A0 =A0 =A0 =A0 =A0 =A0 this.tf =3D tf;
> =A0 =A0 =A0 =A0 =A0 =A0 this.df =3D df;
> =A0 =A0 =A0 =A0 =A0 =A0 tf.setHorizontalAlignment(JTextField.CENTER);
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 @Override
> =A0 =A0 =A0 =A0 public Object getCellEditorValue() {
> =A0 =A0 =A0 =A0 =A0 =A0 try {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return new Double(tf.getText());
> =A0 =A0 =A0 =A0 =A0 =A0 } catch (NumberFormatException e) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return Double.valueOf(0);
> =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 @Override
> =A0 =A0 =A0 =A0 public Component getTableCellEditorComponent(JTable table=
,
> =A0 =A0 =A0 =A0 =A0 =A0 Object value, boolean isSelected, int row, int co=
lumn) {
> =A0 =A0 =A0 =A0 =A0 =A0 tf.setText((value =3D=3D null) ? "" : df.format((=
Double) value));
> =A0 =A0 =A0 =A0 =A0 =A0 if (isSelected) tf.selectAll();
> =A0 =A0 =A0 =A0 =A0 =A0 return tf;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 }
>
> }
>
> --
> John B. Matthews
> trashgod at gmail dot com
> <http://sites.google.com/site/drjohnbmatthews>

Hi John,
A really big thank you for your demo.
I am impressed by the coloring
on the cell chosen and the grid's 4 by 4 appearance.
With a very few modifications this scratch pad
is complete.
Making comment about my earlier questions
and answers that you helped me with, that is,
making sub images out of a larger image.
<http://groups.google.com/group/comp.lang.java.help/browse_thread/
thread/3eb3b7a6599b23ab# >
I have removed the control of the fixed grid.
<http://groups.google.com/group/comp.lang.java.help/browse_thread/
thread/76f86e4cfb54f6c6#>
I now move separate but shuffled sub images to
recombine into the original image.
It is highly graphical specific. Image movement is
not with a mouse but controls using
input from a JTextField(s) and a click on a custom drawing.
Your scratch pad helps me to keep track of
what numbered image to move when getting
to a stage of final alignment. There are 16
sub images.
Thanks again. I truly appreciate it.
bH
0
Reply bH 2/26/2010 4:17:33 AM

7 Replies
344 Views

(page loaded in 0.287 seconds)

Similiar Articles:













7/21/2012 1:52:45 AM


Reply: