[java swing] 테이블에 컬럼 추가/삭제 만들기.

Jong·2021년 8월 12일
0

java swing

목록 보기
2/4


package jdbc;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import gui.StudyFrame;


public class prc extends StudyFrame{

		
	
	public static void main(String[] args) {
		
		
		Dimension dim = new Dimension(400,300);
		
		JFrame frame = new JFrame("Morph`s House");
		frame.setLocation(200, 400);
		frame.setPreferredSize(dim);
		
		String header[] = {"학생이름","국어","영어","수학"};
		
		String contents[][] = {
				{"백호왕","90","87","98"},
				{"왈숙이","100","99","100"},
				{"대장장이","30","25","9"}
		};
		
		DefaultTableModel model = new DefaultTableModel(contents, header);
		JTable table = new JTable(model);
		JScrollPane scrollpane = new JScrollPane(table);
		
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		
		
		JTextField nameField = new JTextField(5);
		JTextField sbj1 = new JTextField(3);
		JTextField sbj2 = new JTextField(3);
		JTextField sbj3 = new JTextField(3);
		
		panel.add(nameField);
		panel.add(sbj1);
		panel.add(sbj2);
		panel.add(sbj3);
		
		JButton addBtn = new JButton("추가");
		
		addBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				String inputStr[] = new String[4];
				
				inputStr[0] = nameField.getText();
				inputStr[1] = sbj1.getText();
				inputStr[2] = sbj2.getText();
				inputStr[3] = sbj3.getText();
				
				model.addRow(inputStr);
				
				nameField.setText("");
				sbj1.setText("");
				sbj2.setText("");
				sbj3.setText("");
				
			}
		});// addActionListener
		
		
		JButton cancelBtn = new JButton("삭제");
		
		 cancelBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				if(table.getSelectedRow() == -1) {
					return;
				}
				else {
					model.removeRow(table.getSelectedRow());
				}
				
			}
		});
		 
		 panel.add(addBtn);
		 panel.add(cancelBtn);
		 
		 frame.add(scrollpane, BorderLayout.CENTER);
		 frame.add(panel, BorderLayout.SOUTH);
		frame.pack();
		 frame.setVisible(true);
		 
		
		
		
	}//main
	
}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
profile
새싹 개발자 수련중(블로그 이전)

0개의 댓글