作業部屋の使い方を試しています。
(empty log message)
| @@ -0,0 +1,221 @@ | ||
| 1 | +/* | |
| 2 | + * To change this license header, choose License Headers in Project Properties. | |
| 3 | + * To change this template file, choose Tools | Templates | |
| 4 | + * and open the template in the editor. | |
| 5 | + */ | |
| 6 | +package package01; | |
| 7 | + | |
| 8 | +import java.awt.Color; | |
| 9 | +import java.awt.Component; | |
| 10 | +import java.util.ArrayList; | |
| 11 | +import javax.swing.JTable; | |
| 12 | +import javax.swing.table.DefaultTableCellRenderer; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * | |
| 16 | + * @author kgto | |
| 17 | + */ | |
| 18 | +public class TestTable01 extends javax.swing.JFrame { | |
| 19 | + | |
| 20 | + TestTableCellRenderer renderer; | |
| 21 | + /** | |
| 22 | + * Creates new form TestTable01 | |
| 23 | + */ | |
| 24 | + public TestTable01() { | |
| 25 | + initComponents(); | |
| 26 | + | |
| 27 | + renderer = new TestTableCellRenderer(); | |
| 28 | + jTable1.setDefaultRenderer(Object.class, renderer); | |
| 29 | + | |
| 30 | + } | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * This method is called from within the constructor to initialize the form. | |
| 34 | + * WARNING: Do NOT modify this code. The content of this method is always | |
| 35 | + * regenerated by the Form Editor. | |
| 36 | + */ | |
| 37 | + @SuppressWarnings("unchecked") | |
| 38 | + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents | |
| 39 | + private void initComponents() { | |
| 40 | + | |
| 41 | + jLabel1 = new javax.swing.JLabel(); | |
| 42 | + jTextRow = new javax.swing.JTextField(); | |
| 43 | + jLabel2 = new javax.swing.JLabel(); | |
| 44 | + jTextCol = new javax.swing.JTextField(); | |
| 45 | + jButton1 = new javax.swing.JButton(); | |
| 46 | + jScrollPane1 = new javax.swing.JScrollPane(); | |
| 47 | + jTable1 = new javax.swing.JTable(); | |
| 48 | + | |
| 49 | + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
| 50 | + | |
| 51 | + jLabel1.setText("Row"); | |
| 52 | + | |
| 53 | + jTextRow.setText("jTextField1"); | |
| 54 | + | |
| 55 | + jLabel2.setText("Column"); | |
| 56 | + | |
| 57 | + jTextCol.setText("jTextField2"); | |
| 58 | + | |
| 59 | + jButton1.setText("set"); | |
| 60 | + jButton1.addActionListener(new java.awt.event.ActionListener() { | |
| 61 | + public void actionPerformed(java.awt.event.ActionEvent evt) { | |
| 62 | + jButton1ActionPerformed(evt); | |
| 63 | + } | |
| 64 | + }); | |
| 65 | + | |
| 66 | + jTable1.setModel(new javax.swing.table.DefaultTableModel( | |
| 67 | + new Object [][] { | |
| 68 | + {null, null, null, null}, | |
| 69 | + {null, null, null, null}, | |
| 70 | + {null, null, null, null}, | |
| 71 | + {null, null, null, null} | |
| 72 | + }, | |
| 73 | + new String [] { | |
| 74 | + "Title 1", "Title 2", "Title 3", "Title 4" | |
| 75 | + } | |
| 76 | + )); | |
| 77 | + jScrollPane1.setViewportView(jTable1); | |
| 78 | + | |
| 79 | + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
| 80 | + getContentPane().setLayout(layout); | |
| 81 | + layout.setHorizontalGroup( | |
| 82 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| 83 | + .addGroup(layout.createSequentialGroup() | |
| 84 | + .addContainerGap() | |
| 85 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| 86 | + .addGroup(layout.createSequentialGroup() | |
| 87 | + .addComponent(jLabel1) | |
| 88 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
| 89 | + .addComponent(jTextRow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
| 90 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
| 91 | + .addComponent(jLabel2) | |
| 92 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
| 93 | + .addComponent(jTextCol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
| 94 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
| 95 | + .addComponent(jButton1)) | |
| 96 | + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
| 97 | + .addContainerGap(13, Short.MAX_VALUE)) | |
| 98 | + ); | |
| 99 | + layout.setVerticalGroup( | |
| 100 | + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| 101 | + .addGroup(layout.createSequentialGroup() | |
| 102 | + .addContainerGap() | |
| 103 | + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
| 104 | + .addComponent(jLabel1) | |
| 105 | + .addComponent(jTextRow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
| 106 | + .addComponent(jLabel2) | |
| 107 | + .addComponent(jTextCol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
| 108 | + .addComponent(jButton1)) | |
| 109 | + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
| 110 | + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE) | |
| 111 | + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
| 112 | + ); | |
| 113 | + | |
| 114 | + pack(); | |
| 115 | + }// </editor-fold>//GEN-END:initComponents | |
| 116 | + | |
| 117 | + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed | |
| 118 | + renderer.addRowColumn(1, 1); | |
| 119 | + renderer.addRowColumn(2, 0); | |
| 120 | + renderer.addRowColumn(2, 2); | |
| 121 | + jTable1.repaint(); | |
| 122 | + }//GEN-LAST:event_jButton1ActionPerformed | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * @param args the command line arguments | |
| 126 | + */ | |
| 127 | + public static void main(String args[]) { | |
| 128 | + /* Set the Nimbus look and feel */ | |
| 129 | + //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> | |
| 130 | + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. | |
| 131 | + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html | |
| 132 | + */ | |
| 133 | + try { | |
| 134 | + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { | |
| 135 | + if ("Nimbus".equals(info.getName())) { | |
| 136 | + javax.swing.UIManager.setLookAndFeel(info.getClassName()); | |
| 137 | + break; | |
| 138 | + } | |
| 139 | + } | |
| 140 | + } catch (ClassNotFoundException ex) { | |
| 141 | + java.util.logging.Logger.getLogger(TestTable01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| 142 | + } catch (InstantiationException ex) { | |
| 143 | + java.util.logging.Logger.getLogger(TestTable01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| 144 | + } catch (IllegalAccessException ex) { | |
| 145 | + java.util.logging.Logger.getLogger(TestTable01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| 146 | + } catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
| 147 | + java.util.logging.Logger.getLogger(TestTable01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| 148 | + } | |
| 149 | + //</editor-fold> | |
| 150 | + | |
| 151 | + /* Create and display the form */ | |
| 152 | + java.awt.EventQueue.invokeLater(new Runnable() { | |
| 153 | + public void run() { | |
| 154 | + new TestTable01().setVisible(true); | |
| 155 | + } | |
| 156 | + }); | |
| 157 | + } | |
| 158 | + | |
| 159 | + // Variables declaration - do not modify//GEN-BEGIN:variables | |
| 160 | + private javax.swing.JButton jButton1; | |
| 161 | + private javax.swing.JLabel jLabel1; | |
| 162 | + private javax.swing.JLabel jLabel2; | |
| 163 | + private javax.swing.JScrollPane jScrollPane1; | |
| 164 | + private javax.swing.JTable jTable1; | |
| 165 | + private javax.swing.JTextField jTextCol; | |
| 166 | + private javax.swing.JTextField jTextRow; | |
| 167 | + // End of variables declaration//GEN-END:variables | |
| 168 | +} | |
| 169 | + | |
| 170 | +class TestTableCellRenderer extends DefaultTableCellRenderer { | |
| 171 | + | |
| 172 | + ArrayList<RowColumn> arrayRowColumn = new ArrayList<>(); | |
| 173 | + | |
| 174 | + TestTableCellRenderer() { | |
| 175 | + super(); | |
| 176 | + } | |
| 177 | + | |
| 178 | + @Override | |
| 179 | + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { | |
| 180 | + Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); | |
| 181 | + | |
| 182 | + Color color = table.getBackground(); | |
| 183 | + if(chkRowColumn(row, column)) { | |
| 184 | + color = Color.red; | |
| 185 | + } | |
| 186 | + c.setBackground(color); | |
| 187 | + | |
| 188 | + return c; | |
| 189 | + } | |
| 190 | + | |
| 191 | + void addRowColumn(int row, int column) { | |
| 192 | + RowColumn inRowColumn = new RowColumn(); | |
| 193 | + inRowColumn.row = row; | |
| 194 | + inRowColumn.column = column; | |
| 195 | + | |
| 196 | + arrayRowColumn.add(inRowColumn); | |
| 197 | + } | |
| 198 | + | |
| 199 | + boolean chkRowColumn(int row, int column) { | |
| 200 | + boolean ans = false; | |
| 201 | + | |
| 202 | + RowColumn inRowColumn = new RowColumn(); | |
| 203 | + inRowColumn.row = row; | |
| 204 | + inRowColumn.column = column; | |
| 205 | + | |
| 206 | + for (RowColumn rowcolumn : arrayRowColumn) { | |
| 207 | + if((rowcolumn.row == inRowColumn.row) && (rowcolumn.column == inRowColumn.column)) { | |
| 208 | + ans = true; | |
| 209 | + break; | |
| 210 | + } | |
| 211 | + } | |
| 212 | + return ans; | |
| 213 | + } | |
| 214 | + | |
| 215 | + class RowColumn { | |
| 216 | + int row; | |
| 217 | + int column; | |
| 218 | + } | |
| 219 | + | |
| 220 | +} | |
| 221 | + |
| @@ -0,0 +1,61 @@ | ||
| 1 | +/* | |
| 2 | + * To change this license header, choose License Headers in Project Properties. | |
| 3 | + * To change this template file, choose Tools | Templates | |
| 4 | + * and open the template in the editor. | |
| 5 | + */ | |
| 6 | +package sample01; | |
| 7 | + | |
| 8 | +import java.awt.Color; | |
| 9 | +import java.awt.Component; | |
| 10 | +import javax.swing.JTable; | |
| 11 | +import javax.swing.table.DefaultTableCellRenderer; | |
| 12 | + | |
| 13 | +public class SampleTableCellRenderer02 extends DefaultTableCellRenderer { | |
| 14 | + private static final long serialVersionUID = -4100672856859272722L; | |
| 15 | + int row; | |
| 16 | + int column; | |
| 17 | + Color color; | |
| 18 | + | |
| 19 | + SampleTableCellRenderer02() { | |
| 20 | + super(); | |
| 21 | + } | |
| 22 | + | |
| 23 | + void setColor(int row, int column, Color color) { | |
| 24 | + this.row = row; | |
| 25 | + this.column = column; | |
| 26 | + this.color = color; | |
| 27 | + repaint(); | |
| 28 | + } | |
| 29 | + | |
| 30 | + @Override | |
| 31 | + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { | |
| 32 | + super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); | |
| 33 | + | |
| 34 | + /* | |
| 35 | + * どういう呼ばれ方をしてるのか確認するための出力文 | |
| 36 | + * 目に見えるセルだけ毎度、描画しているらしい | |
| 37 | + * スクロールさせると、それがわかる | |
| 38 | + */ | |
| 39 | + System.out.println("row:" + row + " /column:" + column + " /selected:" + isSelected + " /focus:" + hasFocus + " /value:" + value); | |
| 40 | + | |
| 41 | + // 選択されている行を赤色にする | |
| 42 | + if (isSelected) { | |
| 43 | + this.setBackground(Color.RED); | |
| 44 | + } else { | |
| 45 | + this.setBackground(table.getBackground()); | |
| 46 | + } | |
| 47 | + | |
| 48 | + // フォーカスが当たっているセルを黄色にする | |
| 49 | + if (hasFocus) { | |
| 50 | + this.setBackground(Color.yellow); | |
| 51 | + } | |
| 52 | + | |
| 53 | + // 行番号=1/列番号=1のセルを青色にする | |
| 54 | + if ((this.row == row) && (this.column == column)) { | |
| 55 | + this.setBackground(this.color); | |
| 56 | + } else { | |
| 57 | + this.setBackground(table.getBackground()); | |
| 58 | + } | |
| 59 | + return this; | |
| 60 | + } | |
| 61 | +} |
| @@ -0,0 +1,59 @@ | ||
| 1 | +/* | |
| 2 | + * To change this license header, choose License Headers in Project Properties. | |
| 3 | + * To change this template file, choose Tools | Templates | |
| 4 | + * and open the template in the editor. | |
| 5 | + */ | |
| 6 | +package sample01; | |
| 7 | + | |
| 8 | +import java.awt.BorderLayout; | |
| 9 | +import java.awt.Color; | |
| 10 | +import java.awt.Dimension; | |
| 11 | +import javax.swing.JFrame; | |
| 12 | +import javax.swing.JPanel; | |
| 13 | +import javax.swing.JScrollPane; | |
| 14 | +import javax.swing.JTable; | |
| 15 | +import javax.swing.table.DefaultTableModel; | |
| 16 | + | |
| 17 | +public class SampleJTable02Main extends JFrame { | |
| 18 | + private static final long serialVersionUID = -5294540159789000687L; | |
| 19 | + private static final String HEADER_TITLE[] = {"Column01", "Column02", "Column03", "Column04"}; | |
| 20 | + | |
| 21 | + public static void main(String[] args) { | |
| 22 | + SampleJTable02Main sampleJTable01 = new SampleJTable02Main("SampleJTable02"); | |
| 23 | + sampleJTable01.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| 24 | + sampleJTable01.setVisible(true); | |
| 25 | + } | |
| 26 | + | |
| 27 | + SampleJTable02Main(String title) { | |
| 28 | + this.setTitle(title); | |
| 29 | + this.setBounds(10, 10, 370, 120); | |
| 30 | + DefaultTableModel defaultTableModel = new DefaultTableModel(HEADER_TITLE, 5); | |
| 31 | + | |
| 32 | + for (int row = 0; row < 5; row++) { | |
| 33 | + defaultTableModel.setValueAt(row, row, 0); | |
| 34 | + } | |
| 35 | + | |
| 36 | + // テーブルモデルを指定してJTable生成 | |
| 37 | + JTable jTable = new JTable(defaultTableModel); | |
| 38 | + | |
| 39 | + // スクロールペインにはめる | |
| 40 | + JScrollPane jScrollPane = new JScrollPane(jTable); | |
| 41 | + jScrollPane.setPreferredSize(new Dimension(350, 70)); | |
| 42 | + JPanel jPanel = new JPanel(); | |
| 43 | + jPanel.add(jScrollPane); | |
| 44 | + | |
| 45 | + //フレームのContent Paneに部品セット | |
| 46 | + //this.getContentPane().add(jPanel, BorderLayout.CENTER); | |
| 47 | + this.add(jPanel, BorderLayout.CENTER); | |
| 48 | + | |
| 49 | + // DefaultTableCellRendererを継承した独自のレンダラをセット | |
| 50 | + SampleTableCellRenderer02 renderer = new SampleTableCellRenderer02(); | |
| 51 | + jTable.setDefaultRenderer(Object.class, renderer); | |
| 52 | + | |
| 53 | + renderer.setColor(0, 1, Color.yellow); | |
| 54 | + renderer.setColor(1, 3, Color.yellow); | |
| 55 | + | |
| 56 | + | |
| 57 | + } | |
| 58 | + | |
| 59 | +} |