grids and borders

Hi experts,

I'm using gridBagConstraints and was wondering if I could have some grids have a border so that it would look like part of a table?

please include code

thanks
SamooramadAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
I tried by putting each component in a JPanel and giving that panel a line border. That works.
But you have the problem that the lines of the grid "don't match": each cell has (can have) a different width/heigth.

I think you'll have to go for a real JTable with its own cell render showing the component you want at the place you want.
But that feels like much work for a simple result.
0
 
zzynxSoftware engineerCommented:
Like this?

/*
 * GridDemo.java
 *
 */


/**
 *
 * @author  zzynx
 */
public class GridDemo extends javax.swing.JFrame {
   
    /** Creates new form GridDemo */
    public GridDemo() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jTextField1.setText("jTextField1");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jTextField1, gridBagConstraints);

        jTextField2.setText("jTextField2");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jTextField2, gridBagConstraints);

        jTextField3.setText("jTextField3");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jTextField3, gridBagConstraints);

        jTextField4.setText("jTextField4");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jTextField4, gridBagConstraints);

        pack();
    }
   
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new GridDemo().show();
    }
   
    // Variables declaration - do not modify
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration
   
}
0
 
zzynxSoftware engineerCommented:
So,
>>gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
is the clue
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ksivananthCommented:
Hi,

   Fill the component in the grid cell and set the border probably line border or matt border to the component.

Regards,
Siva
0
 
SamooramadAuthor Commented:
zzynx, I applied it to my code which uses labels instead of textfields but it didnt change
0
 
SamooramadAuthor Commented:
Siva, I'm not sure what you mean..please post n example
0
 
zzynxSoftware engineerCommented:
>> zzynx, I applied it to my code which uses labels instead of textfields but it didnt change
That shouldn't make any difference. Show me your code.
0
 
objectsConnect With a Mentor Commented:
Try adding a border to the component you require borders for
eg.
comp.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
0
 
SamooramadAuthor Commented:
JLabel titleH = new JLabel ("Hospital");
           titleH.setFont(new java.awt.Font("Serif", 1, 13));
           titleH.setForeground(Color.BLUE);
           gridBagConstraints.gridx = 0;
           gridBagConstraints.gridy = 0;
           gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
           gridBagConstraints.anchor = GridBagConstraints.CENTER;
           panel.add(titleH, gridBagConstraints);
0
 
SamooramadAuthor Commented:
objects, that didnt work either.. could it be I need to set a color or something?
0
 
SamooramadAuthor Commented:
objects, that didnt work either.. could it be I need to set a color or something?
0
 
SamooramadAuthor Commented:
objects, that didnt work either.. could it be I need to set a color or something?
0
 
SamooramadAuthor Commented:
objects, that didnt work either.. could it be I need to set a color or something?
0
 
objectsCommented:
> objects, that didnt work either.. could it be I need to set a color or something?

depends what you want, that was just an example.
you can set whatever border you want.

eg.

comp.setBorder(BorderFactory.createLineBorder(Color.red, 5));
0
 
zzynxSoftware engineerCommented:
In my example you can change the textfields by labels and see that it works.
Can you show us your complete code?
0
 
SamooramadAuthor Commented:
it would be too long..what exactly do you need to see? how I fill the grids?
0
 
SamooramadAuthor Commented:
hey zzynx, I substitued the text fields in your code for labels and it didnt draw any lines between them..I see the labels and they are spaced out in the grids but there wasn't any visible line between them..
did I do it wrong?
0
 
zzynxSoftware engineerCommented:
>> but there wasn't any visible line between them
Ooooooh, didn't realize that you really want to see lines
If you want that you could go like objects said: give each component a line border
0
 
SamooramadAuthor Commented:
>>If you want that you could go like objects said: give each component a line border

for some reason that didnt work
0
 
SamooramadAuthor Commented:
ok I'll try it..if it doesn't work I'll open a new question so you can tell me how to use a JTable :)
thanx
0
 
SamooramadAuthor Commented:
>>But that feels like much work for a simple result.
it is a simple result. thats why I thought I could just use the grids as they are
0
 
zzynxSoftware engineerCommented:
Thanks. Whish I could have helped better.
0
 
SamooramadAuthor Commented:
you always help!!! thanks for the effort you always put into it :)
0
 
zzynxSoftware engineerCommented:
:)
0
 
SamooramadAuthor Commented:
trust me I will asking MANY MANY more questions..just keep helping me
0
 
zzynxSoftware engineerCommented:
>> trust me I will asking MANY MANY more questions
Ready to receive :)
>> just keep helping me
Why shouldn't I ?
0
All Courses

From novice to tech pro — start learning today.