Solved

Swing: Margins or padding

Posted on 2011-09-29
18
1,549 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
ArrayIndexOutOfBoundException 9 81
maven project error 5 55
stackato and cloud 4 85
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

773 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