Solved

grids and borders

Posted on 2004-10-28
249 Views
Last Modified: 2010-03-31
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
0
Question by:Samooramad
    26 Comments
     
    LVL 37

    Expert Comment

    by:zzynx
    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
     
    LVL 37

    Expert Comment

    by:zzynx
    So,
    >>gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
    is the clue
    0
     
    LVL 26

    Expert Comment

    by:ksivananth
    Hi,

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

    Regards,
    Siva
    0
     

    Author Comment

    by:Samooramad
    zzynx, I applied it to my code which uses labels instead of textfields but it didnt change
    0
     

    Author Comment

    by:Samooramad
    Siva, I'm not sure what you mean..please post n example
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    >> 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
     
    LVL 92

    Assisted Solution

    by:objects
    Try adding a border to the component you require borders for
    eg.
    comp.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    0
     

    Author Comment

    by:Samooramad
    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
     

    Author Comment

    by:Samooramad
    objects, that didnt work either.. could it be I need to set a color or something?
    0
     

    Author Comment

    by:Samooramad
    objects, that didnt work either.. could it be I need to set a color or something?
    0
     

    Author Comment

    by:Samooramad
    objects, that didnt work either.. could it be I need to set a color or something?
    0
     

    Author Comment

    by:Samooramad
    objects, that didnt work either.. could it be I need to set a color or something?
    0
     
    LVL 92

    Expert Comment

    by:objects
    > 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
     
    LVL 37

    Expert Comment

    by:zzynx
    In my example you can change the textfields by labels and see that it works.
    Can you show us your complete code?
    0
     

    Author Comment

    by:Samooramad
    it would be too long..what exactly do you need to see? how I fill the grids?
    0
     

    Author Comment

    by:Samooramad
    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
     
    LVL 37

    Expert Comment

    by:zzynx
    >> 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
     

    Author Comment

    by:Samooramad
    >>If you want that you could go like objects said: give each component a line border

    for some reason that didnt work
    0
     
    LVL 37

    Accepted Solution

    by:
    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
     

    Author Comment

    by:Samooramad
    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
     

    Author Comment

    by:Samooramad
    >>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
     
    LVL 37

    Expert Comment

    by:zzynx
    Thanks. Whish I could have helped better.
    0
     

    Author Comment

    by:Samooramad
    you always help!!! thanks for the effort you always put into it :)
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    :)
    0
     

    Author Comment

    by:Samooramad
    trust me I will asking MANY MANY more questions..just keep helping me
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    >> trust me I will asking MANY MANY more questions
    Ready to receive :)
    >> just keep helping me
    Why shouldn't I ?
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Title # Comments Views Activity
    how to extract window origin in spring 2 39
    canBalance challenge 34 42
    hash value 2 22
    countHi challenge 25 41
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now