PDA

View Full Version : tabelle in java


pinzi
23-03-2004, 12:07
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.

kingv
23-03-2004, 13:27
aggiungi:

this.getContentPane().add(table.getTableHeader(), BorderLayout.PAGE_START);

prima della add della table (nella jbInit)

pinzi
23-03-2004, 17:37
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();
}
});
}
}

kingv
24-03-2004, 08:54
uhm così ad occhio non saprei, puo' darsi che nel costruttore di JScrollPane lo faccia, quando ho tempo gli do un occhio :O

cn73
24-03-2004, 09:02
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:

Adding a Table to a Container
It's easy to put a table in a scroll pane. You need just one or two lines of code:
JScrollPane scrollPane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));

The scroll pane automatically gets the table's header, which displays the column names, and puts it on top of the table. Even when the user scrolls down, the column names remain visible at the top of the viewing area. The scroll pane also tries to make its viewing area the same as the table's preferred viewing size. The previous code snippet sets the table's preferred viewing size with the setPreferredScrollableViewportSize method.
If you're using a table without a scroll pane, then you must get the table header component and place it yourself. For example:

container.setLayout(new BorderLayout());
container.add(table.getTableHeader(), BorderLayout.PAGE_START);
container.add(table, BorderLayout.CENTER);

pinzi
24-03-2004, 11:12
Mi puoi dare il link a questo tutorial per le swing? E forse nel sito della SUN?

cn73
24-03-2004, 11:16
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/tutorial/uiswing/index.html

L'indice visuale: http://java.sun.com/docs/books/tutorial/uiswing/components/components.html