Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [Java] JTable e JPopupMenu

    Ciao a tutti.

    Ho un problema. Non riesco a fare in modo che, selezionando una riga di una JTable con il tasto destro del mouse compaia un JPopupMenu

    Il MouseEvent non è proprio rilevato!!!!

    Qualcuno mi aiuti!!!!

    Please!

  2. #2
    Utente di HTML.it L'avatar di bobc
    Registrato dal
    Feb 2004
    Messaggi
    245
    Avendo poco tempo a disposizione, ti posso mandare questa classe , credo che tu possa prendere spunto da questa (Essa consiste in una tabella di componenti per computer, e per ordinare un singolo componente c'è il tasto Ordina nell'ultimo campo della JTable):

    import java.util.*;
    import javax.swing.table.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    class JTableButtonRenderer implements TableCellRenderer{
    private TableCellRenderer defaultrenderer;

    public JTableButtonRenderer(TableCellRenderer renderer){
    this.defaultrenderer=renderer;
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
    if(value instanceof Component){
    return (Component)value;
    }
    else{
    return this.defaultrenderer.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column);
    }
    }
    }

    public class TabellaMCliente extends AbstractTableModel{
    private String[]specifiche;
    public Map liste0;
    private Integer[]quant;
    private String[]code;
    private String[]mar;
    private String[]model;
    private String[]power;
    private Double[]price;
    //public JPanel[]panel;
    public JButton[]button;

    public TabellaMCliente(Map l){
    super();
    this.specifiche=new String[7];
    this.specifiche[0]="Quantita'";
    this.specifiche[1]="Codice";
    this.specifiche[2]="Marca";
    this.specifiche[3]="Modello";
    this.specifiche[4]="Potenzialita'";
    this.specifiche[5]="Prezzo";
    this.specifiche[6]="Ordina";
    this.liste0=l;
    this.quant=new Integer[this.liste0.size()];
    this.code=new String[this.liste0.size()];
    this.mar=new String[this.liste0.size()];
    this.model=new String[this.liste0.size()];
    this.power=new String[this.liste0.size()];
    this.price=new Double[this.liste0.size()];
    //this.panel=new JPanel[this.liste0.size()];
    this.button=new JButton[this.liste0.size()];
    int i=0;
    Iterator it=this.liste0.values().iterator();
    Componente com=new Componente();
    while(it.hasNext()){
    com=(Componente)it.next();
    this.quant[i]=new Integer(com.getQuantita());
    this.setValueAt(this.quant[i],i,0);
    this.code[i]=com.getCodice();
    this.setValueAt(this.code[i],i,1);
    this.mar[i]=com.getMarca();
    this.setValueAt(this.mar[i],i,2);
    this.model[i]=com.getModello();
    this.setValueAt(this.model[i],i,3);
    this.power[i]=com.getPotenzialita();
    this.setValueAt(this.power[i],i,4);
    this.price[i]=new Double(com.getPrezzo());
    this.setValueAt(this.price[i],i,5);
    this.button[i]=new JButton("Ordina");
    this.button[i].setEnabled(true);
    int q=((Integer)this.quant[i]).intValue();
    //this.button[i].addActionListener(new OrdinaListener(q,this.code[i],this.mar[i],this.model[i],this.power[i],String.valueOf(this.price[i]),"Monitor"));
    this.setValueAt(this.button[i],i,6);
    i++;
    }
    }

    public int getRowCount(){
    return this.liste0.size();
    }

    public int getColumnCount(){
    return this.specifiche.length;
    }

    public Object getValueAt(int table_row, int table_column){
    Object result=null;
    if(table_column==0){
    result=this.quant[table_row];
    }
    else if(table_column==1){
    result=this.code[table_row];
    }
    else if(table_column==2){
    result=this.mar[table_row];
    }
    else if(table_column==3){
    result=this.model[table_row];
    }
    else if(table_column==4){
    result=this.power[table_row];
    }
    else if(table_column==5){
    result=this.price[table_row];
    }
    else{
    result=this.button[table_row];
    }
    return result;
    }

    public boolean isCellEditable(int row, int column){
    return false;
    }

    public Class getColumnClass(int column){
    return getValueAt(0,column).getClass();
    }

    public String getColumnName(int column){
    return this.specifiche[column];
    }
    }

    class JTableButtonActionListener implements MouseListener{
    private int q;
    private String codice;
    private String marca;
    private String modello;
    private String potenzialita;
    private String prezzo;
    private String nome;
    private JTable table;
    private JFrame tFrame;
    private String file;

    public JTableButtonActionListener(JTable table, JFrame tf, String n, String file){
    this.table=table;
    this.tFrame=tf;
    this.nome=n;
    this.file=file;
    }

    public void forwardEventToButton(MouseEvent e){
    TableColumnModel model=table.getColumnModel();
    Object value;
    JButton button;

    int column=model.getColumnIndexAtX(e.getX());
    int row=e.getY()/table.getRowHeight();
    value=table.getValueAt(row,column);

    if(!(value instanceof JButton))
    return;

    button=(JButton)value;
    this.q=((Integer)table.getValueAt(row, column-6)).intValue();
    this.codice=(String)table.getValueAt(row, column-5);
    this.marca=(String)table.getValueAt(row, column-4);
    this.modello=(String)table.getValueAt(row, column-3);
    this.potenzialita=(String)table.getValueAt(row, column-2);
    this.prezzo=String.valueOf(table.getValueAt(row, column-1));

    //button.addActionListener(new ActionListener(){
    // public void actionPerformed(ActionEvent e){
    new OrdinaFrame(q,codice,marca,modello,potenzialita,pr ezzo,nome,file);
    this.tFrame.setVisible(false);
    // }
    //});
    }

    public void mouseClicked(MouseEvent e){
    this.forwardEventToButton(e);
    }

    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}

    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.