스윙에서 패널의 크기 조정 문제입니다..

sadrove의 이미지

아래 소스는 panelProperty란 패널에 table이라는 테이블을 추가하는 소스입니다.
여기서 테이블의 내용이 많아질경우 스크롤을 사용할 수 있게 해놨는데요..
문제는 panelProperty 패널의 크기를 조절할 수가 없습니다..
setBounds()와 setSize()... 둘다 써봤는데 안 먹더군요...
어떻게 하면 panelProperty 패널의 크기를 조절할 수 있을까요..?..

그리고 실제 실행시켜보면 테이블의 컬럼부분이 안나옵니다...이건 또 왜이런지.. :(


public class RightGround extends JPanel {
	
	JPanel panelProperty;		//속성 패널
	
	public RightGround(String title){
		super(new FlowLayout(FlowLayout.LEFT));

		//글꼴의 설정
		Font ffont = new Font("굴림", Font.PLAIN, 13);
		//////////////////////////////////////////////////////////////////////////////

	panelProperty = new JPanel(new FlowLayout());
		
        // 컬럼 헤더 이름들
        String col_names[] = {"Class", "on Property", "Property Value", "URI"};
        // 테이블 데이터
        String data[][] = {
                {"Anonymous", "hasSugar", "Dry", "http://www.w3.org/TR/2003/CR-owl-guide-20030818/wine#"},
	            {"Anonymous", "hasSugar", "Dry", "http://www.w3.org/TR/2003/CR-owl-guide-20030818/wine#"}
        };
        
        JLabel propertyName = new JLabel("is a super-class");
        // 테이블을 만든다.
        JTable table = new JTable(data, col_names);
        
        // 테이블을 추가한다.
        JScrollPane scrollpane = new JScrollPane(table);
        
        panelProperty.add(scrollpane, BorderLayout.CENTER);
        panelProperty.add(table);
        panelProperty.setVisible(true);
        
        panelProperty.setBorder(new TitledBorder("클래스 속성"));
        
		add(panelProperty);
		add(table);
				
	}
}




p.s 오늘 질문 무쟈게 하네요..^^;;..답변 주신분들 감사합니다..복 받으세요~~ :wink:

File attachments: 
첨부파일 크기
Image icon 1.gif1.29 KB
recypace의 이미지

setPreferredSize() 를 한번 써보시죠.

sadrove의 이미지

recypace wrote:
setPreferredSize() 를 한번 써보시죠.

죄송한데...
panelProperty.setPreferredSize() 란 메소드는 존재하지 않는데..
어디서 사용해야 하나요..?...

bw001730의 이미지

님의 소스는 님이 멀 만들려고하는지 이해할수 없습니다. 기초를 먼저 습득하심이...
이런 질문은 사실 스윙하시는 분들에겐 기초적인 부분인데
사람들이 답변을 올리지 않는 이유는... 님의 소스가
너무 기초가 없기때문일것 같습니다.(기분상하지 마세요 제발~~)

제가 잘못된 부분을 지적해보겟습니다.

잘못된 부분 1:
table을 세번이나 add 하고 있습니다.
. 두번은 table을 직접 add 하고 한번은 table을 가지고 있는
scrollpane을 add 하고...
이렇게 세번씩이나 add 하시는 목적이 있으신건지 모르겠으나..
세번 add되면 어디로 add될런지..

잘못된 부분 2:
FlowLayout을 직접 생성해서 JPanel생성자에 넣어주셨는데
add할때 BorderLayout.CENTER 라고 하시는 부분
(JPanel은 기본적으로 FlowLayout을 사용하므로 생성자에 지정안해주셔도 됩니다.)

스윙하실때 젤 첨배우는게 Layout 일텐데..
아직 님께선 Layout에 대한 이해가 부족한것 같네요
Layout을 이해하고 나면 Panel을 원하는 크기로 하실수
있을 것입니다.
아니 대부분의 경우에 원하는 크기로 설정하려고 하지 않겠죠..
간단한 예제에서는 프레임의 크기 조절만으로 나머진 자동조절됩니다.

책에 있는 테스트 예제를 좀더 작성해보시길 권해드립니다.
그 후에 님께서 만들려는 것을 시도해보시는 것이 좋을것 같네요

sadrove의 이미지

이렇게 자세하기 지적해주셨는데...기분 상하다뇨..^^..
겸허히 받아들이겠습니다..
정확한 이해없이...무조건 결과물만 얻으려했던 제가 경솔했습니다..
좀 더 공부하겠습니다...^^

recypace의 이미지

윗 분이 말씀하신데로, layout을 적극 이용하시는 것이 좋구요.. :-)

이거 한번 쓰면 size 조절하고 배치해야 하는 것들이 다 짜증이 나실껍니다.

그러고, JPanel에 setPreferredSize()가 있습니다.

혹시 JBuilder 같은 것을 쓰신다거나, Eclipse에 visual editing을 쓰신다면

generation되는 code들을 한번씩 쭉 보세요. 머가 최소한으로 해야 하는

일인지 아시게 될껍니다. 첨에 쫌 고생하셔야 금방금방 늡니다.

첨에 쉽게 시작했더니, 요즘엔 code helper없으면 코딩을 못하겠더군요.

쩝. -__-;;

public void setPreferredSize(Dimension preferredSize)

    Sets the preferred size of this component. If preferredSize is null, the UI will be asked for the preferred size.
JCastle의 이미지

FlowLayout 에서는 자체적으로 size를 계산하기 때문에,
setSize() 등의 메소드가 먹지 않습니다.
즉, RightGround 의 크기에 따라 panelProperty 의 크기가 자동계산됩니다.

   public RightGround(String title){ 
      super(new FlowLayout(FlowLayout.LEFT));

위 부분을

   public RightGround(String title){ 
      super(null);

이렇게 해 주시고 setBounds()를 한 번 호출해 주시면 될 것 같네요.

안녕하세요 ^^

recypace의 이미지

참고로, setSize와 setPreferredSize는 다릅니다.
layout manager가 preferredSize를 query해서 적당한 크기등등을
계산하기 때문에죠. 따라서 자신만의 component를 만들때
getPreferredSize, getMinimunSize, getMaximumSize등등은
구현해줘야 합니다.

아래 layout 하는 방법에 대한 설명이 도움이 되실껍니다.

Quote:
Here's an example of a layout management sequence for a frame (JFrame).

1. After the GUI is constructed, the pack method is invoked on the JFrame. This specifies that the frame should be at its preferred size.

2. To find the frame's preferred size, the frame's layout manager adds the size of the frame's edges to the preferred size of the component directly contained by the frame. This is the sum of the preferred size of the frame's content pane, plus the size of the frame's menu bar, if any.

3. The content pane's layout manager is responsible for figuring out the content pane's preferred size. By default, this layout manager is a BorderLayout object. However, let's assume that we replace it with a GridLayout object that's set up to create two columns. The interesting thing about grid layout is that it forces all components to be the same size, and it tries to make them as wide as the widest component's preferred width and as high as highest one's preferred height.

First, the grid layout manager queries the content pane for its insets — the size of the content pane's border, if any. Next, the grid layout manager queries each component in the content pane for its preferred size, noting the largest preferred width and largest preferred height. Then it calculates the content pane's preferred size.

4. When a component in the content pane is asked for its preferred size, the default implementation (used by most components) first checks whether the user specified a preferred size. If so, it reports that size. If not, it queries its look and feel for the preferred size.

그리고, custom component만드는 부분도 말이죠.

http://java.sun.com/docs/books/tutorial/uiswing/14painting/practice.html

sadrove의 이미지

답변 주신분들 진심으로 감사드립니다..
책 뒤적이다가 Box레이아웃으로 구성하는게 적당할 것 같아서..
어느정도 구성했는데... 한가지가 말썽이네요..

박스레이아웃 안에 Label을 하나 넣었습니다..
제대로 되긴 되는데..
문제는 이 Label이 가운데 정렬(?)로 표시된다는 겁니다..
왼쪽으로 오게 하고 싶은데..
어찌해야할지..
책을 뒤져봐도 도저히 없네요..-.-...
도움 부탁드립니다..

--------생략--------

Box rightBox = Box.createHorizontalBox();

--------생략--------

JLabel propertyName = new JLabel("test");

rightBox.add(Box.createHorizontalStrut(5));	//여백
rightBox.add(propertyName);

--------생략--------

여기서 test란 글자가 박스의 왼쪽에 오지 않고 가운데로 옵니다..
혹시 제 설명이 부족할까봐...
그 부분만 캡춰해서 올립니다.

댓글 첨부 파일: 
첨부파일 크기
Image icon 0바이트

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.