The Artima Developer Community
Sponsored Link

Java Answers Forum
Tab Problem!!!

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
nawaray

Posts: 3
Nickname: menwa
Registered: Nov, 2003

Tab Problem!!! Posted: Dec 6, 2003 2:55 AM
Reply to this message Reply
Advertisement
hi,,,

i have this application that has a tab (box tab,voice tab , etc..), buttons , Jtable..
i also have an add button , whenever i click on the add button to add a user(this users info will be added to the JTable) but also a new tab(box) will be added , i don't want a new tab to be added to happen ,,
i don't know how to solve this error..

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.io.*;
import javax.swing.JPanel;
import java.applet.*;
import sun.audio.*;
import java.net.*;
import java.text.*;



class UtilityMethods extends JFrame implements ActionListener
{
private boolean tabsVisible=false;
private File fileName;
private RandomAccessFile output;
AudioStream voice;
UtilityMethods utilities;
Connection conn;
Statement stat;
String dsnName, userStatus;
String appTable = "CUSTOMER";
Container c = getContentPane();
JTabbedPane tabs;
JTable dataTable, addataTable;//,vdataTable;
static String[][] tableData;//,vtableData;
JScrollPane scrollpane, adscrollpane,vscrollpane;
JTextField id, name, address, phone, sex, dob, photo,audio;
JTextArea comments;
JButton search, update, clear, add;
JPanel customerPanel;
JPanel backPanel, inputPanel, middlePanel, buttonPanel1, buttonPanel2, photoHolder;


public UtilityMethods()
{
makeGUI();
}

//============================================================================= ======================
//Create the GUI:

void makeGUI()
{
c.setLayout(new BorderLayout());
tabs = new JTabbedPane();
customerPanel = new JPanel(new BorderLayout());

//1. Construct customers tab:
//The data input section:

inputPanel = new JPanel(new GridLayout(4, 4));
id = new JTextField(20);
name = new JTextField(20);
address = new JTextField(20);
phone = new JTextField(20);
sex = new JTextField(20);
dob = new JTextField(20);
photo = new JTextField(20);
audio = new JTextField(20);

inputPanel.add(new JLabel("CPR", JLabel.CENTER));
inputPanel.add(id);
inputPanel.add(new JLabel("Name", JLabel.CENTER));
inputPanel.add(name);
inputPanel.add(new JLabel("Address", JLabel.CENTER));
inputPanel.add(address);
inputPanel.add(new JLabel("Phone", JLabel.CENTER));
inputPanel.add(phone);
inputPanel.add(new JLabel("Sex", JLabel.CENTER));
inputPanel.add(sex);
inputPanel.add(new JLabel("Date Of Birth", JLabel.CENTER));
inputPanel.add(dob);
inputPanel.add(new JLabel("Customer Photo ", JLabel.CENTER));
inputPanel.add(photo);
inputPanel.add(new JLabel("Voice ", JLabel.CENTER));
inputPanel.add(audio);

customerPanel.add(inputPanel, BorderLayout.NORTH);

//The buttons section:
backPanel = new JPanel();
middlePanel = new JPanel(new BorderLayout());
buttonPanel1 = new JPanel();
buttonPanel2 = new JPanel();
search = new JButton("SEARCH");
update = new JButton("UPDATE");
clear = new JButton("CLEAR");
add = new JButton("ADD");


search.setPreferredSize(new Dimension(102, 26));
update.setPreferredSize(new Dimension(102, 26));
clear.setPreferredSize(new Dimension(102, 26));
add.setPreferredSize(new Dimension(102, 26));

search.addActionListener(this);
update.addActionListener(this);
clear.addActionListener(this);
add.addActionListener(this);


buttonPanel1.add(search);
buttonPanel1.add(update);
buttonPanel1.add(clear);
buttonPanel2.add(add);

middlePanel.add(buttonPanel1, BorderLayout.NORTH);
middlePanel.add(buttonPanel2, BorderLayout.CENTER);

photoHolder = new JPanel();
Icon curPhoto = new ImageIcon("");
Icon[] custPhotos = {curPhoto};
JList photosList = new JList(custPhotos);
photosList.setFixedCellHeight(100);
photosList.setFixedCellWidth(80);
photoHolder.add(photosList);

backPanel.add(middlePanel);
backPanel.add(photoHolder);


customerPanel.add(backPanel, BorderLayout.CENTER);



tabs.add(customerPanel, "Customers");


//if(userStatus.equals("user"))
//{

// vdelete.setEnabled(false);
tabs.setEnabledAt(1, false);
//}

pack();
setVisible(true);
} //makeGUI
//============================================================================= ======================

//============================================================================= ======================
//Update the customer table from the database:

void updateTable(String userStatus)
{
this.userStatus = userStatus;
updateTable();
}

void updateTable()
{
ResultSet results = null;
ResultSet results1 = null;
try
{
//Get the number of rows in the table so we know how big to make the data array..
int rowNumbers = 0;
int columnCount = 6;

results = stat.executeQuery("SELECT COUNT(*) FROM CUSTOMER ");
if(results.next())
{
rowNumbers = results.getInt(1);
} //if

if(rowNumbers == 0)
rowNumbers = 1;
tableData = new String[rowNumbers][columnCount];

//Initialize the data array with "" so we avoid possibly having nulls in it later..
for(int i =0;i<tableData.length;i++)
{
for(int j=0;j<tableData[0].length;j++)
tableData[i][j] = "";
}

//Populate the data array with results of the query on the database..
int currentRow = 0;
results1 = stat.executeQuery("SELECT * FROM CUSTOMER ORDER BY ID");
while (results1.next())
{
for(int i = 0; i < columnCount; i++)
tableData[currentRow][i] = results1.getString(i + 1);
currentRow++;
} //while
//----------------------------------------------------------------------------- ------------------------
//Create the table model:

final String[] colName = { "CPR", "Name", "Address", "Phone", "Sex", "Date OF Birth" };

TableModel pageModel = new AbstractTableModel()
{
public int getColumnCount()
{
return tableData[0].length;
} //getColumnCount

public int getRowCount()
{
return tableData.length;
} //getRowCount

public Object getValueAt(int row, int col)
{
return tableData[row][col];
} //getValueAt

public String getColumnName(int column)
{
return colName[column];
} //getcolName

public Class getColumnClass(int col)
{
return getValueAt(0, col).getClass();
} //getColumnClass

public boolean isCellEditable(int row, int col)
{
return false;
} //isCellEditable

public void setValueAt(String aValue, int row, int column)
{
tableData[row][column] = aValue;
} //setValueAt
//dataTable.setValue( new JScrollBar(JScrollBar.HORIZONTAL), 2,1 );

}; //pageModel

//----------------------------------------------------------------------------- ------------------------
//Create the JTable from the table model:

JTable dataTable = new JTable(pageModel);


//----------------------------------------------------------------------------- ------------------------

/*if (scrollpane != null)
{
scrollpane.setVisible(false);
scrollpane = null;
} //if*/

//scrollpane = new JScrollPane(dataTable);
//scrollpane.setVisible(true);

if (inputPanel == null)
makeGUI();
JScrollPane scrollpane = new JScrollPane(dataTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);


// Create vertical box for second tab
Box box = Box.createVerticalBox();
// Create component for top
box.add(inputPanel);
// Place glue between components
box.add(Box.createVerticalGlue());
// Create component for middle
box.add(middlePanel);
// Place glue between components
box.add(Box.createVerticalGlue());
// Create component for middle
box.add(backPanel);
// Place glue between components
box.add(Box.createVerticalGlue());
// Create component for bottom
box.add(scrollpane);
// Add box to JTabbedPane
tabs.add(box, "Box");
if (!tabsVisible) {

c.add(tabs);
tabsVisible=true;
}




// customerPanel.add(scrollpane, BorderLayout.SOUTH);




// c.add(tabs);
id.grabFocus();
pack();
repaint();

} //try
catch (Exception e)
{
System.out.println("Caught updateTable exception: " + e);
} //catch
} //updatetable
//============================================================================= ======================



//The button actions:

public void actionPerformed(ActionEvent e)
{
FileInputStream fis = null;

if (e.getSource() == add) //The ADD button.
{
//User has not populated all the input fields.

if(name.getText().equals("")|| address.getText().equals("")|| phone.getText().equals("")|| sex.getText().equals("")|| dob.getText().equals("")|| photo.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Please fill in all the fields","Missing Fields",JOptionPane.INFORMATION_MESSAGE);
}
else
{
// save the new customer:

try
{
//1. take the customer's data and photo:

int userId = Integer.parseInt(id.getText());
String userName = name.getText();
String userAddress = address.getText();
String userPhone = phone.getText();
String userSex = sex.getText();
String userDateBirth = dob.getText();
String photoName = photo.getText();
String audioName= audio.getText();
File file = new File(photoName);
int fileLength = (int)file.length();

//2. Set the user's photo into the photoHolder:

photoHolder.setVisible(false);
photoHolder = null;
comments.setVisible(false);
comments = null;
Icon[] custPhotos = {new ImageIcon(photoName)};
JList photosList = new JList(custPhotos);
photosList.setFixedCellHeight(100);
photosList.setFixedCellWidth(80);
photoHolder = new JPanel();
photoHolder.add(photosList);


//3. Insert the data and photo into the database:

if(fileLength > 0)
{
fis = new FileInputStream(file);
String query = " INSERT INTO CUSTOMER VALUES('"+userId+"', '"+ userName+ "', '"+ userAddress+ "', " +" '"+ userPhone+ "', '"+ userSex+ "', '"+ userDateBirth+ "', ?,?,? ) ";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setBinaryStream(1, fis, fileLength);
pstmt.setString(2,photoName);
pstmt.setString(3,audioName);
pstmt.executeUpdate();
comments.setText(userName+", added.");
}
else
{
String query = " INSERT INTO CUSTOMER (id, name, address, phone, sex, dob) VALUES('"+userId+"', '"+userName+"', '"+userAddress+"', '"+userPhone+"', '"+userSex+"', '"+userDateBirth+"') ";
stat.executeUpdate(query);

}

backPanel.add(photoHolder);


updateTable();
} //try
catch (Exception ee)
{
//The danger of putting creating the JOptionPane in here is that it will show the same message regardless of the error.
JOptionPane.showMessageDialog(null, "Customers CPR already exits!!Please enter another CPR","Invalid",JOptionPane.INFORMATION_MESSAGE);
System.out.println("Caught exception in add action: " + ee);
} //catch
} //if
}//add button
}
//----------------------------------------------------------------------------- ------------------------

//Handle the connections to the database:

void makeConnection(String dsnName)
{
try
{
this.dsnName = dsnName;
System.out.println("Connecting to database..");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:" + dsnName);
stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
}
catch (Exception e)
{
System.out.println("Caught exception in makeConnection: " + e);
}
} //makeConnection()
//----------------------------------------------------------------------------- ------------------------

void closeConnection()
{
try
{
System.out.println("Closing database connections..");
conn.close();
conn = null;
stat = null;
}
catch (Exception e)
{
System.out.println("Caught closeConnection exception: " + e);
}
} //closeConnection()
public static void main(String args[]){
UtilityMethods application=new UtilityMethods();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}//MAIN
}

Topic: Connection reset by peer Previous Topic   Next Topic Topic: EJB

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use