473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Java swings

1
This is my code, i want to know by adding or modifying what can i have my code refresh the JTable everytime its accessed.

package library2;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class home extends JFrame {

protected static final ListSelectionEvent ListSelectionEvent = null;

static JFrame mainfram = new JFrame("welcome to library");

static JFrame f2 = new JFrame("LIST OF THE BOOKS");
static JButton b1 = new JButton("Add A New Book");
static JButton b2 = new JButton("Modify The Book Details");
static JButton b3 = new JButton("List Of Available Books");
static JButton lihp = new JButton("HOMEPAGE");
static JLabel l1 = new JLabel(
".............WELCOME TO LIBRARY..............", JLabel.CENTER);
static JLabel l2 = new JLabel("HAVE A GREAT DAY :)))", JLabel.CENTER);
static JPanel p1 = new JPanel(new GridLayout(5, 3, 40, 40));

public home() {
p1.setVisible(true);
b1.setSize(50, 100);

mainfram.setSize(800, 400);
mainfram.add(p1);
mainfram.setVisible(true);

// f2.add(lihp);

}

public static void main(String args[]) {
{

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mainfram.setVisible(false);
mainfram.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);

Addingbook ab = new Addingbook();
// ab.setVisible(true);
// ab.setSize(800, 400);

}

});

b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mainfram.setVisible(false);
mainfram.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);

listing li = new listing();
// li.setVisible(true);
// li.setSize(800, 400);

}

});

b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

Vector columnNames = new Vector();
Vector data = new Vector();
data.clear();
JPanel p = new JPanel(); /* (new GridLayout(2,1,10,10) */
p.setBounds(0, 0, 400, 300);
JLabel l = new JLabel();

try {

// Connection con =
// DriverManager.getConnection("Jdbc:Odbc:test");

String sql = "select *from books";
String cs = "jdbc:mysql://localhost:3306/test";
String user = "root";
String pwd = "thbs123!";
Class.forName("com.mysql.jdbc.Driver").newInstance ();
Connection con = DriverManager.getConnection(cs, user,
pwd);

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
// md.getServerExecutions();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(md.getColumnName(i));
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rs.getObject(i));

}
data.addElement(row);
}
rs.close();
stmt.close();
} catch (Exception e1) {
System.out.println(e1);
}

JTable table = new JTable(data, columnNames);

//data = getServerExecutions();
//table.fireTableDataChanged();
// table.getModel().setModel();
// table.valueChanged(ListSelectionEvent);




//data(set);
// columnNames();

TableColumn col;
for (int i = 0; i < table.getColumnCount(); i++) {
col = table.getColumnModel().getColumn(i);
col.setMaxWidth(999);

}

JScrollPane scrollPane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(750,
275));
p.setVisible(true);
p.add(l);

p.add(scrollPane);
// p.add(lihp);

f2.add(p);

f2.setSize(800, 400);
f2.setVisible(true);

}
});
f2.add(lihp);
lihp.setBounds(320, 320, 120, 30);
lihp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

mainfram.setVisible(true);
f2.setVisible(false);
// f2.setDefaultCloseOperation(EXIT_ON_CLOSE);

}
});

mainfram.setLayout(new FlowLayout());
// mainfram.imageUpdate(Blue, arg1, arg2, arg3, arg4, arg5)
mainfram.setVisible(true);
mainfram.setSize(800, 400);
p1.add(new JLabel(""));
p1.add(l1);
p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(b1);

p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(b2);
p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(b3);
p1.add(new JLabel(""));
p1.add(new JLabel(""));
p1.add(l2);
p1.add(new JLabel(""));
mainfram.add(p1);

// f2.setLayout(new FlowLayout());

f2.setResizable(false);

}

}

public void setVisible(boolean b) {
// TODO Auto-generated method stub

}
}
Nov 28 '11 #1
1 2281
if you want to have your table reflect changes once the user manipulates it, you can apply the observer pattern

http://en.wikipedia.org/wiki/Observer_pattern

or you can simply call update() or updateUI() on your JTable every time the user manipulates the table.

i would also recommend that you refactor some of your code into separate classes so it's a little more organized. For example, i would have one class that deals with sql connections, another class for database manipulation, another class for user interface creation and another class for handling what happens when the action listeners are clicked.

hope this helps
Dec 4 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Arvinder Channey | last post by:
Hello Sir!, i could not find any information on how to make a non-rectangular Frame in JAVA swings.For that i think one has to write own new Frame container,then how to do this?.. If anybody...
0
by: navjyot | last post by:
Hi everybody I am using eclipse 3.1.0 version.I am new to it.Actually i want to run few screens which are made up in java swings in eclipse.So ,how can i run it.What run as option to choose.... ...
1
by: manju1983 | last post by:
hello hi everyone, i am doing project using java swings now i have created a Jframe .that jframe contains 3 text fields and 2 jbuttons. now the problem is , when i press the submit button ,...
1
by: better | last post by:
I want to take help for the Table Handling in java and the Trees handling. I am not having any clues about it. I just downlad a cuple of of books on it but they does not explain these things but just...
1
by: swatiawasthy | last post by:
hi i am developing a gui in java swings and i am getting my database displayed in jtable... i have a textfield labeled CUSTID and my table also has its sec column as CUSTID (which is not a primary...
2
by: shashmita | last post by:
Pls solve my problem In a menu,file by clicking a subitem of it,a window to enter input will be displayed. then a window r frame must be visible. here my problem is by performing all...
11
by: gaya3 | last post by:
Hi All, I am very much new to java Swings I need to open the PDF file from Swings application.. I tried as follows... Runtime r= Runtime.getRuntime(); Process p = r.exec("C:/Program...
1
by: rajarameshchint | last post by:
import java.util.*; import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; import java.awt.print.*; public class sd7 extends JFrame implements...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.