티스토리 뷰

* CardLayout을 사용하여 두가지 이상의 패널을 이용할 경우
* 패널이 열렸을때 JScrollPane이 마우스 휠에 반응을 하지 않는다.
* 현재 파악한 바로는 Focus문제인듯하며 해당 패널이 열릴때 잠시나마
* 해당 JScrollPane의 requestFocusInWindow() 메소드 이용하여
* 포커스를 주면 정상적으로 마우스 휠에 반응을 하여 스크롤이 가능하다.

메인 프레임
public class Frame extends JFrame{
	private CardLayout cards = new CardLayout();;
	private P_One one;
	private P_Two two;
	
	public Frame() {
		super("JScrollPane need Focus.");
		setSize(500, 500);
		getContentPane().setLayout(cards);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		
		one = new P_One(this);
		two = new P_Two(this);
		
		getContentPane().add("One", one);
		getContentPane().add("Two", two);
		
		setVisible(true);
	}
	
	public void changePanel() {
		cards.next(this.getContentPane());
		
		//show될때 한번 포커스를 주면 마우스 휠에 바로 반응한다.
		one.getScrollPane().requestFocusInWindow();
		two.getScrollPane().requestFocusInWindow();
	}
}

패널1
public class P_One extends JPanel {
	private JTextField textField;
	private JLabel lblNewLabel;
	private JButton btnNewButton;
	private Frame F;
	private JScrollPane scrollPane;
	
	public P_One(Frame f) {
		setSize(500, 500);
		setLayout(null);
		
		F = f;
		
		textField = new JTextField();
		textField.setColumns(10);
		textField.setBounds(12, 10, 116, 21);
		add(textField);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 250, 500, 250);
		add(scrollPane);
		
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(480, 650));
		scrollPane.setViewportView(panel);
		panel.setLayout(null);
		
		JTextArea textPane = new JTextArea();
		textPane.setText("A");
		textPane.setBounds(12, 10, 460, 200);
		panel.add(textPane);
		
		JTextArea txtrB = new JTextArea();
		txtrB.setText("B");
		txtrB.setBounds(12, 220, 460, 200);
		panel.add(txtrB);
		
		JTextArea txtrC = new JTextArea();
		txtrC.setText("C");
		txtrC.setBounds(12, 430, 460, 200);
		panel.add(txtrC);
		
		lblNewLabel = new JLabel("Panel One");
		lblNewLabel.setBounds(429, 13, 59, 15);
		add(lblNewLabel);
		
		btnNewButton = new JButton("Change Panel");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				F.changePanel();
			}
		});
		btnNewButton.setBounds(375, 195, 113, 23);
		add(btnNewButton);

		setVisible(true);
	}
	
	public JScrollPane getScrollPane() {	return scrollPane;	}
}

패널2
public class P_Two extends JPanel {
	private JTextField textField;
	private JLabel lblNewLabel;
	private JButton btnNewButton;
	private Frame F;
	private JScrollPane scrollPane;
	
	public P_Two(Frame f) {
		setSize(500, 500);
		setLayout(null);
		
		F = f;
		
		textField = new JTextField();
		textField.setColumns(10);
		textField.setBounds(12, 10, 116, 21);
		add(textField);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 250, 500, 250);
		add(scrollPane);
		
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(480, 650));
		scrollPane.setViewportView(panel);
		panel.setLayout(null);
		
		JTextArea textPane = new JTextArea();
		textPane.setText("A");
		textPane.setBounds(12, 10, 460, 200);
		panel.add(textPane);
		
		JTextArea txtrB = new JTextArea();
		txtrB.setText("B");
		txtrB.setBounds(12, 220, 460, 200);
		panel.add(txtrB);
		
		JTextArea txtrC = new JTextArea();
		txtrC.setText("C");
		txtrC.setBounds(12, 430, 460, 200);
		panel.add(txtrC);
		
		lblNewLabel = new JLabel("Panel Two");
		lblNewLabel.setBounds(427, 13, 61, 15);
		add(lblNewLabel);
		
		btnNewButton = new JButton("Change Panel");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				F.changePanel();
			}
		});
		btnNewButton.setBounds(375, 195, 113, 23);
		add(btnNewButton);

		setVisible(true);
	}
	
	public JScrollPane getScrollPane() {	return scrollPane;	}
}
댓글