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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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
objectsCommented:
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
zzynxSoftware 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.