jtable visiblity false

i have jtable and one more frame in frame there r buttons i want to make that when i clicked on button jtable visibility became falsehow to do that
[175 byte] By [sudh_friendsforevera] at [2007-11-27 2:06:23]
# 1

First Create Two Panels using JPanel.

Create the JTable and add it to First Panel. Set Border Layout to these two Panels.

JTable tab = new JTable(data,cols);

JTableHeader head = tab.getTableHeader();

panel1.add("North",head);

panel1.add("Center",tab);

Now Create a Button and place it in a Second Panel and attach ActionListener to this.

While implementing 'actionPerformed()' remove the Table added to the panel1 and call 'repaint()'.

public void actionPerformed(ActionEvent ae)

{

String str = ae.getActionCommand();

if(str.equals("Remove"))

{

p1.remove(head);

p1.remove(tab);

}

repaint();

}

Vencya at 2007-7-12 1:53:27 > top of Java-index,Desktop,Core GUI APIs...
# 2

> First Create Two Panels using JPanel.

>

> Create the JTable and add it to First Panel.

>Set Border Layout to these two Panels.

>JTable tab = new JTable(data,cols);

> JTableHeader head = tab.getTableHeader();

>

>panel1.add("North",head);

> panel1.add("Center",tab);

>

> Now Create a Button and place it in a Second

> Panel and attach ActionListener to this.

>

> While implementing 'actionPerformed()' remove the

> Table added to the panel1 and call 'repaint()'.

>

> public void actionPerformed(ActionEvent

> ae)

> {

> String str = ae.getActionCommand();

>

> if(str.equals("Remove"))

> {

> p1.remove(head);

> p1.remove(tab);

> }

> repaint();

> }

but i have another frame for jatable by defaulttablemodel and i m calling that table by clicking on textfield of main frame.

the button is also on main frame.

sudh_friendsforevera at 2007-7-12 1:53:27 > top of Java-index,Desktop,Core GUI APIs...
# 3
I didn't get you. But, normally the above code works for all situations (by making small changes).If the table and button are in the Same Frame then just change the statements in actionPerformed() to :[Container c =
Vencya at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I didn't get you. But, normally the above code works

> for all situations (by making small changes).

>

> If the table and button are in the Same Frame then

> just change the statements in actionPerformed() to :

>

>[Container c = getContentPane();]

>c.remove(head);

>c.remove(tab);

no the table and the button are not in same frame.

in table frame there is only table

and in another frame there is button and textfield

sudh_friendsforevera at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 5
Initially I assumed same thing. It is similar to JPanel concept only.Can you place your code here.
Vencya at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 6

import javax.swing.*;

import java.awt.*;

import java.sql.*;

import java.awt.event.*;

class classmaster

{

JFrame f;

JPanel p;

JLabel lclass,lclass1;

JTextField tclass,tclass1;

JButton badd,bedit,bdelete,bexit;

String edclass;

classtable1 ct=new classtable1();

boolean flag=true;

public void master()

{

f=new JFrame("Class Master");

f.setBounds(200,150,400,160);

p=new JPanel();

p.setBackground(Color.WHITE);

p.setLayout(null);

f.getContentPane().add(p);

lclass=new JLabel("Class Name");

lclass.setBounds(15,10,100,20);

lclass1=new JLabel("Existing Class Name");

lclass1.setBounds(15,50,120,20);

p.add(lclass);

p.add(lclass1);

tclass=new JTextField();

tclass.setBounds(150,10,150,20);

tclass1=new JTextField();

tclass1.setBounds(150,50,150,20);

p.add(tclass);

p.add(tclass1);

badd=new JButton("Add");

bedit=new JButton("Edit");

bdelete=new JButton("Delete");

bexit=new JButton("Exit");

badd.setBounds(25,100,80,20);

p.add(badd);

bedit.setBounds(115,100,80,20);

p.add(bedit);

bdelete.setBounds(205,100,80,20);

p.add(bdelete);

bexit.setBounds(295,100,80,20);

p.add(bexit);

lclass.setEnabled(false);

tclass.setEnabled(false);

lclass1.setEnabled(false);

tclass1.setEnabled(false);

f.setVisible(true);

now this code for table

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

import java.util.*;

import javax.swing.table.DefaultTableModel;

public class classtable1//tableDemo

{

JFrame f;

JTable table;

public void demo()

{

final classtable1 ct=new classtable1();

//final tableDemo app=new tableDemo();

//if(!app.err)

{

javax.swing.SwingUtilities.invokeLater

(

new Runnable()

{

public void run()

{

ct.drawWindow();

ct.activateDatabase();

}

}

);

}

}

private boolean err=false;

private Vector columnNames=new Vector(0,1);

private Object queriedDataRow[]=new String[2];

private MyDefaultTableModel dtm=new MyDefaultTableModel();

private void activateDatabase()

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:srpinfo","sa","sa");

Statement st=con.createStatement();

ResultSet rs=st.executeQuery("SELECT * FROM classmaster");

while( rs.next() )

{

queriedDataRow[0]=(Object)rs.getString("classname");

//queriedDataRow[1]=(Object)rs.getString("col2");

dtm.addRow(queriedDataRow);

}

rs.close();

st.close();

}

catch(Exception e)

{

System.out.println(e);

err=true;

}

}

public void drawWindow()

{

table=new JTable(dtm);

JScrollPane scrollPane=new JScrollPane(table);

scrollPane.setPreferredSize(new Dimension(200,600));

JPanel pane=new JPanel();

pane.add(scrollPane);

f=new JFrame("Table");

//f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(pane);

f.pack();

f.setVisible(true);

}

public class MyDefaultTableModel extends DefaultTableModel

{

String colNames[]={"classname"};

public int getColumnCount()

{

return colNames.length;

}

public String getColumnName(int col)

{

return colNames[col];

}

public boolean isCellEditable(int row,int col)

{

return false;

}

public Class getColumnClass(int col)

{

return getValueAt(0,col).getClass();

}

}

public void close()

{

f.setVisible(false);

}

}

sudh_friendsforevera at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 7
i have send code for my main frame and jtable now plzz help me thanx in advance plzzzzzzzzzzzzzz
sudh_friendsforevera at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 8

> i have send code for my main frame and jtable now plzz help me

Yes but we don't want to see the code for your application. We want code that demonstrates the problem is a simple manner. We don't have acces to your database so we can't execute your code.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

> i want to make that when i clicked on button jtable visibility became false

Then create a SSCCE with these requirements. That is create a frame with a button and and a table. Then add an ActionListener to the button to hide the table.

It take onlys one line of code to create a table for simple demo purposes:

JTable table = new JTable(3, 5);

I'll let you create the rest of the SSCCE.

camickra at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 9
sorry for it but i want to hide jtable and again activate iti m calling jtable class object in another frame.and from that frame i want to hide jtableplzzzzzzzzzsorry again bcoz i m new to java i dont know that much i found jtable also from here
sudh_friendsforevera at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 10

> sorry again bcoz i m new to java i dont know that much

Thats why you start with something simple (learn to walk before you run). You create a frame with a two buttons ("hide table", "show table"). The whole program should be less than 20 lines of code. Once you understand how to do it in a really simple demo, then you use the information you learned from the demo and apply the same concepts to your real program.

camickra at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 11

this is my table code

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

import java.util.*;

import javax.swing.table.DefaultTableModel;

public class tableDemo

{

private boolean err=false;

private Vector columnNames=new Vector(0,1);

private Object queriedDataRow[]=new String[2];

private MyDefaultTableModel dtm=new MyDefaultTableModel();

private void activateDatabase()

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:srpinfo","sa","sa");

Statement st=con.createStatement();

ResultSet rs=st.executeQuery("SELECT * FROM classmaster");

while( rs.next() )

{

queriedDataRow[0]=(Object)rs.getString("classname");

//queriedDataRow[1]=(Object)rs.getString("col2");

dtm.addRow(queriedDataRow);

}

rs.close();

st.close();

}

catch(Exception e)

{

System.out.println(e);

err=true;

}

}

private void drawWindow()

{

JTable table=new JTable(dtm);

JScrollPane scrollPane=new JScrollPane(table);

scrollPane.setPreferredSize(new Dimension(500,200));

scrollPane.setBounds(780,15,225,700);

JPanel pane=new JPanel();

pane.add(scrollPane);

JFrame frame=new JFrame("Table");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(pane);

frame.pack();

frame.setVisible(true);

}

public class MyDefaultTableModel extends DefaultTableModel

{

String colNames[]={"classname"};

public int getColumnCount()

{

return colNames.length;

}

public String getColumnName(int col)

{

return colNames[col];

}

public boolean isCellEditable(int row,int col)

{

return false;

}

public Class getColumnClass(int col)

{

return getValueAt(0,col).getClass();

}

}

public static void main(String[] args)

{

final tableDemo app=new tableDemo();

//if(!app.err)

{

javax.swing.SwingUtilities.invokeLater

(

new Runnable()

{

public void run()

{

app.drawWindow();

app.activateDatabase();

}

}

);

}

}

}

this u can execute..

now plzz help me

i m calling object of this table in another frame

there i m making it show by key event.

then i want to make it visible false how to do it..

actually i m working on project and i have to give it in this month its abt my job plzzzzzzzzzzz

htats y i dont have time to walk .

sudh_friendsforevera at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 12

> i dont have time to walk .

Why is it that every time you ask a question it takes 20 answers to solve your problem. That because you don't listen to the advice given.

I've already given you code on how to load a table from a database. Instead you decide to waste time writing your own TableModel.

Now you have a question about making a table invisble. I have no idea why you are attempting to do this. You load data into a table and then you want to make it invisible. Why? Whats the point of creating a table and not showing it.

It you post a reasonable example that shows what you are attempting to don then maybe you will get some help, but I'm not going to waste my time reading unnecessary.

For a simple demo program it take a single line of code to create a table:

JTable table = new JTable(10, 50);

Thats it. Now you have a simple demo to play with and learn the basics.

camickra at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 13

sorry for it plz dont get angry.

i have made simple demo as u said

by single line

JTable table = new JTable(10, 50);

but i have to retrive data from database for that i used my table model.

there is one more form in which i have textfield and button

now if i type any thing on textfield this table will open and if i click button this table will again became invisible.

sudh_friendsforevera at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 14

> sorry for it plz dont get angry.

> i have made simple demo as u said

> by single line

> JTable table = new JTable(10, 50);

> but i have to retrive data from database for that i

> used my table model.

> there is one more form in which i have textfield and

> button

> now if i type any thing on textfield this table will

> open and if i click button this table will again

> became invisible.

import javax.swing.*;

import java.awt.*;

public class jt

{

public static void main(String args[])

{

JFrame frame=new JFrame("Table");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTable table=new JTable(3,5);

frame.getContentPane().add(table);

frame.pack();

frame.setVisible(true);

}

}

plz reply me

sudh_friendsforevera at 2007-7-12 1:53:28 > top of Java-index,Desktop,Core GUI APIs...
# 15

You've also been asked to use the "Code Formatting Tags", not just sometimes when you post code, but every time you post code.

Whats the point of this demo, the table displays correctly. If you want the heading to display then you add the table to a scroll pane and add the scroll pane to the content pane.

Again you question is not clear I don't know how you expect us to give you an answer.

camickra at 2007-7-21 20:19:14 > top of Java-index,Desktop,Core GUI APIs...
# 16
Have you tried just called setVisible(false) on the Jtable? myTable.setVisibe(false);
gracklemanna at 2007-7-21 20:19:14 > top of Java-index,Desktop,Core GUI APIs...
# 17
yes i tried it but i dont know y it is not working
sudh_friendsforevera at 2007-7-21 20:19:14 > top of Java-index,Desktop,Core GUI APIs...