Lucky Luke Δημοσ. 4 Ιουλίου 2012 Δημοσ. 4 Ιουλίου 2012 (επεξεργασμένο) Προσπαθώ να κάνω χρήση JTable για να βελτιώσω σε μία παλιά εφαρμογή τον τρόπο με τον οποίο παρουσιάζονται τα δεδομένα που λαμβάνονται από μία βάση δεδομένων. Για το σκοπό αυτό έχω ένα combo box και ανάλογα με την επιλογή που κάνω επιστρέφονται άλλα δεδομένα με βάση κάποια προκαθορισμένα queries. Με λίγα λόγια θέλω το μοντέλο του JTable (πλήθος και ονόματα στηλών αλλά και πλειάδες δεδομένων) να αλλάζει δυναμικά ανάλογα με την επιλογή στο combo box. Όταν φτιάχνω και γεμίζω "καρφωτά" το JTable μέσα από τον κατασκευαστή της Demo κλάσης που έχω το JTable εμφανίζεται κανονικά. Όταν όμως πάω να το παράγω μέσα από τον listener handler (actionPerformed()) τότε δεν εμφανίζεται τίποτα... Είμαι πεπεισμένος ότι κάνω πολύ τραγικό λάθος αλλά δε μπορώ να δω τι ακριβώς. Παραθέτω ενδεικτικό κώδικα (είναι απλοποιημένος ώστε να μην περιλαμβάνει το κομμάτι της βάσης δεδομένων και να τρέχει standalone) αν μπορεί να βοηθήσει κάποιος. Έχω βάλει να εμφανίζεται το table (μία μόνο πλειάδα) όταν επιλέγουμε την "Customers" option στον listener. > package comboboxjtabledemo; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import java.util.Vector; public class ComboBoxJTableDemo extends JPanel implements ActionListener { JLabel picture; JTable table; JScrollPane scroll; String[] columnNames; DefaultTableModel model; public ComboBoxJTableDemo() { super(new BorderLayout()); String[] options = { "Customers", "Agencies", "Rooms", "Employees", "Services" }; //Create the combo box, select the item at index 1. JComboBox optionList = new JComboBox(options); optionList.setSelectedIndex(1); optionList.addActionListener(this); //Set up the picture. picture = new JLabel(); picture.setFont(picture.getFont().deriveFont(Font.ITALIC)); picture.setHorizontalAlignment(JLabel.CENTER); //updateLabel(options[optionList.getSelectedIndex()]); picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0)); //The preferred size is hard-coded to be the width of the //widest image and the height of the tallest image + the border. //A real program would compute this. picture.setPreferredSize(new Dimension(177, 122+10)); //Lay out the demo. add(optionList, BorderLayout.PAGE_START); add(picture, BorderLayout.PAGE_END); setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); /*TESTING code from within constructor*/ /* columnNames[0] = "ID"; columnNames[1] = "Name"; columnNames[2] = "Surname"; columnNames[3] = "Father name"; columnNames[4] = "Address"; olumnNames[5] = "Telephone"; columnNames[6] = "County"; columnNames[7] = "City"; model = new DefaultTableModel(columnNames, 0); Vector<String> row = new Vector<String>(); row.add("1"); row.add("John"); row.add("Johnakis"); row.add("Sr. John"); row.add("Adamantos 4"); row.add("0123456789"); row.add("Papua New Guinea"); row.add("Noland"); model.addRow(row); table = new JTable(model); scroll = new JScrollPane(table); add(scroll);*/ } /** Listens to the combo box. */ public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); String option = (String)cb.getSelectedItem(); if (option.equals("Customers")) { columnNames[0] = "ID"; columnNames[1] = "Name"; columnNames[2] = "Surname"; columnNames[3] = "Father name"; columnNames[4] = "Address"; columnNames[5] = "Telephone"; columnNames[6] = "County"; columnNames[7] = "City"; model = new DefaultTableModel(columnNames, 0); //0 data rows for the moment //add 1 record/row manually just for testing Vector<String> row = new Vector<String>(); row.add("1"); row.add("John"); row.add("Johnakis"); row.add("Sr. John"); row.add("Adamantos 4"); row.add("0123456789"); row.add("Papua New Guinea"); row.add("Noland"); model.addRow(row); table = new JTable(model); } else //just to prevent Null Pointer Exception { table = new JTable(); } scroll = new JScrollPane(table); add(scroll); table.updateUI(); } /** Returns an ImageIcon, or null if the path was invalid. */ protected static ImageIcon createImageIcon(String path) { java.net.URL imgURL = ComboBoxJTableDemo.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("ComboBoxDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. JComponent newContentPane = new ComboBoxJTableDemo(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } Επεξ/σία 5 Ιουλίου 2012 από Lucky Luke
pantpesl2 Δημοσ. 4 Ιουλίου 2012 Δημοσ. 4 Ιουλίου 2012 Στο τέλος της actionPerformed άλλαξε το table.updateUI(); με το this.updateUI();.
Lucky Luke Δημοσ. 4 Ιουλίου 2012 Μέλος Δημοσ. 4 Ιουλίου 2012 Το δοκίμασα αλλά δε δούλεψε. Ευχαριστώ πάντως για την απάντηση. Δούλεψε όμως ο παρακάτω κώδικας: > package comboboxjtabledemo; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import java.util.Vector; public class ComboBoxJTableDemo extends JPanel implements ActionListener { JLabel picture; JTable table; JScrollPane scroll; String[] columnNames; DefaultTableModel model; public ComboBoxJTableDemo() { super(new BorderLayout()); String[] options = { "Customers", "Agencies", "Rooms", "Employees", "Services" }; //Create the combo box, select the item at index 1. JComboBox optionList = new JComboBox(options); optionList.setSelectedIndex(1); optionList.addActionListener(this); //Set up the picture. picture = new JLabel(); picture.setFont(picture.getFont().deriveFont(Font.ITALIC)); picture.setHorizontalAlignment(JLabel.CENTER); //updateLabel(options[optionList.getSelectedIndex()]); picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0)); //The preferred size is hard-coded to be the width of the //widest image and the height of the tallest image + the border. //A real program would compute this. picture.setPreferredSize(new Dimension(177, 122+10)); //Lay out the demo. add(optionList, BorderLayout.PAGE_START); add(picture, BorderLayout.PAGE_END); setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); table = new JTable(model); scroll = new JScrollPane(table); add(scroll); } /** Listens to the combo box. */ public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); String option = (String)cb.getSelectedItem(); if (option.equals("Customers")) { columnNames = new String[8]; columnNames[0] = "ID"; columnNames[1] = "Name"; columnNames[2] = "Surname"; columnNames[3] = "Father name"; columnNames[4] = "Address"; columnNames[5] = "Telephone"; columnNames[6] = "County"; columnNames[7] = "City"; model = new DefaultTableModel(columnNames, 0); //0 data rows for the moment //add 1 record/row manually just for testing Vector<String> row = new Vector<String>(); row.add("1"); row.add("John"); row.add("Johnakis"); row.add("Sr. John"); row.add("Adamantos 4"); row.add("0123456789"); row.add("Papua New Guinea"); row.add("Noland"); model.addRow(row); table = new JTable(model); } else //just to prevent Null Pointer Exception { table = new JTable(); } scroll.setViewportView(table); } /** Returns an ImageIcon, or null if the path was invalid. */ protected static ImageIcon createImageIcon(String path) { java.net.URL imgURL = ComboBoxJTableDemo.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("ComboBoxDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. JComponent newContentPane = new ComboBoxJTableDemo(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } Επιγραμματικά κάνω την αρχικοποίηση σε JTable, JScrollPane (που το περιέχει) καθώς και το Model του JTable στον κατασκευαστή και μέσα στον handler στο τέλος του καλώ το >scroll.setViewportView(table); που κάνει κάτι σαν update. Αν και δεν κατάλαβα απόλυτα γιατί είναι απαραίτητο αυτό (γιατί όπως προανέφερα όταν το έφτιαχνα μέσα από τον κατασκευαστή δούλευε από "μόνο" του, χωρίς την παραπάνω κλήση), για κάποιο ωραίο λόγο δούλεψε. Αν γνωρίζει κανείς ας το μοιραστεί μαζί μας.
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα