[英]How to use Jcombo + Listener
I'm french and newbie in Java.. I have a project to do concerning "regates" (races) of "voilier" (ship). 我是Java的法语和新手。我有一个项目涉及“专职”(种族)的“法规”(竞赛)。 There is a combo of regate and this must make a Jtable of ship where we can add time of the end of their race.
有一个组合,这必须使一个Jtable成为船舰,我们可以在其中增加比赛结束的时间。 My problem is that I want to make this Jtable when we click on one item of the combo but I don't know how to do..
我的问题是,当我们单击组合中的一项时,我想创建此Jtable,但是我不知道该怎么做。
My code : 我的代码:
package eole;
import java.awt.event.ItemEvent;
public class ArrivéesVoiliers extends JFrame implements ItemListener {
private JPanel contentPane;
private JTable tableArrivées;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ArrivéesVoiliers frame = new ArrivéesVoiliers();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ArrivéesVoiliers() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JComboBox combRegate = new JComboBox();
combRegate.setBounds(30, 25, 90, 20);
contentPane.add(combRegate);
ArrayList<Regate> lesReg = Application.getRegates();
for (Regate laReg : lesReg) {
combRegate.addItem(laReg.getNomReg());
}
combRegate.addItemListener(this);
String nom = (String) combRegate.getSelectedItem();
Regate regSelec = Application.getRegate(nom);
JLabel lblDateDep = new JLabel(regSelec.getDate());
lblDateDep.setBounds(130, 31, 60, 14);
contentPane.add(lblDateDep);
JLabel lblHeuredepart = new JLabel(regSelec.getTime());
lblHeuredepart.setBounds(200, 31, 60, 14);
contentPane.add(lblHeuredepart);
ArrayList<Voilier> voiliersPart = Application.getVoiliers(regSelec);
String[] entetes = { "Voiliers participants", " Classe", "Rating",
"Heure arrivée", "en seconde", "Abandon", "Stop Chrono",
"Ajout" };
DefaultTableModel voilPart = new DefaultTableModel();
voilPart.setColumnCount(8);
for (Voilier unVoil : voiliersPart) {
voilPart.addRow(new Object[] { unVoil.getNom(), unVoil.getNum(),
unVoil.getRating(), "Heure arrivée", "en seconde",
new Boolean(false), new Boolean(false), "Ajouter" });
}
new AbstractTableModel() {
public int getColumnCount() {
return 0;
}
public int getRowCount() {
return 0;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return null;
}
public boolean isCellEditable(int row, int col) {
if (col == 4) {
return true;
} else {
return false;
}
}
};
tableArrivées = new JTable(voilPart);
tableArrivées.setBounds(50, 228, 312, -125);
contentPane.add(tableArrivées);
}
@Override
public void itemStateChanged(ItemEvent e) {
}
}
代替使用EventListener
,应该使用ChangeListener
。
Change your itemStateChanged(ItemEvent e)
method in next way: 通过以下方式更改您的
itemStateChanged(ItemEvent e)
方法:
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
voilPart.addRow(new Object[] { "col1", "col2",
"col3", "col4", "col5",
new Boolean(false), new Boolean(false), "col8" });
}
}
Also I recommend you to: 我也建议您:
1)Use setLayout(null);
1)使用
setLayout(null);
and setBounds()
methods instead of the, try to use LayoutManager
, try to start from FlowLayout
and BorderLayout
it's really simple. 和
setBounds()
方法代替,尝试使用LayoutManager
,尝试从FlowLayout
和BorderLayout
开始,这确实很简单。
2)For setting size of your JFrame
use method pack()
; 2)对于设置
JFrame
大小,使用方法pack()
;
3)I think that entetes
it's column names create your TableModel like next 3)我认为很
entetes
的是,列名如下创建您的TableModel
DefaultTableModel voilPart = new DefaultTableModel(new Object[][]{},entetes);
4) Set voilPart
as instance variable for using it in itemStateChanged()
method for adding a new row. 4)将
voilPart
设置为实例变量,以便在itemStateChanged()
方法中使用它来添加新行。
5) This isn't important code, you can delete it, because it's local variable that never used : 5)这不是重要的代码,可以删除它,因为它是从未使用过的局部变量:
new AbstractTableModel() {
public int getColumnCount() {
return 0;
}
public int getRowCount() {
return 0;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return null;
}
public boolean isCellEditable(int row, int col) {
if (col == 4) {
return true;
} else {
return false;
}
}
};
6) add your table to JScrollPane
for scrolling. 6)将表格添加到
JScrollPane
进行滚动。
Usefull liks: 有用的提示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.