Solved

Swing: Margins or padding

Posted on 2011-09-29
18
1,485 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

758 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

20 Experts available now in Live!

Get 1:1 Help Now