Solved

Swing: Margins or padding

Posted on 2011-09-29
18
1,736 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
[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
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
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 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…

626 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