التعليم السعودي

الطرق التي يمكن فيها جعل المستخدم يقوم بتحديد القيم الموجودة في الـ JTable

الطرق التي يمكن فيها جعل المستخدم يقوم بتحديد القيم الموجودة في الـ JTable
الطرق التي يمكن فيها جعل المستخدم يقوم بتحديد القيم الموجودة في الـ JTable
الطرق التي يمكن فيها جعل المستخدم يقوم بتحديد القيم الموجودة في الـ JTable
الطرق التي يمكن فيها جعل المستخدم يقوم بتحديد القيم الموجودة في الـ JTable
الطرق التي يمكن فيها جعل المستخدم يقوم بتحديد القيم الموجودة في الـ JTable

المناهج السعودية

الطرق التي يمكن فيها جعل المستخدم يقوم بتحديد القيم الموجودة في الـ JTable

المثال التالي يعلمك الطرق التي يمكن فيها جعل المستخدم يقوم بتحديد القيم الموجودة في الجدول.

مثال

Main.java

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.ButtonGroup;
  6. import javax.swing.JRadioButton;
  7. import javax.swing.ListSelectionModel;
  8. import javax.swing.JTable;
  9. import javax.swing.table.DefaultTableModel;
  10. import javax.swing.JScrollPane;
  11. publicclass Main {
  12. // هنا قمنا بإنشاء النافذة و جميع الأشياء التي سنضعها فيها
  13. static JFrame frame = newJFrame(“JTable demo”);
  14. static DefaultTableModel model = newDefaultTableModel();
  15. static JTable table = newJTable( model );
  16. static JScrollPane tableScroller = newJScrollPane( table );
  17. static JLabel label_1 = newJLabel(“Selection Mode”);
  18. static JRadioButton radioButton_mode_1 = newJRadioButton(“Multiple Selection”true);
  19. static JRadioButton radioButton_mode_2 = newJRadioButton(“Interval Selection”);
  20. static JRadioButton radioButton_mode_3 = newJRadioButton(“Single Selection”);
  21. static JLabel label_2 = newJLabel(“Selection Option”);
  22. static JRadioButton radioButton_option_1 = newJRadioButton(“Row Selection”true);
  23. static JRadioButton radioButton_option_2 = newJRadioButton(“Column Selection”);
  24. static JRadioButton radioButton_option_3 = newJRadioButton(“Cell Selection”);
  25. publicstaticvoidmain(String[] args){
  26. // الخاصة فيه model هنا قمنا بتخزين أسماء أعمدة الجدول و القيم التي ستظهر فيه ثم وضعناها في الـ
  27. String columns[] = {“ID”“First Name”“Last Name”“Phone”};
  28. String data[][] = {
  29. {“1”“Mhamad”“Harmush”“70123456”},
  30. {“2”“Ahmad”“Mousally”“76454532”},
  31. {“3”“Hala”“Hassan”“03555124”},
  32. {“4”“Rim”“Al Mouhandes”“01321123”},
  33. {“5”“Said”“Al Kurdy”“07445599”},
  34. {“6”“Abdullah”“Saadi”“71001234”},
  35. {“7”“Ibrahim”“Marhaba”“04555666”},
  36. {“8”“Omar”“El Koussa”“01357894”},
  37. {“9”“Riad”“Asaad”“76887123”},
  38. {“10”“Rawan”“Hoblos”“01200500”},
  39. {“11”“Naya”“Asmar”“03654123”},
  40. {“12”“Mahmoud”“Mawwas”“70040300”},
  41. {“13”“Elena”“Shbib”“71199113”}
  42. };
  43. model.setDataVector(data, columns);
  44. // في مجموعة واحدة حتى يستطيع المستخدم إختيار واحدة منهم Radio Buttons هنا قمنا بوضع أول ثلاثة
  45. ButtonGroup group_1 = newButtonGroup();
  46. group_1.add(radioButton_mode_1);
  47. group_1.add(radioButton_mode_2);
  48. group_1.add(radioButton_mode_3);
  49. // في مجموعة واحدة حتى يستطيع المستخدم إختيار واحدة منهم Radio Buttons هنا قمنا بوضع ثاني ثلاثة
  50. ButtonGroup group_2 = newButtonGroup();
  51. group_2.add(radioButton_option_1);
  52. group_2.add(radioButton_option_2);
  53. group_2.add(radioButton_option_3);
  54. // frame هنا قمنا بتحديد أماكن الأشياء التي سنضيفها في الـ
  55. tableScroller.setBounds(00585200);
  56. label_1.setBounds(2022015020);
  57. label_2.setBounds(20022015020);
  58. radioButton_mode_1.setBounds(2025015020);
  59. radioButton_mode_2.setBounds(2028015020);
  60. radioButton_mode_3.setBounds(2031015020);
  61. radioButton_option_1.setBounds(20025015020);
  62. radioButton_option_2.setBounds(20028015020);
  63. radioButton_option_3.setBounds(20031015020);
  64. // frame هنا قمنا بإضافة جميع الأشياء التي قمنا بتعريفها سابقاً في الـ
  65. frame.add(tableScroller);
  66. frame.add(label_1);
  67. frame.add(label_2);
  68. frame.add(radioButton_mode_1);
  69. frame.add(radioButton_mode_2);
  70. frame.add(radioButton_mode_3);
  71. frame.add(radioButton_option_1);
  72. frame.add(radioButton_option_2);
  73. frame.add(radioButton_option_3);
  74. // frame هنا قمنا بتحديد خصائص الـ
  75. frame.setSize(600400);
  76. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  77. frame.setLayout(null);
  78. frame.setVisible(true);
  79. // radioButton_mode_1 هنا وضعنا الأوامر التي نريد تنفيذها عند النقر على الـ
  80. radioButton_mode_1.addActionListener(newActionListener(){
  81. @Override
  82. publicvoidactionPerformed(ActionEvent e)
  83. {
  84. table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  85. }
  86. });
  87. // radioButton_mode_2 هنا وضعنا الأوامر التي نريد تنفيذها عند النقر على الـ
  88. radioButton_mode_2.addActionListener(newActionListener(){
  89. @Override
  90. publicvoidactionPerformed(ActionEvent e)
  91. {
  92. table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
  93. }
  94. });
  95. // radioButton_mode_3 هنا وضعنا الأوامر التي نريد تنفيذها عند النقر على الـ
  96. radioButton_mode_3.addActionListener(newActionListener(){
  97. @Override
  98. publicvoidactionPerformed(ActionEvent e)
  99. {
  100. table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  101. }
  102. });
  103. // radioButton_option_1 هنا وضعنا الأوامر التي نريد تنفيذها عند النقر على الـ
  104. radioButton_option_1.addActionListener(newActionListener(){
  105. @Override
  106. publicvoidactionPerformed(ActionEvent e)
  107. {
  108. table.setRowSelectionAllowed(true);
  109. table.setColumnSelectionAllowed(false);
  110. }
  111. });
  112. // radioButton_option_2 هنا وضعنا الأوامر التي نريد تنفيذها عند النقر على الـ
  113. radioButton_option_2.addActionListener(newActionListener(){
  114. @Override
  115. publicvoidactionPerformed(ActionEvent e)
  116. {
  117. table.setRowSelectionAllowed(false);
  118. table.setColumnSelectionAllowed(true);
  119. }
  120. });
  121. // radioButton_option_3 هنا وضعنا الأوامر التي نريد تنفيذها عند النقر على الــ
  122. radioButton_option_3.addActionListener(newActionListener(){
  123. @Override
  124. publicvoidactionPerformed(ActionEvent e)
  125. {
  126. table.setCellSelectionEnabled(true);
  127. }
  128. });
  129. }
  130. }

ستظهر لك النافذة التالية عند التشغيل.
لتحديد أكثر من قيمة في الجدول أبق إصبعك على الزر Ctrl أثناء التحديد.

الطرق التي يمكن فيها جعل المستخدم يقوم باختيار القيم الموجودة في الجدول JTable في جافا

 
المصدر: الطرق التي يمكن فيها جعل المستخدم يقوم بتحديد القيم الموجودة في الـ JTable – المناهج السعودية

مقالات ذات صلة

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

زر الذهاب إلى الأعلى