samedi 23 avril 2016

Checkbox wont insert into jtable as column. JTable not showing up on JFrame [duplicate]

This question already has an answer here:

I've been trying to put checkboxes as a column in a table and have a question assigned to it. However, the checkboxes dont appear in the table and instead i get an error where the checkbox is supposed to be

(
javax.swing.JCheckBox[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1f3d186,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,right=2],paintBorder=false,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Question]
).

All the text in "Col1" come out fine.

I also can't have the table and the button up at the same time, because i think the overlays the table. I tried to find to solution for this but to no avail.

Could someone please kindly help me find where the error in my code is, i would really appreciate it.

  DefaultTableModel model = new DefaultTableModel(){

      public Class<?> getColumnClass(int column){
          switch(column)
          {
          case 0:
              return Boolean.class;
          case 1:
              return String.class;

              default:
                  return String.class;
          }
      }

  };

  JTable Table = new JTable(model);
  // Assign the model to the table
  Table.setModel(model);
  model.addColumn("Select?");
  model.addColumn("Col1");

  for(String temp: text){
      JCheckBox b = new JCheckBox(temp);
      b.setSelected(false);
      model.addRow(new Object[] {b,temp});
  }


  JButton Ok_Button = new JButton("OK");
  Ok_Button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {

        for(int i = 0; i < Table.getRowCount(); i++){
            Boolean checked = Boolean.valueOf(Table.getValueAt(i,0).toString());
            String col = Table.getValueAt(i,1).toString();

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

  JFrame Frame = new JFrame();


  Table.setBounds(0,0,200,200);
  Table.setVisible(true);
  Frame.add(new JScrollPane(Table), BorderLayout.PAGE_START);
  Ok_Button.setSize(90, 39);
  Frame.getContentPane().setLayout(null);
  Frame.add(Ok_Button, BorderLayout.PAGE_END);

  Frame.setBounds(10,10,704,514);

  Frame.setVisible(true);
  Frame.setLocationRelativeTo(null);

}




Aucun commentaire:

Enregistrer un commentaire