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

    Problema con jtable

    Nonostante abbia dato un'occhiata ai tutorial in rete, non riesco a capire come posso riempire una jtable creata con netBeans.
    Nel caso specifico, ho una tabella con tre colonne e una sola riga, in ognuna delle tre celle devo inserire uno dei seguenti valori (insomma, tre float):
    codice:
    float ist, min, max;
    
            ist=s.getConsumoIst();
            min=s.getConsumoMax();
            max=s.getConsumoMax();
    come si fa?

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2011
    Messaggi
    16
    Non ho mai usato NetBeans, ma alla fin fine non dovrebbe esserci nessuna differenza se si usa NetBeans, Eclipse o altro perché tutti usano javac per compilare e java per la fase di running.
    ecco qui un'esempio di JTable che ho scritto qualche tempo fa:
    codice:
    /**
    	 * This constructor builds the main skeleton of the table.
    	 */
    	public TableServers() {
    
    		listServers = new ArrayList<ServerInformation>();
    
    		model = new DefaultTableModel();
    		table = new JTable(model);
    		// generating columns
    		model.addColumn("HOSTNAME");
    		model.addColumn("PORT");
    		// this avoids multiple selections and listen to what is selected
    		table.setCellSelectionEnabled(true);
    
    		cellSelectionModel = table.getSelectionModel();
    		cellSelectionModel
    		.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    		// add header to the table
    		JTableHeader header = table.getTableHeader();
    		header.setBackground(Color.yellow);
    		// add table to a scroll-able pane
    		table.setGridColor(Color.black);
    		scrollPane = new JScrollPane(table);
    		scrollPane.setPreferredSize(new Dimension(TABLE_HEIGHT, TABLE_WIDTH));
    		add(scrollPane,  BorderLayout.NORTH);
    		
    		// updates the selected row !!!
    		check();
    		
    		// The connect button is used to connect with one of the selected servers. The default selected server is the first one.
    		button = new JButton("Connect");
    		ConnectToSelectedServer selectedServer = new ConnectToSelectedServer(this);
    		button.addActionListener(selectedServer);
    		add(button, BorderLayout.SOUTH);
    
    		pack();
    		setResizable(false);
    		setVisible(true);
    
    	}
    codice:
    // this methods helps to work only with the selected row
    	private void check() {
    		table.setCellSelectionEnabled(true);
    		cellSelectionModel = table.getSelectionModel();
    		cellSelectionModel
    		.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    		cellSelectionModel
    		.addListSelectionListener(new ListSelectionListener() {
    			public void valueChanged(ListSelectionEvent e) {
    				int selectedRow = table.getSelectedRow();
    				actualIndexTable = selectedRow;
    			}
    		});
    	}
    // Aggiunge una nuova riga alla tabella.
    codice:
    /**
    	 * Adds a new Server's 2-tuple to the table.
    	 * @param host
    	 * @param port
    	 */
    	protected void addElementsToTable(String host, int port) {
    
    		boolean isDuplicate = false;
    		// Add the elements to the list of servers and to the table. Checks if element already exists.
    		for (int i = 0; i < listServers.size(); i++) {
    			if((host.equals(listServers.get(i).getHost()) && (port == listServers.get(i).getPort()))) {
    				isDuplicate = true;
    			}
    		}
    		
    		// Add the new connection server to the list only if it's not duplicated.
    		if (!isDuplicate) {
    			// This is the crucial point for the user of the ArrayList listServers. In fact there are only different elements in the list.
    			listServers.add(new ServerInformation(host, port));
    			model.addRow(new Object[] {host, port});
    		}
    	}
    // Metodo per ottenere il valore selezionato per quella riga.

    codice:
    	/**
    	 * Gets the Servers that has been selected. 
    	 * @return ServerInformation
    	 */
    	protected ServerInformation getSelectedServer() {
    		
    		ServerInformation selectedServer;
    		selectedServer = new ServerInformation((String) table.getValueAt(actualIndexTable, 0), 
    				((Integer)table.getValueAt(actualIndexTable, 1)).intValue()) ;
    
    		return selectedServer;
    		
    	}

    // L'actionListener
    codice:
    /**
     * This class works as a bridge between the Connect class and the TableServers class.
     * @author simone
     *
     */
    public class ConnectToSelectedServer extends Connect implements ActionListener{
    
    	private TableServers table;
    
    	/**
    	 * It responds to the press of the button connect in the Table window.
    	 */
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    
    		String hostname = table.getSelectedServer().getHost();
    		int port  = table.getSelectedServer().getPort();
    		try {
    			standardConnection(hostname, port);
    		} catch (IOException e) {
    			JOptionPane.showMessageDialog(null, "I/O Exception. Failed to connecto to " + hostname, "Error !", JOptionPane.ERROR_MESSAGE);
    		}
    
    	}
    
    	/**
    	 * Constructor used to set the table field.
    	 * @param table
    	 */
    	public ConnectToSelectedServer(TableServers table) {
    		this.table = table;
    	}
    
    }

    // Questo è l'elenco delle variabili globali
    codice:
    private JTable table;
    	private DefaultTableModel model;
    	private JScrollPane scrollPane;
    	private ListSelectionModel cellSelectionModel;
    	private JButton button;
    	
    	// The JDialog Window's dimension.
    	private static final int TABLE_HEIGHT = 400;
    	private static final int TABLE_WIDTH = 300;
    
    	// Indicates the index of row selected in the table. It's 0 by default.
    	private int actualIndexTable = 0;
    	
    	// This stores the Servers to be passed to the Table.
    	protected ArrayList<ServerInformation> listServers;

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.