JScrollPane 스크롤바 보이기/숨기기
프로그래밍/java 2021. 12. 28. 22:52반응형
setHorizontalScrollBarPolicy, setVerticalScrollBarPolicy 를 사용하여 설정
package test.swing;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
// JScrollPane scrollbar show / hide
public class MainTest {
public static void main(String[] args) {
MainUI mainUI = new MainUI();
mainUI.setVisible(true);
}
}
class MainUI extends JFrame {
JScrollPane scrollPane1 = null;
JScrollPane scrollPane2 = null;
JScrollPane scrollPane3 = null;
JTable table1 = null;
JTable table2 = null;
JTable table3 = null;
DefaultTableModel tableModel = null;
MainUI() {
setPreferredSize(new Dimension(400, 600));
setLayout(new GridLayout(3, 1));
System.out.println("JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED = " + JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
System.out.println("JScrollPane.HORIZONTAL_SCROLLBAR_NEVER = " + JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
System.out.println("JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS = " + JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
System.out.println("JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED = " + JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
System.out.println("JScrollPane.VERTICAL_SCROLLBAR_NEVER = " + JScrollPane.VERTICAL_SCROLLBAR_NEVER);
System.out.println("JScrollPane.VERTICAL_SCROLLBAR_ALWAYS = " + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
table1 = new JTable();
tableModel = (DefaultTableModel) table1.getModel();
tableModel.addColumn("Column");
tableModel.addRow(new Object[]{"SCROLLBAR_AS_NEEDED"});
scrollPane1 = new JScrollPane(table1);
add(scrollPane1);
System.out.println("default h policy = " + scrollPane1.getHorizontalScrollBarPolicy() + ", v policy = " + scrollPane1.getVerticalScrollBarPolicy());
table2 = new JTable();
tableModel = (DefaultTableModel) table2.getModel();
tableModel.addColumn("Column");
tableModel.addRow(new Object[]{"HORIZONTAL_SCROLLBAR_NEVER"});
tableModel.addRow(new Object[]{"VERTICAL_SCROLLBAR_ALWAYS"});
scrollPane2 = new JScrollPane(table2);
scrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane2);
table3 = new JTable();
tableModel = (DefaultTableModel) table3.getModel();
tableModel.addColumn("Column");
tableModel.addRow(new Object[]{"HORIZONTAL_SCROLLBAR_ALWAYS"});
tableModel.addRow(new Object[]{"VERTICAL_SCROLLBAR_NEVER"});
scrollPane3 = new JScrollPane(table3);
scrollPane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
add(scrollPane3);
pack();
}
}
실행결과
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED = 30
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER = 31
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS = 32
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED = 20
JScrollPane.VERTICAL_SCROLLBAR_NEVER = 21
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS = 22
default h policy = 30, v policy = 20
반응형