Swing/DisableScrolling のバックアップ(No.9)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/DisableScrolling へ行く。
- 1 (2005-10-05 (水) 11:30:25)
- 2 (2005-12-16 (金) 17:18:38)
- 3 (2006-02-27 (月) 15:47:47)
- 4 (2006-04-12 (水) 19:41:03)
- 5 (2007-03-10 (土) 01:35:10)
- 6 (2007-10-03 (水) 23:38:10)
- 7 (2011-11-14 (月) 18:17:33)
- 8 (2011-11-15 (火) 18:00:20)
- 9 (2011-11-26 (土) 12:35:32)
- 10 (2011-11-28 (月) 15:42:39)
- 11 (2012-01-10 (火) 14:26:43)
- 12 (2013-03-22 (金) 11:56:55)
- 13 (2013-04-10 (水) 16:18:08)
- 14 (2014-03-16 (日) 22:49:46)
- 15 (2014-09-17 (水) 13:13:19)
- 16 (2014-10-21 (火) 01:58:28)
- 17 (2015-11-13 (金) 00:51:18)
- 18 (2016-09-22 (木) 21:20:17)
- 19 (2017-03-23 (木) 16:42:58)
- 20 (2017-03-28 (火) 19:27:51)
- 21 (2018-02-15 (木) 17:28:06)
- 22 (2020-02-20 (木) 15:07:14)
- 23 (2021-08-06 (金) 19:02:43)
TITLE:JScrollPaneのスクロールを禁止
Posted by aterai at 2005-10-03
JScrollPaneのスクロールを禁止
JScrollPaneのスクロールを一時的に禁止します。
- &jnlp;
- &jar;
- &zip;
サンプルコード
JCheckBox b = new JCheckBox("スクロールを禁止する");
b.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
JCheckBox box = (JCheckBox)ie.getSource();
boolean flag = !box.isSelected();
JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setEnabled(flag);
scrollPane.setWheelScrollingEnabled(flag);
table.setEnabled(flag);
}
});
解説
- スクロールバーを取得し、JScrollBar#setEnabled(boolean)メソッドを使って、これを無効にします。
- JScrollPane#setWheelScrollingEnabled(boolean)メソッドで、マウスホイールによるスクロールを無効にします。
- JScrollPaneに配置したコンポーネントをsetEnabled(false)とし、フォーカスの移動などによるスクロールを無効にします。
JDK 1.7.0 で追加されたJLayerを使用して、以下のように入力を禁止する方法もあります。
- JLayerで指定したコンポーネントへの入力を禁止
//package example; //-*- mode:java; encoding:utf8n; coding:utf-8 -*- // vim:set fileencoding=utf-8: //@homepage@ import java.awt.*; import java.awt.event.*; import java.beans.PropertyChangeEvent; import javax.swing.*; import javax.swing.plaf.LayerUI; import javax.swing.table.*; public class MainPanel extends JPanel { private final String[] columnNames = {"String", "Integer", "Boolean"}; private final Object[][] data = { {"aaa", 12, true}, {"bbb", 5, false}, {"CCC", 92, true}, {"DDD", 0, false}, }; DefaultTableModel model = new DefaultTableModel(data, columnNames) { @Override public Class<?> getColumnClass(int column) { return getValueAt(0, column).getClass(); } }; private final JTable table = new JTable(model); private final JScrollPane scroll = new JScrollPane(table); private final JCheckBox check = new JCheckBox("Disable Scrolling"); public MainPanel() { super(new BorderLayout()); TableColumn col = table.getColumnModel().getColumn(0); col.setMinWidth(60); col.setMaxWidth(60); col.setResizable(false); for(int i=0;i<100;i++) { model.addRow( new Object[] {"Name "+i, Integer.valueOf(i), Boolean.FALSE}); } final DisableInputLayerUI layerUI = new DisableInputLayerUI(); final JLayer<JComponent> jlayer = new JLayer<JComponent>(scroll, layerUI); check.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent ie) { JCheckBox box = (JCheckBox)ie.getSource(); layerUI.setEnabled(!box.isSelected()); // JScrollBar bar = scroll.getVerticalScrollBar(); // bar.setEnabled(flag); // scroll.setWheelScrollingEnabled(flag); // table.setEnabled(flag); } }); scroll.setComponentPopupMenu(new TablePopupMenu()); table.setInheritsPopupMenu(true); add(jlayer); add(check, BorderLayout.NORTH); setPreferredSize(new Dimension(320, 240)); } class TestCreateAction extends AbstractAction{ public TestCreateAction(String label, Icon icon) { super(label,icon); } @Override public void actionPerformed(ActionEvent evt) { testCreateActionPerformed(evt); } } private void testCreateActionPerformed(ActionEvent e) { model.addRow(new Object[] {"New Name", Integer.valueOf(0), Boolean.FALSE}); Rectangle rect = table.getCellRect(model.getRowCount()-1, 0, true); table.scrollRectToVisible(rect); } class DeleteAction extends AbstractAction{ public DeleteAction(String label, Icon icon) { super(label,icon); } @Override public void actionPerformed(ActionEvent evt) { deleteActionPerformed(evt); } } public void deleteActionPerformed(ActionEvent evt) { int[] selection = table.getSelectedRows(); if(selection==null || selection.length<=0) return; for(int i=selection.length-1;i>=0;i--) { model.removeRow(table.convertRowIndexToModel(selection[i])); } } private class TablePopupMenu extends JPopupMenu { private final Action createAction = new TestCreateAction("add", null); private final Action deleteAction = new DeleteAction("delete", null); public TablePopupMenu() { super(); add(createAction); addSeparator(); add(deleteAction); } @Override public void show(Component c, int x, int y) { createAction.setEnabled(!check.isSelected()); int[] l = table.getSelectedRows(); deleteAction.setEnabled(l!=null && l.length>0); super.show(c, x, y); } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); } public static void createAndShowGUI() { try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception e) { e.printStackTrace(); } JFrame frame = new JFrame("@title@"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(new MainPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } class DisableInputLayerUI extends LayerUI<JComponent> { private boolean isBlocking = false; @Override public void paint(Graphics g, JComponent c) { super.paint(g, c); if(!isBlocking) return; Graphics2D g2 = (Graphics2D) g.create(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f)); g2.setPaint(Color.GRAY); g2.fillRect(0, 0, c.getWidth(), c.getHeight()); g2.dispose(); } @Override public void installUI(JComponent c) { super.installUI(c); JLayer jlayer = (JLayer)c; jlayer.setLayerEventMask( AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.MOUSE_WHEEL_EVENT_MASK | AWTEvent.KEY_EVENT_MASK); } @Override public void uninstallUI(JComponent c) { JLayer jlayer = (JLayer)c; jlayer.setLayerEventMask(0); super.uninstallUI(c); } @Override public void eventDispatched(AWTEvent e, JLayer l) { if(isBlocking && e instanceof InputEvent) { ((InputEvent)e).consume(); } } private static final String CMD_REPAINT = "repaint"; public void setEnabled(boolean flag) { isBlocking = !flag; firePropertyChange(CMD_REPAINT,flag,!flag); } @Override public void applyPropertyChange(PropertyChangeEvent pce, JLayer l) { String cmd = pce.getPropertyName(); if(CMD_REPAINT.equals(cmd)) { l.getGlassPane().setVisible((Boolean)pce.getNewValue()); l.repaint(); } } }