안녕하세요

프로그램 과정에서 막혔던 문제들에 대한 해결책 정리


페이지 목록

2011년 7월 25일 월요일

JAVA JFrame X button 클릭 시 이벤트 헨들

public void setDefaultCloseOperation(int operation)
Sets the operation that will happen by default when the user initiates a "close" on this frame. You must specify one of the following choices:
  • DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
  • HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registeredWindowListener objects.
  • DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.
  • EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.
The value is set to HIDE_ON_CLOSE by default. DO_NOTHING_ON_CLOSE => 아무 것도 안한다. HIDE_ON_CLOSE => 프레임을 감춘다. DISPOSE_ON_CLOSE => 프레임을 감추고 버린다. (그 프레임만 종료) EXIT_ON_CLOSE => 프로그램 자체를 끈다 (모든 프레임을 종료) 이 상수를 쓸 시에는 JFrame. 을 앞에 붙여야 한다. JFrame의 상수이기 때문이다.

JTable 값 가져 오는 방법

getValueAt

public Object getValueAt(int row,
                         int column)
Returns the cell value at row and column.Note: The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, the column at a given index in the view will change. Meanwhile the user's actions never affect the model's column ordering.

Parameters:
row - the row whose value is to be queried
column - the column whose value is to be queried
Returns:
the Object at the specified cell

Container에 Table 삽입 방법

Adding a Table to a Container

Here is typical code for creating a scroll pane that serves as a container for a table:
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
// ScrollPane에 테이블을 넣는 방법
The two lines in this snippet do the following:
  • The JScrollPane constructor is invoked with an argument that refers to the table object. This creates a scroll pane as a container for the table; the table is automatically added to the container.
  • JTable.setFillsViewportHeight is invoked to set the fillsViewportHeight property. When this property is true the table uses the entire height of the container, even if the table doesn't have enough rows to use the whole vertical space. This makes it easier to use the table as a drag-and-drop target.
The scroll pane automatically places the table header at the top of the viewport. The column names remain visible at the top of the viewing area when the table data is scrolled.
If you are using a table without a scroll pane, then you must get the table header component and place it yourself. For example:
container.setLayout(new BorderLayout());
container.add(table.getTableHeader(), BorderLayout.PAGE_START);
container.add(table, BorderLayout.CENTER);
//Container에 직접 table을 삽입하는 방법
출처: http://download.oracle.com/javase/tutorial/uiswing/components/table.html#data

JTable 열과 행 삽입 방법


DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);

// Create a couple of columns
model.addColumn("Col1");
model.addColumn("Col2");

// Append a row
model.addRow(new Object[]{"v1", "v2"});
// there are now 2 rows with 2 columns

// Append a row with fewer values than columns.
// The left-most fields in the new row are populated
// with the supplied values (left-to-right) and fields
// without values are set to null.
model.addRow(new Object[]{"v1"});
// there are now 3 rows with 2 columns

// Append a row with more values than columns.
// The extra values are ignored.
model.addRow(new Object[]{"v1", "v2", "v3"});
// there are now 4 rows with 2 columns
출처: http://www.exampledepot.com/egs/javax.swing.table/AppendRow.html

2011년 7월 22일 금요일

세션 제거하기

 시간을 체크한다.

 세션 종료의 세가지 이유
 1) 시간이 다 되어서 (타임 아웃)
 2) 개발자가 세션 객체에 Invalidate() 메소드를 실행
 3) App이 다운되는 경우( 문제가 생겨 다운되거나, 언디플로이(undeploy) 되는 경우)

쿠키(Cookie)

 쿠키는 Header 중 하나이다.
 Container에서 거의 모든 Cookie 관련 작업을 수행한다.

 Response 객체에 세션 Cookie 보내는 방법
 HttpSession session = request.getSession();

 Request 객체로부터 세션 ID 가져오기
 HttpSession session = request.getSession();

session.isNew() - 세션이 새로 만들어졌는지 아닌지 확인

 URL 재작성 => response.encodeURL( " blur blur ");

 쿠키 => URL 재작성

 정적인 페이지는 URL 재작성이 불가 하다.

컨테이너에서 클라이언트를 구분 하는 방법

 Client는 유일한 Session ID가 필요하다. 그리고 쿠키를 사용해서 Client와 Container는 이 Session ID를 공유한다.