Java Jbutton执行更多操作
我有一个JDialog,其中显示了一个Jtable,Jtable的一个单元格是一个打开新JFrame(新)的JButton:Java Jbutton执行更多操作,java,swing,jframe,jbutton,jdialog,Java,Swing,Jframe,Jbutton,Jdialog,我有一个JDialog,其中显示了一个Jtable,Jtable的一个单元格是一个打开新JFrame(新)的JButton: publicsvoidbirthstats(字符串统计){ //System.out.println(classeMain); String titolo=新字符串(); 如果(“nascite”。等于(stat)){ titolo=lingua_corrente.getString(“Nassite_title”); elencojLabel.setText(titol
publicsvoidbirthstats(字符串统计){
//System.out.println(classeMain);
String titolo=新字符串();
如果(“nascite”。等于(stat)){
titolo=lingua_corrente.getString(“Nassite_title”);
elencojLabel.setText(titolo);
}
否则{
titolo=相关语言(anella);
elencojLabel.setText(titolo);
}
尺寸i=新尺寸(500700);
nascitejDialog.setSize(尺寸i);
setTitle(“GesAll 1.0-”+titolo);
setModal(true);
setModel(新的javax.swing.table.DefaultTableModel(
新对象[][]{
{null,null,null,null,null}
},
新字符串[]{
“Num”、lingua_corrente.getString(“铜版标签”)、lingua_corrente.getString(“annojLabel”)、lingua_corrente.getString(“jLabel8”)、lingua_corrente.getString(“纳斯塔标签”)
}
) {
类[]类型=新类[]{
java.lang.String.class、java.lang.String.class、java.lang.String.class、JButton.class、java.lang.String.class
};
boolean[]canEdit=新的boolean[]{
假,假,假,假,假,假
};
@凌驾
公共类getColumnClass(int columnIndex){
返回类型[列索引];
}
@凌驾
公共布尔值isCellEditable(int-rowIndex、int-columnIndex){
返回canEdit[columnIndex];
}
});
hatchlistijTable.setShowGrid(真);
hatchlistijTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
hatchlistijTable.getColumnModel().getColumn(0).setHeaderValue(“Num.”;//Numero
hatchlistijTable.getColumnModel().getColumn(0).SetResizeable(false);
hatchlistijTable.getColumnModel().getColumn(0).setPreferredWidth(60);
hatchlistijTable.getColumnModel().getColumn(1).setHeaderValue(lingua_corrente.getString(“CoppiLabel”);//coppia
hatchlistijTable.getColumnModel().getColumn(1).SetResizeable(false);
hatchlistijTable.getColumnModel().getColumn(1).setPreferredWidth(50);
hatchlistijTable.getColumnModel().getColumn(2).setHeaderValue(lingua_corrente.getString(“annojLabel”);//anno
hatchlistijTable.getColumnModel().getColumn(2).SetResizeable(false);
hatchlistijTable.getColumnModel().getColumn(2.setPreferredWidth(50);
hatchlistijTable.getColumnModel().getColumn(3).setHeaderValue(lingua_corrente.getString(“jLabel8”);//gabbia
hatchlistijTable.getColumnModel().getColumn(3).SetResizeable(false);
hatchlistijTable.getColumnModel().getColumn(3.setPreferredWidth(50);
如果(“nascite”。等于(stat)){
hatchlistijTable.getColumnModel().getColumn(4).setHeaderValue(lingua_corrente.getString(“schiusa”);//数据nascita prevista
}
否则{
hatchlistijTable.getColumnModel().getColumn(4).setHeaderValue(lingua_corrente.getString(“NascitaLabel”);//数据nascita
}
hatchlistijTable.getColumnModel().getColumn(4).SetResizeable(false);
hatchlistijTable.getColumnModel().getColumn(4.setPreferredWidth(150);
幼体可孵化。设置行高(25);
//hatchlistijTable.getColumnModel().getColumn(5).setHeaderValue(“跳转”);//salto
//hatchlistijTable.getColumnModel().getColumn(5).SetResizeable(false);
//hatchlistijTable.getColumnModel().getColumn(5.setPreferredWidth(50);
对于(int i=hatchlistijTable.getRowCount()-1;i>=0;i--)
((javax.swing.table.DefaultTableModel)hatchlistijTable.getModel()).removeRow(i);
String qDep=“从Deposizioni d中选择d,其中d.fecondo=“+””+“+”S“+”“+”和d.morto=“+””+“+”N“+”””“+”和d.dataSchiusa按d.dataDeposizione ASC的顺序为空”;
如果(!“nascite.”等于(stat))
qDep=“从Deposizioni d中选择d,其中d.morto=“+”“+”“+”N“+”“+”和d.anello为空“+”,d.dataSchiusa按d.dataSchiusa ASC的顺序不为空”;
List depo=DBgateway.getInstance()。运行查询(qDep);
System.out.println(depo.size());
//deposizionidd=(Deposizioni)DBgateway.getInstance().Run_query(qDep);
对于(int i=0;i=0&&column=0){
对象值=table.getValueAt(行、列);
if(JButton的值实例){
((JButton)值).doClick();
//if(((JButton)值).isEnabled())
//anella(行、列);
}
}
}
}
JTable显示正确我遇到的问题是,当我第一次单击Jbutton时,新的JFrame显示一次,如果我再次单击,它将显示7的3倍,以此类推。你能帮我只展示一次吗?
谢谢Gianluca。您不应该在TableModel中添加JButton。JTable使用渲染器和编辑器来显示数据
请参阅,以了解一种方法,该方法允许您在列中存储文本,但随后使用该列的自定义渲染器/编辑器,使其外观和行为类似于按钮。你所需要做的就是在点击单元格时提供一个动作。好的阅读:也要考虑感谢你的建议,我会记住并尝试跟随,改变我的代码,但是具体的问题是什么?peeskillet不是一个可读的例子,也许不是最小的遗憾。第二条评论的全部要点是,我喜欢大多数人太懒,或者不必花时间去看代码墙。因此,我建议您按照MCTRE链接的说明,创建一个最小的、可运行的示例来复制问题,我相信大多数人都会非常愿意提供帮助这是我尝试过的,但似乎什么都没有发生。
public void birthstats(String stat) {
//System.out.println(classeMain);
String titolo=new String();
if ("nascite".equals(stat)){
titolo=lingua_corrente.getString("nascite_title");
elencojLabel.setText(titolo);
}
else{
titolo=lingua_corrente.getString("anella");
elencojLabel.setText(titolo);
}
Dimension dimensioni = new Dimension(500, 700);
nascitejDialog.setSize(dimensioni);
nascitejDialog.setTitle("GesAll 1.0 - " + titolo);
nascitejDialog.setModal(true);
hatchlistijTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null}
},
new String [] {
"Num", lingua_corrente.getString("coppiaLabel"), lingua_corrente.getString("annojLabel"), lingua_corrente.getString("jLabel8"), lingua_corrente.getString("NascitaLabel")
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, JButton.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false
};
@Override
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
hatchlistijTable.setShowGrid(true);
hatchlistijTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
hatchlistijTable.getColumnModel().getColumn(0).setHeaderValue("Num."); //Numero
hatchlistijTable.getColumnModel().getColumn(0).setResizable(false);
hatchlistijTable.getColumnModel().getColumn(0).setPreferredWidth(60);
hatchlistijTable.getColumnModel().getColumn(1).setHeaderValue(lingua_corrente.getString("coppiaLabel")); //coppia
hatchlistijTable.getColumnModel().getColumn(1).setResizable(false);
hatchlistijTable.getColumnModel().getColumn(1).setPreferredWidth(50);
hatchlistijTable.getColumnModel().getColumn(2).setHeaderValue(lingua_corrente.getString("annojLabel")); //anno
hatchlistijTable.getColumnModel().getColumn(2).setResizable(false);
hatchlistijTable.getColumnModel().getColumn(2).setPreferredWidth(50);
hatchlistijTable.getColumnModel().getColumn(3).setHeaderValue(lingua_corrente.getString("jLabel8")); //gabbia
hatchlistijTable.getColumnModel().getColumn(3).setResizable(false);
hatchlistijTable.getColumnModel().getColumn(3).setPreferredWidth(50);
if ("nascite".equals(stat)){
hatchlistijTable.getColumnModel().getColumn(4).setHeaderValue(lingua_corrente.getString("schiusa")); //data nascita prevista
}
else{
hatchlistijTable.getColumnModel().getColumn(4).setHeaderValue(lingua_corrente.getString("NascitaLabel")); //data nascita
}
hatchlistijTable.getColumnModel().getColumn(4).setResizable(false);
hatchlistijTable.getColumnModel().getColumn(4).setPreferredWidth(150);
hatchlistijTable.setRowHeight(25);
//hatchlistijTable.getColumnModel().getColumn(5).setHeaderValue("Jump"); //salto
//hatchlistijTable.getColumnModel().getColumn(5).setResizable(false);
//hatchlistijTable.getColumnModel().getColumn(5).setPreferredWidth(50);
for (int i=hatchlistijTable.getRowCount()-1;i>=0;i--)
((javax.swing.table.DefaultTableModel) hatchlistijTable.getModel()).removeRow(i);
String qDep = "SELECT d FROM Deposizioni d WHERE d.fecondo=" + '"' + 'S' +'"' +" AND d.morto=" + '"' + 'N' +'"' + " AND d.dataSchiusa is null ORDER by d.dataDeposizione ASC";
if (!"nascite".equals(stat))
qDep = "SELECT d FROM Deposizioni d WHERE d.morto=" + '"' + 'N' +'"' + " AND d.anello is null" + " AND d.dataSchiusa is not null ORDER by d.dataSchiusa ASC";
List depo = DBgateway.getInstance().Run_query(qDep);
System.out.println(depo.size());
//Deposizioni dd = (Deposizioni) DBgateway.getInstance().Run_query(qDep);
for (int i=0;i<depo.size();i++){
//Deposizioni dd = (Deposizioni) DBgateway.getInstance().Run_query(qDep).get(i);
Deposizioni dd = (Deposizioni) depo.get(i);
int toAdd = Program.getConf().getSchiusa();
Calendar cal = Calendar.getInstance();
if ("nascite".equals(stat)){
cal.setTime(dd.getDataDeposizione());
}
else {
toAdd = Program.getConf().getAnellare();
cal.setTime(dd.getDataSchiusa());
}
Calendar today=Calendar.getInstance();
cal.add(Calendar.DATE, toAdd); // add n days
//if(today.after(cal)){
System.out.println(toAdd + " " + today.after(cal) +" " + "nascite".equals(stat));
Object [] rowData=getRowData(dd,i, cal,stat);
if("nascite".equals(stat))
((javax.swing.table.DefaultTableModel) hatchlistijTable.getModel()).addRow(rowData);
else
if(today.after(cal) && !"nascite".equals(stat))
((javax.swing.table.DefaultTableModel) hatchlistijTable.getModel()).addRow(rowData);
}
TableCellRenderer buttonRenderer = new JTableButtonRenderer();
hatchlistijTable.getColumn(lingua_corrente.getString("jLabel8")).setCellRenderer(buttonRenderer);
hatchlistijTable.addMouseListener(new JTableButtonMousePriListener(hatchlistijTable));
nascitejDialog.setVisible(true);
}
private Object[] getRowData(Deposizioni dd,int i, Calendar cal, final String stat) {
try {
String qCp="SELECT c FROM Coppie c WHERE c.idCoppia=" + findDatiCova(dd.getIdDeposizione()).getIdCoppia();
Coppie rCp=(Coppie) DBgateway.getInstance().Run_query(qCp).get(0);
String data = df.format(cal.getTime());
//Bottone salto cova
final JButton jump=new JButton(rCp.getGabbia().toString());
jump.addActionListener(new java.awt.event.ActionListener() {
private Object classeMain = this.getClass();
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jumpActionPerformed(evt);
}
private void jumpActionPerformed(ActionEvent evt) {
System.out.println("jump" +jump.getText().toString());
String qS ="SELECT c.idCoppia from Coppie c WHERE c.attiva=" + "'"+"S"+"'" + " AND c.gabbia="+jump.getText().toString();
CoveNew cova = new CoveNew(mainClass, (Integer) DBgateway.getInstance().Run_query(qS).get(0),stat);
cova.setVisible(true);
nascitejDialog.setVisible(false);
mainClass.setVisible(false);
}
});
//
Object [] rowData = {i+1, // progressivogianluca
rCp.getNumeroCoppia(), // numero coppia
rCp.getAnno(),
//rCp.getGabbia(), // fecondo
jump,
data};
return(rowData);
} catch (IOException ex) {
Logger.getLogger(Main_menu.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
private static class JTableButtonMousePriListener extends MouseAdapter {
private final JTable table;
public JTableButtonMousePriListener(JTable table) {
this.table = table;
}
@Override public void mouseClicked(MouseEvent e) {
int column = table.getColumnModel().getColumnIndexAtX(e.getX());
int row = e.getY()/table.getRowHeight();
if (row < table.getRowCount() && row >= 0 && column < table.getColumnCount() && column >= 0) {
Object value = table.getValueAt(row, column);
if (value instanceof JButton) {
((JButton)value).doClick();
//if(((JButton)value).isEnabled())
// anella(row,column);
}
}
}
}