I'm in the process of making an application that keeps attendance. Once I have a list of students, how would I go about displaying them all and checking whether they are present or absent. I intially thought JTable would work, but it's not the easiest thing in the world to add swing components to a JTable. Is there an easier way to go about this? The image below shows roughly what I'm picturing, but if you have a different idea, feel free to share. Thanks!
-
_I intially thought JTable would work, but it's not the easiest thing in the world to add swing components to a JTable._ Why do you think so? – Amarnath Jan 24 '13 at 15:07
-
[`CheckOne`](http://stackoverflow.com/a/7920159/230513) is a basic example using `JChckbox`; some `JRadioButton` alternatives are cited [here](http://stackoverflow.com/a/11173600/230513). – trashgod Jan 24 '13 at 16:37
3 Answers
My suggestion is, use checkbox instead of two radio box controls .its look better .
And check the presented user .

- 120,458
- 37
- 198
- 307
-
-
1Just use a boolean value for the "present" column. The data will automatically displayed as a Checkbox. If the checkbox is not checked the student was absent – Hendrik Ebbers Jan 24 '13 at 15:13
-
@HendrikEbbers I think RadioButton looks promising than using CheckBox. – Amarnath Jan 24 '13 at 15:20
-
@Che Checkbox will come automatically if your column is Boolean,otherwise, will have to use the method given in your answer. – joey rohan Jan 24 '13 at 15:44
-
@joeyrohan I know that. Just write Boolean.class it will do the trick. But I am talking about the visibility. The _OP_ choice was good even it is not easy as check box. – Amarnath Jan 24 '13 at 16:16
AFAIK the first 3 columns look normal. But the fourth column is what I think you got stuck at.
Read about Renderer and Editor in JTable
.
I think rendering a JPanel
to the last column will do the trick. On that JPanel
add two JRadioButton
's.
One example of doing it is shown here and here.
P.S: I tried this approach, it works.
-
Thanks for the information. +1 I learned about Renders and Editors from your answer. However, I'm going to go with the cop-out solution provided by joey. – Joel Christophel Jan 25 '13 at 02:37
Here is an EG to show how you can use JTable
I have kept a boolean value for ABS/Present
coloumn, as suggested by @Hendrik Ebbers ,It will automatically come as a CheckBox
.
First you have to set the model of your JTabel
using fooTable.setModel(dataModel)
,
Something like this:
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"Joel", "Hails", new Integer(1011001), new Boolean(true)},
{"James", "Roggers", new Integer(1912212), null},
{"Rehy", "Gomes", new Integer(1121212), new Boolean(true)},
{"Sunil", "gawas", new Integer(9909090), null}
},
new String [] {
"1st name", "last name", "ID/NUMBER", "ABS/Present"
}
)
Where in new Object [][]
i have my all Data, and new String []
will have the column names.
Rest you can find more info on
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
Try yourself,Ask for the source code if you have any problems.
UPDATE:
you can try out this:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.SwingUtilities;
public class NewJFrame3 extends JPanel {
public NewJFrame3() {
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"1st Name",
"Last Name",
"ID/NUMBER",
"PRESENT"};
private Object[][] data = {
{"Joel", "Hails",
"110023", new Boolean(false)},
{"John", "Doe",
"343409", new Boolean(true)},
{"Sue", "well",
"899800", new Boolean(false)},
{"Jane", "White",
"990909", new Boolean(true)},
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
NewJFrame3 newContentPane = new NewJFrame3();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

- 3,505
- 5
- 33
- 70
-
I do have some problems. I tried doing what you did here (http://pastebin.com/u0w4tcLq). However, I got this result (http://imageshack.us/scaled/landing/407/jtable.png). No titles and no rendered check boxes. – Joel Christophel Jan 25 '13 at 04:16
-
I fixed the title problem by adding the table to a scroll pane, but there are still no check boxes. – Joel Christophel Jan 25 '13 at 04:33
-
@JoelA.Christophel Please update the full source code **here**.Don't provide links for that.And have you taken tutorial?Please copy paste your full source code here(i mean as an update in your Question). – joey rohan Jan 25 '13 at 04:54
-
-
@JoelA.Christophel please please look at the link i gave.It covers examples – joey rohan Jan 25 '13 at 15:16