Thin borderlines for JText cells

Hi,

The following example makes a table based on JTextFields.

I would like to get thinner border lines as they are in a JTable.

Who knows how?

Thanks

Michel

package MTabProtoypes;

import java.awt.Color;

import java.awt.Container;

import java.awt.HeadlessException;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.BorderFactory;

import javax.swing.JFrame;

import javax.swing.JTextField;

/* @author scm */

publicclass TabJtextfieldsArray02extends JFrameimplements MouseListener{

private JTextField[][] txt;// View: 2D Array of textfields o=o 2D model array

private Container cp = getContentPane();

publicstaticvoid main(String[] args){

int totRows = 256;int totCols=64;int visRows = 16;int visCols=8;int x=10;int y=10;int h=20;int w=70;

TabJtextfieldsArray02 mTabTest =new TabJtextfieldsArray02(totRows,totCols,visRows,visCols,x,y,w,h);// Neue Table

mTabTest.setSize(600, 400);

mTabTest.setVisible(true );

}

public TabJtextfieldsArray02(int totRows,int totCols,int visRows,int visCols,int xpos,int ypos,int width,int height)throws HeadlessException{

//super ("Tabellen - TabJtextfieldsArray02");

cp.setLayout(null );

txt =new JTextField[totRows][totCols];// Table / Textfield-Array Params (rows/columns)

for (int i = 0; i < visRows; i++){appRow (i, visCols, xpos, ypos, width, height);}// append row by row

for (int i = 1; i <=visCols; i++){insText (1,i,"ColHead "+i);}// Index Not 0/0, but 1/1, including header

for (int i = 1; i <=visCols; i++){setLineAttrib (1,i,1);}// Header (light gray)

cp.addMouseListener((MouseListener)this);

}

privatevoid appRow (int row,int visCols,int x,int y,int w,int h){// build column row by row

for (int j = 0; j < visCols; j++){

txt[row][j] =new JTextField();

txt[row][j].addMouseListener(this);

//txt[row][j].setBorder(BorderFactory.createEmptyBorder());// no border

//if ((j+1)%2==0) {// trial with only odd rows - forget it

//txt[row][j].setBorder(BorderFactory.createRaisedBevelBorder()); // border

//} else {

//txt[row][j].setBorder(BorderFactory.createEmptyBorder());// no border

//}

txt[row][j].setBorder(BorderFactory.createRaisedBevelBorder());

//txt[row][j].setBackground(Color.lightGray );

//txt[row][j].setForeground(Color.gray);

//txt[row][j].setBorder(BorderFactory.createLoweredBevelBorder());

//txt[row][j].setBorder(BorderFactory.createLineBorder(Color.black));// Thickness = 1

//txt[row][j].setBorder(BorderFactory.createLineBorder(Color.black,1)); // Color, Thickness

//System.out.println(txt[row][j].getBorder());

//txt[row][j].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));

//txt[row][j].setBorder(BorderFactory.createEtchedBorder());

//txt[row][j].setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED )); //

txt[row][j].setBounds(x+j*w,y+y*2*row,w,h);

//txt[row][j].setBounds(x+j*(w-1),y+y*2*row,w,h);

cp.add(txt[row][j]);

}

}

//Text in ein Tabellenfeld einf黦en

privatevoid insText (int row,int col, String tabField){

txt[row-1][col-1].setText(tabField);

}

//Eine Tabellenelement bekommt ein bestimmtes Aussehen

publicvoid setLineAttrib (int row,int col,int attrib){

switch (attrib){

case 1:// Hintergrund grau

txt[row-1][col-1].setBackground(Color.lightGray);

break;

default:

break;

}

}

publicvoid mouseClicked(MouseEvent arg0){

System.out.println("mouseClicked: "+arg0);

}

publicvoid mouseEntered(MouseEvent arg0){

System.out.println("mouseEntered: "+arg0);

}

publicvoid mouseExited(MouseEvent arg0){

System.out.println("mouseExited: "+arg0);

}

publicvoid mousePressed(MouseEvent arg0){

System.out.println("mousePressed: "+arg0);

}

publicvoid mouseReleased(MouseEvent arg0){

System.out.println("mouseReleased: "+arg0);

}

}

[8865 byte] By [DeutscherMichela] at [2007-11-27 7:52:18]
# 1
txt[row][j].setBorder(BorderFactory.createMatteBorder(0, j==0?1:0, 1, 1, Color.GRAY));
Michael_Dunna at 2007-7-12 19:33:32 > top of Java-index,Desktop,Core GUI APIs...
# 2
Perfect., works, but I don't know why.Where in the sun (or other) documentation do I find this?ThanksMichel
DeutscherMichela at 2007-7-12 19:33:32 > top of Java-index,Desktop,Core GUI APIs...
# 3

> but I don't know why.

you had a border for each 'cell', meaning between adjacent 'cells' there were 2 borders,

right side of one adjoining left side of adjacent

likewise for bottom/top

the matte border allows you to specify 0 pixels wide for top/left/bottom/right,

meaning the overall 2 borders are 1 pixel wide

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/BorderFactory.html#createMatteBorder(int,%20int,%20int,%20int,%20java.awt.Color)

Michael_Dunna at 2007-7-12 19:33:32 > top of Java-index,Desktop,Core GUI APIs...