Results 1 to 5 of 5
  1. #1
    selman555 is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Question Refreshing/repainting JPanel or JtextField after scrolling in database

    Hello,

    I'm a computing student and I need a little help figering out how to refresh a JPanel or some JTextFields after a JButton has called an ActionListener.
    This code will scroll through my database and put every rowinput in a JTextField in another panel.
    The question is, whenever I call the JButton while running the program, the JTextFields won't refresh autmatically (I don't think something is wrong with my code)
    Is someone able to figure out how to do this for me?
    Java Code:
        package Miniproject;  
          
        import java.awt.BorderLayout;  
        import java.awt.Color;  
        import java.awt.Dimension;  
        import java.awt.GridLayout;  
        import java.awt.event.ActionEvent;  
        import java.awt.event.ActionListener;  
        import java.awt.event.ComponentAdapter;  
        import java.awt.event.WindowEvent;  
        import java.awt.event.WindowListener;  
        import java.sql.ResultSet;  
        import java.sql.Statement;  
          
        import javax.swing.BorderFactory;  
        import javax.swing.Icon;  
        import javax.swing.ImageIcon;  
        import javax.swing.JButton;  
        import javax.swing.JFrame;  
        import javax.swing.JLabel;  
        import javax.swing.JPanel;  
        import javax.swing.JTable;  
        import javax.swing.JTextField;  
        import javax.swing.border.TitledBorder;  
          
        public class Frame2 extends JFrame implements WindowListener, ActionListener{  
            static int tellerFrame = 0;  
            static int tel = 0;  
            static Object[][] data; //tweedimensionele tabel  
            static JButton btnVolgende, btnLaatste, btnVorige, btnEerste;  
            private TitledBorder onderBorder, bovenBorder; //groupbox  
            private JPanel pnlOnderPaneel, pnlBovenPaneel;  
            private JLabel lblID, lblFilm, lblPrijsZonderKorting, lblDatumVertoning, lblTijdstipVertoning, lblKortingscode, lblVerklaring, lblKortingProcent;  
            JTextField txtID, txtFilm, txtPrijsZonderKorting, txtDatumVertoning, txtTijdstipVertoning, txtKortingscode, txtVerklaring, txtKortingProcent;  
          
            public Frame2() {  
                super("Opgave 5");  
                super.setLayout(new BorderLayout()); //defining the layout for the frame  
                tellerFrame++; //framecounter: prevent the startframe from opening the same frame twice.
          
                setSize(300,500);  
                setLocation(700,200);  
                setVisible(true);  
                addWindowListener(this);  
                  
                Connectie c = new Connectie();  //connection c = new connection();
                  
                //testing connection in if-statement
                if (c.maakConnectie()) { 
                    c.maakConnectie();  //Connection method (ODBC.JDBC driver(url)) is in an external class...
                    String[] kolomNamen = c.haalKolomNamenOp(); //Getting names of columns  from database
                    data = c.haalGegevensOp();  //geting data from database
                    JTable tblTabel = new JTable(data, kolomNamen);  
                      
                    //defining properties of the panels inside main panel
                    bovenBorder = BorderFactory.createTitledBorder("Koffiegegevens");  
                    bovenBorder.setTitleJustification(TitledBorder.LEFT);  
                      
                    onderBorder = BorderFactory.createTitledBorder("Navigatie");  
                    onderBorder.setTitleJustification(TitledBorder.LEFT);  
                      
                    //Making icons for buttons 
                    Icon f = new ImageIcon(getClass().getResource("Eerst.png"));  
                    Icon p = new ImageIcon(getClass().getResource("Vorig.png"));  
                    Icon n = new ImageIcon(getClass().getResource("Volgend.png"));  
                    Icon l = new ImageIcon(getClass().getResource("Laatst.png"));  
                      
                    //making buttons (new JButton) + icoon + (f)
                    btnEerste = new JButton(f);
                    //setting size of buttons to the size of the icons
                    btnEerste.setPreferredSize(new Dimension(40,40));
                    btnVorige = new JButton(p);
                    btnVorige.setPreferredSize(new Dimension(40,40));
                    btnVolgende = new JButton("Volgende", n);
                    btnVolgende.setPreferredSize(new Dimension(40,40));  
                    btnLaatste = new JButton(l);  
                    btnLaatste.setPreferredSize(new Dimension(40,40));  
                      
                    //defining labels 
                    lblID = new JLabel("ID", JLabel.LEFT);  
                    lblFilm = new JLabel("Film", JLabel.LEFT);  
                    lblPrijsZonderKorting = new JLabel("Prijs zonder korting",JLabel.LEFT);  
                    lblDatumVertoning = new JLabel("Datum vertoning", JLabel.LEFT);  
                    lblTijdstipVertoning = new JLabel("Tijdstip vertoning", JLabel.LEFT);  
                    lblKortingscode = new JLabel("Tijdstip vertoning", JLabel.LEFT);  
                    lblVerklaring = new JLabel("Verklaring", JLabel.LEFT);  
                    lblKortingProcent = new JLabel("Kortingspercentage", JLabel.LEFT);  
                      
                    //defining textfields
                    txtID = new JTextField(JTextField.RIGHT);  
                    //setting the length of the textfields  
                    txtID.setColumns(8);  
                    //Loading data in the textfields
                    txtID.setText(data[tel][0].toString());  
                    txtFilm = new JTextField(JTextField.RIGHT);  
                    txtFilm.setColumns(8);  
                    txtFilm.setText(data[tel][1].toString());  
                    txtPrijsZonderKorting = new JTextField(JTextField.RIGHT);  
                    txtPrijsZonderKorting.setColumns(8);  
                    txtPrijsZonderKorting.setText(data[tel][2].toString());  
                    txtDatumVertoning = new JTextField(JTextField.RIGHT);  
                    txtDatumVertoning.setColumns(8);  
                    txtDatumVertoning.setText(data[tel][3].toString());  
                    txtTijdstipVertoning = new JTextField(JTextField.RIGHT);  
                    txtTijdstipVertoning.setColumns(8);  
                    txtTijdstipVertoning.setText(data[tel][4].toString());  
                    txtKortingscode = new JTextField(JTextField.RIGHT);  
                    txtKortingscode.setColumns(8);  
                    txtKortingscode.setText(data[tel][4].toString());  
                    txtVerklaring = new JTextField(JTextField.RIGHT);  
                    txtVerklaring.setColumns(8);  
                    txtVerklaring.setText(data[tel][4].toString());  
                    txtKortingProcent = new JTextField(JTextField.RIGHT);  
                    txtKortingProcent.setColumns(8);  
                    txtKortingProcent.setText(data[tel][4].toString());  
                      
                    //defining panel(north)
                    pnlBovenPaneel = new JPanel();  
                    //Load properties (made earlier) of the subframes
                    pnlBovenPaneel.setBorder(bovenBorder);  
                    //giving gridLayout pnlBovenPaneel GridLayout(Rijen,kolommen,horizontal gap, vertical gap)  
                    pnlBovenPaneel.setLayout(new GridLayout(8,2,1,30));  
                      
                    //defining panel(south)
                    pnlOnderPaneel = new JPanel();  
                    //loading properties (made earlier)
                    pnlOnderPaneel.setBorder(onderBorder);  
                      
                    //adding buttons to the second panel  
                    pnlOnderPaneel.add(btnEerste);  
                    pnlOnderPaneel.add(btnVorige);  
                    pnlOnderPaneel.add(btnVolgende);  
                    pnlOnderPaneel.add(btnLaatste);  
                      
                    //adding labels and textfields to the upperpanel
                    pnlBovenPaneel.add(lblID);  
                    pnlBovenPaneel.add(txtID);  
                    pnlBovenPaneel.add(lblFilm);  
                    pnlBovenPaneel.add(txtFilm);  
                    pnlBovenPaneel.add(lblPrijsZonderKorting);  
                    pnlBovenPaneel.add(txtPrijsZonderKorting);  
                    pnlBovenPaneel.add(lblDatumVertoning);  
                    pnlBovenPaneel.add(txtDatumVertoning);  
                    pnlBovenPaneel.add(lblTijdstipVertoning);  
                    pnlBovenPaneel.add(txtTijdstipVertoning);  
                    pnlBovenPaneel.add(lblKortingscode);  
                    pnlBovenPaneel.add(txtKortingscode);  
                    pnlBovenPaneel.add(lblVerklaring);  
                    pnlBovenPaneel.add(txtVerklaring);  
                    pnlBovenPaneel.add(lblKortingProcent);  
                    pnlBovenPaneel.add(txtKortingProcent);  
                      
                    //setting backgroundcolor of subpanel(south)
                    pnlOnderPaneel.setBackground(Color.PINK);  
                      
                    //adding subpanels to the super-panel in a borderlayout
                    super.add(pnlOnderPaneel, BorderLayout.SOUTH);  
                    super.add(pnlBovenPaneel, BorderLayout.CENTER);  
                    c.sluitConnectie(); //closing the connection
                } else {  
                    Exception e = new Exception();  
                    e.printStackTrace();  
                    System.out.println("Connection could not be made."); //If connection failled, display this
                }  
            }  
            public void actionPerformed(ActionEvent e) {  
                if (e.getSource() == btnVolgende) {  
                    if (tel<7)  
                        tel++;  
                    else   
                        tel=0;  
                }             //defining a new number (tel) for the column number where the textfields should get their data from...
                if (e.getSource() == btnVorige) {  
                    if (tel>0)  
                        tel--;  
                    else  
                        tel=8;  
                }  
                if (e.getSource() == btnEerste)  
                    tel=0;  
                if (e.getSource() == btnLaatste)  
                    tel=7;  //7 rows in the database
                  
                //attempt to redefine the text in the textfields (but doesn't display while running)
                txtID.setText(data[tel][0].toString());  
                txtFilm.setText(data[tel][1].toString());  
                txtPrijsZonderKorting.setText(data[tel][2].toString());  
                txtDatumVertoning.setText(data[tel][3].toString());  
                txtTijdstipVertoning.setText(data[tel][4].toString());  
                txtKortingscode.setText(data[tel][5].toString());  
                txtVerklaring.setText(data[tel][6].toString());  
                txtKortingProcent.setText(data[tel][7].toString());  
                
                //attempts to refresh the upperpanel which holds the textfields:
                pnlBovenPaneel.repaint();  
                pnlBovenPaneel.revalidate();
                
                //An attempt that doesn't make sense to me...
                /*pnlBovenPaneel.add(txtID); 
                pnlBovenPaneel.add(txtFilm); 
                pnlBovenPaneel.add(txtPrijsZonderKorting); 
                pnlBovenPaneel.add(txtDatumVertoning); 
                pnlBovenPaneel.add(txtTijdstipVertoning); 
                pnlBovenPaneel.add(txtKortingscode); 
                pnlBovenPaneel.add(txtVerklaring); 
                pnlBovenPaneel.add(txtKortingProcent);*/  
                  
            }  
          
            @Override  
            public void windowActivated(WindowEvent e) {  
                // TODO Auto-generated method stub  
          
            }  
            @Override  
            public void windowClosed(WindowEvent e) {  
                // TODO Auto-generated method stub  
          
            }  
            @Override  
            public void windowClosing(WindowEvent e) {  
                // TODO Auto-generated method stub  
                tellerFrame=0;  
            }  
            @Override  
            public void windowDeactivated(WindowEvent e) {  
                // TODO Auto-generated method stub  
          
            }  
            @Override  
            public void windowDeiconified(WindowEvent e) {  
                // TODO Auto-generated method stub  
          
            }  
            @Override  
            public void windowIconified(WindowEvent e) {  
                // TODO Auto-generated method stub  
          
            }  
            @Override  
            public void windowOpened(WindowEvent e) {  
                // TODO Auto-generated method stub  
          
            }  
          
        }

    Please note that I'm an intermediate programmer so go easy on me
    ps. I know there are some things that I could leave out, like the JTable and some implemented stuff...

    Edit: added english comments to explain the dutch methodnames/variables.
    Last edited by selman555; 06-12-2011 at 12:03 AM. Reason: too many dutch descriptions

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Too much code to look at but in general if you updated a GUI after it is visible by adding or removing components from a panel then you need to do:

    Java Code:
    panel.add(...);
    panel.revalidate();
    panel.repaint();

  3. #3
    selman555 is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    @camickr:
    Thx for your reply...

    Sorry for the long code, but it should be simple to understand (I hope)
    I tried adding pnlBovenPaneel.revalidate() and pnlBovenPaneel.repaint() in the actionPerformed method,
    The problem isn't solved this way...

    You advised adding al the components to the panel, but the textfields are already in the panel.
    Does this mean that all the components will be added again? That's obviously not what i'm looking for

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If all you're doing is setting the text of a JTextField then nothing need be done to "update" the GUI as the JTextFields will display the text given to them. You've likely got a bug, and you'll need to debug your code either with a debugger or with a bunch of println statements. I'm afraid that your code is too large, complex and incomplete for me to debug it.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One more important thing: call setVisible(true) on your JFrame AFTER you've added all your components to the JFrame, and usually after you've packed the JFrame. So for instance with your code, I'd do all this stuff at the bottom of your constructor. i.e.,

    Java Code:
          } else {
             Exception e = new Exception();
             e.printStackTrace();
             System.out.println("Er kon geen verbinding gemaakt worden.");
          }
    
          // *** added ***      
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          pack();
          setLocationRelativeTo(null);
          setVisible(true);
       }
    Last edited by Fubarable; 06-12-2011 at 12:02 AM.

Similar Threads

  1. Replies: 4
    Last Post: 04-19-2011, 05:09 AM
  2. Question about repainting a JPanel
    By Nosrettap in forum Advanced Java
    Replies: 5
    Last Post: 01-02-2011, 08:29 PM
  3. JPanel and JLabels Not Repainting
    By phosphide in forum AWT / Swing
    Replies: 6
    Last Post: 11-13-2010, 03:17 AM
  4. JPanel not refreshing
    By pahiker in forum SWT / JFace
    Replies: 23
    Last Post: 07-14-2010, 02:59 PM
  5. Replies: 3
    Last Post: 04-20-2009, 11:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •