|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Oct 2003
Città: Umbertide
Messaggi: 180
|
tabelle in java
Salve a tutti
il seguente programma dovrebbe creare un dialogo in cui sia visibile una tabella in cui i nomi delle colonne sono evidenziati(il programma è diviso in due file): //file:: Main_Program.java package condensa; import javax.swing.UIManager; import java.awt.*; public class Main_Program { boolean packFrame = false; public Main_Program() { Frame1 frame = new Frame1(); if (packFrame) { frame.pack(); } else { frame.validate(); } Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.setVisible(true); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { e.printStackTrace(); } new Main_Program(); } } //file:: Frame1.java package condensa; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.border.*; public class Frame1 extends JFrame { JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout(); String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)} }; final JTable table = new JTable(data, columnNames); TitledBorder titledBorder1; TitledBorder titledBorder2; public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); titledBorder1 = new TitledBorder(""); titledBorder2 = new TitledBorder(""); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("Frame Title"); this.getContentPane().add(table, BorderLayout.CENTER); } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } } Il programma in effetti traccia la tabella descritta nel codice tuttavia non vengono mostrati i nomi delle tabelle. Qualcuno sa dirmi dov'è l'errore? Saluti Enrico. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Jan 2001
Città: Milano
Messaggi: 5707
|
aggiungi:
this.getContentPane().add(table.getTableHeader(), BorderLayout.PAGE_START); prima della add della table (nella jbInit) |
|
|
|
|
|
#3 |
|
Member
Iscritto dal: Oct 2003
Città: Umbertide
Messaggi: 180
|
Hai ragione con questa linea di codice riesco a visualizzare i nomi delle colonne. Ma allora perchè nel seguente programma non è necassario utilizzare il metodo che tu mi hai suggerito?
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class SimpleTableDemo extends JPanel { private boolean DEBUG = false; public SimpleTableDemo() { super(new GridLayout(1,0)); String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)} }; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); //Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); //Add the scroll pane to this panel. add(scrollPane); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("SimpleTableDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SimpleTableDemo newContentPane = new SimpleTableDemo(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Jan 2001
Città: Milano
Messaggi: 5707
|
uhm così ad occhio non saprei, puo' darsi che nel costruttore di JScrollPane lo faccia, quando ho tempo gli do un occhio
|
|
|
|
|
|
#5 | |
|
Senior Member
Iscritto dal: Jul 1999
Città: Torino
Messaggi: 2221
|
Perchè nel secondo esempio aggiungi la tabella a un JScrollPane e poi aggiungi quest0ultimo al contentPane del Frame...
come CHIARAMENTE spiegato nel Tutorial delle Swing: Quote:
|
|
|
|
|
|
|
#6 |
|
Member
Iscritto dal: Oct 2003
Città: Umbertide
Messaggi: 180
|
Mi puoi dare il link a questo tutorial per le swing? E forse nel sito della SUN?
|
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Jul 1999
Città: Torino
Messaggi: 2221
|
E' linkato anche nelle API della JTable. E' linkato nei topic in rilievo.
Cmq tutti i tutorial sono raggiungibili da http://java.sun.com/docs/books/tutorial/index.html Quello sulle swing: http://java.sun.com/docs/books/tutor...ing/index.html L'indice visuale: http://java.sun.com/docs/books/tutor...omponents.html |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 05:27.



















