Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Refresh JTable
Giuseppa Cefalu
Ranch Hand
Posts: 121
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is the code.
setLayout(new BorderLayout()); panel = new JPanel(); frame = new JFrame(); try{ conn = getConnection(); stm = conn.createStatement(); rs = stm.executeQuery("SELECT *FROM TableNamesTable"); DefaultComboBoxModel model = new DefaultComboBoxModel(); model.addElement(""); while(rs.next()){ model.addElement(rs.getString(1)); } comboBox = new JComboBox(model); }catch(Exception e){ System.out.println(e.getMessage()); }finally{ try{ rs.close(); stm.close(); conn.close(); }catch(SQLException e){ System.out.println(e.getMessage()); } } comboBox.setBackground(Color.GRAY); comboBox.setForeground(Color.RED); txt = new JTextField(10); panel.add(comboBox, BorderLayout.NORTH); panel.add(txt); frame.add(panel); comboBox.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent ie){ String str = (String)comboBox.getSelectedItem(); txt.setText(str); } }); frame.setSize(400, 200); frame.setVisible(true);
Paul Clapham
Marshal
Posts: 28193
95
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I don't see any JTable in that example code. But that doesn't matter... the easiest way to change all the data in a JTable is to create a new instance of the table model and pass that to the JTable via its setModel() method.
Rob Spoor
Sheriff
Posts: 22783
131
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hasn't anybody ever told you to
UseCodeTags
before?
Anyway, you don't need to replace the JTable. You only need to replace the JTable's TableModel. Create a new one, and call
setModel
on the JTable.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Giuseppa Cefalu
Ranch Hand
Posts: 121
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
thanks! could you post an example pelase?
Paul Clapham
Marshal
Posts: 28193
95
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Example based on code from your original post:
comboBox.setModel(model);
Giuseppa Cefalu
Ranch Hand
Posts: 121
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thank you very much!
Giuseppa Cefalu
Ranch Hand
Posts: 121
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thank you! The code below works very well.
public class Combo { private Connection conn; private Statement stmt; private ResultSet rset; private JComboBox comboBox; private String databaseTable; private JFrame frame; private JPanel panel; private JTextField txt; private Dat dtable; private JTable table; private JScrollPane pane; public Combo(){ frame = new JFrame(); panel = new JPanel(); DefaultComboBoxModel model = new DefaultComboBoxModel(); try{ conn = getConnection(); stmt = conn.createStatement(); rset = stmt.executeQuery("SELECT *FROM tableNamesTable"); while(rset.next()){ model.addElement(rset.getString(1)); } }catch(Exception e){ System.out.println(e.getMessage()); }finally{ try{ rset.close(); stmt.close(); conn.close(); }catch(SQLException se){ System.out.println(se.getMessage()); } } comboBox = new JComboBox(model); txt = new JTextField(10); panel.add(comboBox, BorderLayout.NORTH); panel.add(txt); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Command: " + actionEvent.getActionCommand()); ItemSelectable is = (ItemSelectable)actionEvent.getSource(); System.out.println(", Selected: " + selectedString(is)); txt.setText(selectedString(is)); dtable = new Dat(selectedString(is)); table.setModel(dtable); } }; comboBox.addActionListener(actionListener); table = new JTable(dtable); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); pane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); pane.getViewport().setBackground(Color.WHITE); frame.add(pane, BorderLayout.CENTER); frame.add(panel,BorderLayout.NORTH); frame.setSize(400, 200); frame.setVisible(true);}
Their achilles heel is the noogie! Give them noogies tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
GUI Access Problems
Wanted: Advise on threads and application layout
JTable action listener
JComboBox stores same
Problem adding JComboBox to JFrame
More...