• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Visibility help?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to make a gui for connecting to a database and updating/deleting files/searching tables. I wanted to have all the buttons on the left side then use the rest of the application for the option they choose. EX: they clicked "Search by name" it will show a field on the right side with a name box, a search button, and underneath a table like box that will hold all the records in the result.

When i thoguht of how i could do this ifigure i can make a panel cooresponding to the buttons they pressed and set the visibility to false, then when a buttons pressed the qpanel that goes for that button will have visibility set to true.

The problem i'm having is making a panel inside contentPane and havig it Not visible and changing it to visible. The code i have now is:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;

import javax.swing.*;
public class GUI extends JFrame implements ActionListener{
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public GUI(){
setSize(WIDTH, HEIGHT);
WindowDestroyer listener = new WindowDestroyer( );
addWindowListener(listener);


Container contentPane = getContentPane();
contentPane.setBackground(Color.white);

contentPane.setLayout(new BorderLayout());

JPanel buttonpanel = new JPanel();
buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.PAGE_AXIS));
buttonpanel.setBackground(Color.white);

JButton custid = new JButton("CustID");
custid.addActionListener(this);
buttonpanel.add(custid);

JButton fname = new JButton("First Name");
fname.addActionListener(this);
buttonpanel.add(fname);

JButton lname = new JButton("Last Name");
lname.addActionListener(this);
buttonpanel.add(lname);

JButton phone = new JButton("Phone");
phone.addActionListener(this);
buttonpanel.add(phone);

JButton email = new JButton("E-Mail");
email.addActionListener(this);
buttonpanel.add(email);

contentPane.add(buttonpanel, BorderLayout.WEST);

JPanel testing = new JPanel();
testing.setLayout(new BoxLayout(testing, BoxLayout.PAGE_AXIS));
testing.setBackground(Color.white);

JButton TEST1 = new JButton("THIS");
TEST1.addActionListener(this);
testing.add(TEST1);

JButton TEST2 = new JButton("IS");
TEST2.addActionListener(this);
testing.add(TEST2);

JButton TEST3 = new JButton("TESTING");
TEST3.addActionListener(this);
testing.add(TEST3);



contentPane.add(testing, BorderLayout.EAST);




}
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand( );
Container contentPane = getContentPane( );

if (actionCommand.equals("CustID"))
contentPane.setBackground(Color.BLUE);
else if (actionCommand.equals("First Name"))
contentPane.setBackground(Color.GRAY);
else if (actionCommand.equals("Last Name"))
contentPane.setBackground(Color.GREEN);
else if (actionCommand.equals("Phone"))
contentPane.setBackground(Color.RED);
else if (actionCommand.equals("E-Mail"))
/*THIS IS WHERE THE ERROR IS testing cannot be resolved*/ testing.setVisible(true);
else
System.out.println("Error in button interface.");
}
}

Near the bottom you can see where its erroring, testing.setVisible(true);
How would i access the visibility of testing? or am i doing this all wrong and there's another way to make all of the happen besides making everything invisible?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it sounds like a cardlayout would be a good option for you to consider.

here's a simple demo program (it has radiobuttons down the left side
instead of JButtons) - select any of the radiobuttons to show the
corresponding panel

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The_Anomaly

Welcome to Javaranch. We don't have too many rules around here but we do have a Naming Policy. Please adjust your display name accordingly.

Thanks.
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll have to give that a try and see what i come up with. The layout i was thinking of isnt something i'm forced to i just thought it would be the best for the program i'm making.

sorry about the name thing, first day mistake. its changed now
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this looks like something i would be able to work with. But i stillw ant to have buttons instead of radio because of some of the other options i plan to add in.

There's a few lines confusing me:

rb[x].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
cl.show(center,index);}});

Instead of adding the method actionPerformed like i did this is an alternative? Fron what i gather from it your still making a new method but its within another method, If thats how it is i didnt know that was legal.

also is this line:

setDefaultCloseOperation(EXIT_ON_CLOSE);

an alternative to making a windows destroyer class?

Sorry for all thge questions that may be noobish, i just started taking java classes a little while ago
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>There's a few lines confusing me:
>rb[x].addActionListener(new ActionListener(){
>public void actionPerformed(ActionEvent ae){
>cl.show(center,index);}});

this is just an inner class, where the listener action is added directly to the source.

it is just as easy to have a generic actionPerformed(), as you have, and then
identify the source prior to performing an action.
(either way is a matter of preference)

> also is this line:
> setDefaultCloseOperation(EXIT_ON_CLOSE);
> an alternative to making a windows destroyer class?

this is from the api docs for JFrame (will explain it better than I can)


[ July 31, 2005: Message edited by: Gregg Bolinger ]
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok this is making my brain bleed i think

The way you have the sample program set up it makes 3 panels that when the cooresponding button is pressed will come into view. I made a String Array thathas the name of each button i want to add and instead of having panel x+1 i have buttonArray[x] which works fine. The problem i'm having is i need each button to do something different and i cant really figure out how. I tried making the actionPerformed mas i did before and using:

if(ae.equals("CustID"){
//The code i want executed
}else if(ae.equals("First Name"){
//The code i want executed
}else//so on and so forth

but this doesnt work, and it seems i'm just making myself try the same thing i was having problems with before. So the question now is, what can i do to make it so each button has different events? I also tried putting the same statements within the actionPerformed lines you already had but that still didnt work. Could you show me a little something within that program you posted before that would be able to make each button do something else. Or explain how i could make it do this if it should already and i'm just not seeing it. Not trying to leach free code, i just learn alot better looking at a the source of a working program.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this for your actionPerformed

 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i figured out some logic and a way to do the different panels using the other action listener way you showed. I think i got the gui pretty much done, tomarrow i just have to learn how to do SQL in java.

Still one last question, i would rather not use the radio buttons and use JButtons but as of right now i only have 5 buttons and their height is equally spread until it reaches the bottom. How could i define a height or make the layout not go all the way to the bottom?

Here's what i have now if you dont understand what i'm saying:

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
add/change this at the bottom of the constructor

JPanel holdingPanel = new JPanel();//add
holdingPanel.add(west);//add
//getContentPane().add(west,BorderLayout.WEST);//change to below line
getContentPane().add(holdingPanel,BorderLayout.WEST);
getContentPane().add(center,BorderLayout.CENTER);//included as marker
//rb[0].setSelected(true);;//not required
}
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AWSOME! You've been alot of help, now just to figure out the SQL

Do you know any good sites/coding examples to figure out how to use SQL in java? plz dont say sun, i can never find anything on their site

[ August 01, 2005: Message edited by: Bob Thompson ]
[ August 01, 2005: Message edited by: Bob Thompson ]
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this: Connecting to a Database

My first Java application involved a serial port reader and a SQL connection to a database via a DSN. I believe it was this article that helped me out.

hth.

Steve
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> plz dont say sun, i can never find anything on their site

Yes, it is a bit 'navigation-unfriendly', but this should be where you start

http://java.sun.com/docs/books/tutorial/jdbc/

To search sun's site, use this link
http://onesearch.sun.com/search/onesearch/advanced.jsp

enter the keywords, click search, then at the results,
click on developer, then forums
(this is where you'll get most of your answers).
you can further narrow it down using the comboboxes to select specific forums, or forum groups

The article from Stephen Boston's link looks good
(have to remove the 2nd http:// from the link)
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guess i should refresh before replying

I tried suns tutorial but i couldnt make head or tails of it. Said something about having to load a driver and when i used the line they supplied it didnt work.
[ August 01, 2005: Message edited by: Bob Thompson ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's another link, which might offer a bit more insight (worth bookmarking the site)

Roedy Green's Java Website

a lot depends on what db you are trying to access, so perhaps your best
approach is to post a question at the jdbc forum (where the jdbc experts are):
"how to connect to oracle" (or whatever db it is).
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i tried that tutorial and it didnt work, i still think its this driver thing. i get this error with their code exacly

SQL Exception: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Exception in thread "main" java.lang.NullPointerException
at dbWrapper.Select(dbWrapper.java:57)
at DBTest.main(DBTest.java:13)

do i have to download/install something?

i should have probably mentioned i'm using mySQL with phpadmin
[ August 01, 2005: Message edited by: Bob Thompson ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's a couple of info sites for java/mySQL

http://www.javacoding.net/articles/technical/java-mysql.html

http://www.developer.com/java/data/article.php/3417381
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cant take it anymore, i want to smash my computer . I tried those links and installed the driver form the first one but that didnt work. I changed the driver in the program too and when ran it said "unsuitable driver".

My roomate claims that to use sun.jdbc.odbc.JdbcOdbcDriver you shouldnt have to install anything new, i should already have it. any idea's?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't use MySQL, so there's not a lot further I can suggest at this stage.
I've now downloaded MySQL, and I'll play around with it at home tonight
(to see if I can get it working)

In the meantime, if you have MS Access, here's a link to a fully working
program (reply #6)
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=504709

change the "c:/somedirectorySomedatabase" to the correct path
run the program
click on the combobox
select the db name from the dropdown list (must be selected from the dropdown)
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Managed to get MySQL working OK, Windows XP java 1.5

these were the steps (from both Gamelan and JavaCoding.net sites)
1. downloaded MySQL
2. downloaded the connector file
3. unzipped/installed MySQL
4. unzipped the connector file
mysql-connector-java-3.0.17-ga-bin.jar
copied the jar file into this directory
C:\Program Files\Java\jdk1.5.0_03\jre\lib\ext
5. opened MySQL, using the code from Gamelan
create the db JunkDB
created the user, permissions, password
6. combined the code from both sites (code follows)

after fixing a couple of typo's, all worked fine

 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so this is beging weird. I had installed the drivers the same plave you did and tried running your code. at firstit said driver found then it said Oops, no connection then while editing like 4 minutes later a bunch of stuff came up

MySQL Driver found
OOPs - No Connection
java.sql.SQLException: Communication link failure: java.io.EOFException, underlying cause: null

** BEGIN NESTED EXCEPTION **

java.io.EOFException

STACKTRACE:

java.io.EOFException
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1395)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:1414)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:625)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1808)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MySQL_Test.getConnection(MySQLTest.java:28)
at MySQL_Test.<init>(MySQLTest.java:6)
at MySQL_Test.main(MySQLTest.java:70)


** END NESTED EXCEPTION **


at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:1447)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:625)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1808)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MySQL_Test.getConnection(MySQLTest.java:28)
at MySQL_Test.<init>(MySQLTest.java:6)
at MySQL_Test.main(MySQLTest.java:70)

I also tried using the driver registering as you did within the codei have and its still saying "unsuitable driver"
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops! Sorry about the double http.

Bob, did you setup a DSN on your PC? It will fail if you do not have that setup to a database.

My experience is only on windows (so far) but it is fairly easy to setup.

Drat, I missed your last post. It looks like you made it that far.

Where is the database at? Local or Internet?
[ August 02, 2005: Message edited by: Stephen Boston ]
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm running easyPHP on my comp. works fine for everything as a testing db. I've mainly been using it for php.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
definitely is weird.

You did complete the middle steps OK?

mysql> CREATE DATABASE JunkDB;
Query OK, 1 row affected (0.13 sec)

and

mysql> GRANT SELECT,INSERT,UPDATE,
-> DELETE,CREATE,DROP
-> ON JunkDB.*
-> TO 'auser'@'localhost'
-> IDENTIFIED BY 'drowssap';
Query OK, 0 rows affected (0.01 sec)
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but i did change the username and pass to the one i always use for my php work. It has all access for everything.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did a quick search on the error message, there were a couple of suggestions
of possible duff drivers, and to 'get the latest' etc.

perhaps its worth matching what I dowloaded, because it definitely worked OK

mysql-4.1.13-win32.zip
mysql-connector-java-3.0.17-ga.zip
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another thought
url = "jdbc:mysql://localhost:3306/JunkDB";

perhaps all you need is
url = "jdbc:mysql://localhost/JunkDB";
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm usnig port 8080, i did change it to 8080 and still had the message. i'll try downloading the files you specefied
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting, i removed the 8080 and got this:

MySQL Driver found
Connection OK
Found record 1 first entry
Found record 2 second entry
Found record 3 third entry

Now my quetion is, it seems your program is supposed to create a table? The DB is still empty after running it. At least the connection works though, now i can actually work with it

Ok i can be slapped for that last question, i overlooked the executeUpdate(con,"drop table test"); line. if i comment that the table stays. I think i can try what i wanted now
[ August 03, 2005: Message edited by: Bob Thompson ]
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where would i find how to make this same line in java:

thats from a php file i made. It also has:

then there's an array in a while statement that fetches all of the data from the field specefied:


So what i'm trying to do it get into an existing table, fetch everything on that table thats in the specified field, then display it somehow.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Where would i find how to make this same line in java:

perhaps now is the time to venture into the jdbc forum.

I don't know how active it is, so if you don't get a solution there,
try the jdbc forum at sun.
 
Bob Thompson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
roger that capn
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Dunn:

perhaps now is the time to venture into the jdbc forum.

I don't know how active it is, so if you don't get a solution there,
try the jdbc forum at sun.


For anyone following this, the JDBC thread is
here.

Michael, The JDBC forum is quite active here.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic