Solved

Swing: Margins or padding

Posted on 2011-09-29
18
1,521 Views
Last Modified: 2012-05-12
I am adding a JList to a JPanel. Please let me know how I can set the left margin or add some padding.

If possible, please provide some sample code. Thanks!
0
Comment
Question by:dshrenik
  • 10
  • 4
  • 4
18 Comments
 

Author Comment

by:dshrenik
ID: 36817863
Also, I want to set the spacing between the elements in the JList.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36817871
You can add an empty border - probably the easiest

http://www.exampledepot.com/egs/javax.swing/Borders.html
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36817881

I think you can customize your items in the list usiing CellRenderer

http://download.oracle.com/javase/6/docs/api/javax/swing/JList.html#renderer
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36817888
When you adding JList to JPanel - then its position will normally be determined by the layout which will choose, like any other elements.

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36817902
You just need to decide how you want to layout your components and chosse teh most appropriate layout - then you usually can come up with best arrangledmnt.
There are some alyouts where you can explicitly specify gaps, etc. but it is rarely when you really need to do it
0
 

Author Comment

by:dshrenik
ID: 36817911
Can you provide some sample code as to how I can set layouts as per my requirement? Thanks!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36817947

And what are your requirements?

Usually if you have some main comomnent and a few other smaller components on the sides of the window then
BorderLayout in the best choice

For example say you have List as your main element and some buttons on the right suide
something like that:
jpanel.setLayout(new BorderLyaout());
jpanel.add(jList);
jpanle.add(jpanle2, BorderLayout.EAST);
jpanel2.setLayout(new GridLayout(3,1));
jpanel2.add(button1);
jpanel2.add(button2);
jpanel2.add(button3);

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36817950
So if you look up FlowLayout()
BorderLayout() and GridLayout() you'll be able to make all more or laess simple layouts
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36817956
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 47

Expert Comment

by:for_yan
ID: 36817962
For some more sophisticetd cases you amy wand GridBagLayout - but this is rather rare
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36817973
So lauyout can be specified for any of you components, like panels and those panels can be placed within each other,
so even those three layouts in fact provide you with a lot of flexibility
0
 

Author Comment

by:dshrenik
ID: 36817982
Right now, when I add the JList to the JPanel, the the list is displayed at the extreme left, woth no border or padding. I want to have a small padding between the left edge of the panel and the edge where the list will be displayed.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36818255
>>
the the list is displayed at the extreme left, woth no border or padding. I want to have a small padding between the left edge of the panel and the edge where the list will be displayed.
>>

I told you how to do that above
0
 

Author Comment

by:dshrenik
ID: 36818269
@CEHJ:
Sorry, I forgot to reply to your post. I tried doing that, but it did not make a difference to the padding.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36818286
Can you show your code?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 36818314

check this:
import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class FrameWithList extends JFrame
{
    public FrameWithList (){


        Vector<String> v = new Vector<String>();
        v.add("1");
        v.add("2");
        v.add("3");

          JList list = new JList(v);

        JPanel panel = new JPanel();
        JPanel panel1 = new JPanel();
                BorderLayout layout =           new BorderLayout();
        layout.setHgap(15);

        panel.setLayout(layout);
        panel.add(list);
        panel.add(panel1,BorderLayout.WEST);

        Container c  = this.getContentPane();
        c.add(panel);

        this.setSize(300,300);
        this.setVisible(true);


    }

    public static void main(String[] args) {
        new FrameWithList();
    }

}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36818322
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 36818580
The following shows how the border thing works. Start the program

java F

and

java -Dpad=yes F

and note the difference
import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;


public class F extends JFrame {
    private void setGui() {
        try {
            setLocation(0, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container cp = getContentPane();
            String[] items = { "Alpha", "Beta", "Gamma" };
            JList list = new JList(items);
            JScrollPane sp = new JScrollPane(list);

            if ("yes".equals(System.getProperty("pad"))) {
                sp.setBorder(BorderFactory.createMatteBorder(20, 20, 20, 20,
                        Color.white));
            }

            cp.add(sp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        F f = new F();
                        f.setGui();
                        f.setSize(200, 200);
                        f.setVisible(true);
                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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 …

919 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

14 Experts available now in Live!

Get 1:1 Help Now