개발바닥곰발바닥
Published 2021. 5. 18. 06:10
JTable에 JCheckBox 넣기 JAVA/Swing
728x90
mport java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class MyTable extends JFrame{

  //MAIN METHOD
  public static void main(String[] args)
  {

       EventQueue.invokeLater(new Runnable()
       {
           public void run()
           {
               //INITIALIZE JFRAME FORM
               MyTable form=new MyTable();
               form.setVisible(true);;
           }
       });

  }

  //CONSTRUCTOR
  public MyTable()
  {
    //the form
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(200,200,800,300);
    setTitle("ProgrammingWizards Channel");
    getContentPane().setLayout(null);

    //ADD SCROLLPANE
    JScrollPane scroll=new JScrollPane();
    scroll.setBounds(70,80,600,200);
    getContentPane().add(scroll);

    //THE TABLE
    final JTable table=new JTable();
    scroll.setViewportView(table);

    //THE MODEL OF OUR TABLE
    DefaultTableModel model=new DefaultTableModel()
    {
      public Class<?> getColumnClass(int column)
      {
        switch(column)
        {
        case 0:
          return Boolean.class;
        case 1:
          return String.class;
        case 2:
          return String.class;
        case 3:
          return String.class;
        case 4:
          return String.class;

          default:
            return String.class;
        }
      }
    };

    //ASSIGN THE MODEL TO TABLE
    table.setModel(model);

    model.addColumn("Select");
    model.addColumn("Position");
    model.addColumn("Team");
    model.addColumn("Points");
    model.addColumn("Manager");

    //THE ROW
    for(int i=0;i<=7;i++)
    {
      model.addRow(new Object[0]);
      model.setValueAt(false,i,0);
      model.setValueAt("Our Row"+(i+1), i, 1);
      model.setValueAt("Our Column 2", i, 2);
      model.setValueAt("Our Column 3", i, 3);
      model.setValueAt("Our Column 4", i, 4);
    }

    //OBTAIN SELECTED ROW
    JButton btn=new JButton("Get Selected");
    btn.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

        //GET SELECTED ROW
        for(int i=0;i<table.getRowCount();i++)
        {
          Boolean checked=Boolean.valueOf(table.getValueAt(i, 0).toString());
          String col=table.getValueAt(i, 1).toString();

          //DISPLAY
          if(checked)
          {
            JOptionPane.showMessageDialog(null, col);
          }
        }

      }
    });

    //ADD BUTTON TO FORM
    btn.setBounds(20,30,130,30);
    getContentPane().add(btn);
  }

}

DefaultModel에 함수 하나만 넣어주면 깔끔하게 된다..

출처 : https://camposha.info/java-jtable-render-checkbox-column/

 

 

728x90

'JAVA > Swing' 카테고리의 다른 글

JTextPane 텍스트 가운데 정렬  (0) 2021.05.30
profile

개발바닥곰발바닥

@bestinu

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!