Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

grids and borders

Posted on 2004-10-28
26
Medium Priority
?
298 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
Comment
Question by:Samooramad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 9
  • 2
  • +1
26 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12433223
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
ID: 12433252
So,
>>gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
is the clue
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 12434154
Hi,

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

Regards,
Siva
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

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

Author Comment

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

Expert Comment

by:zzynx
ID: 12436963
>> 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
objects earned 400 total points
ID: 12439782
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
ID: 12449248
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
ID: 12449267
objects, that didnt work either.. could it be I need to set a color or something?
0
 

Author Comment

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

Author Comment

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

Author Comment

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

Expert Comment

by:objects
ID: 12449442
> 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
ID: 12452452
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
ID: 12452812
it would be too long..what exactly do you need to see? how I fill the grids?
0
 

Author Comment

by:Samooramad
ID: 12452834
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
ID: 12453235
>> 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
ID: 12453685
>>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:
zzynx earned 800 total points
ID: 12461021
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
ID: 12461181
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
ID: 12461190
>>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
ID: 12470533
Thanks. Whish I could have helped better.
0
 

Author Comment

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

Expert Comment

by:zzynx
ID: 12473922
:)
0
 

Author Comment

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

Expert Comment

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

604 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