Solved

Swing: Margins or padding

Posted on 2011-09-29
18
1,591 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 34
by zero exception 10 52
ejb stateless example 2 21
jsp login check 12 42
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

856 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