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?
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.