JavaRanch » Java Forums »
Java »
Swing / AWT / SWT
| Author |
JTabbedPane
|
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
Hi, Just wondering if anyone could help me with this, i'm using choice menus, in the PersonalDetails and the EventDetails parts, but when i run this they are both displayed in the PersonalDetails tab, this problem goes when i select the Event tab, can someone please help? import javax.swing.JTabbedPane; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JComponent; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.KeyEvent; import java.applet.*; import java.awt.*; import java.util.*; import java.awt.event.*; import java.applet.Applet; import javax.swing.*; import javax.swing.event.ChangeEvent; import java.io.*; import javax.swing.JScrollPane; //for scroll bars import java.sql.*; import java.awt.font.*; public class TabbedDemo extends JApplet { public TabbedDemo() { JTabbedPane tabbedPane = new JTabbedPane(); ImageIcon icon = createImageIcon("middle.gif"); PersonalDetails pers = new PersonalDetails(); JComponent panel1 = pers.makePersonalDetails(); tabbedPane.addTab("Personal Details", icon, panel1, "Allows user to enter personal details"); tabbedPane.setMnemonicAt(0, KeyEvent.VK_1); OtherDetails fam = new OtherDetails(); JComponent panel2 = fam.makeFOtherDetails("Panel #2"); tabbedPane.addTab("Family Tree", icon, panel2, "Gives a other details"); tabbedPane.setMnemonicAt(1, KeyEvent.VK_2); EventDetails event = new EventDetails(); JComponent panel3 = event.makeEventDetails("Panel #3"); tabbedPane.addTab("Events", icon, panel3, "Allows user to enter event details"); tabbedPane.setMnemonicAt(2, KeyEvent.VK_3); tabbedPane.update(tabbedPane.getGraphics()); //Add the tabbed pane to this panel. this.getContentPane().add(tabbedPane); //Uncomment the following line to use scrolling tabs. tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); int index = tabbedPane.getSelectedIndex(); } /** Returns an ImageIcon, or null if the path was invalid. */ protected static ImageIcon createImageIcon(String path) { java.net.URL imgURL = TabbedDemo.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("Family Tree"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. frame.getContentPane().add(new TabbedDemo(), BorderLayout.CENTER); //Display the window. frame.pack(); frame.setSize(600,350); frame.setVisible(true); } public void actionPerformed(ActionEvent event) { } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
|
 |
Don Kiddick
Ranch Hand
Joined: Dec 12, 2002
Posts: 580
|
|
You need to include the PersonalDetails, OtherDetails and EventDetails source code for us to help you. D.
|
 |
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
The OtherDetails isn't important but herre are the other 2: import java.awt.event.*; import java.awt.Color; import java.awt.Choice; import javax.swing.*; import javax.swing.JScrollPane; //for scroll bars import javax.swing.text.JTextComponent; import java.sql.*; import java.applet.*; import java.awt.*; class PersonalDetails extends JPanel implements ActionListener { /* ------ FIELDS ------ */ // GUI features private JTextField firstNameField; private JTextField middleNameField; private JTextField surnameField; private JTextField maidenNameField; private JTextField photoField; private JTextField dobField; private JTextField notesField; privateChoice backgroundMenu; private JButton submit; private JPanel inputPanel; /** Output panel */ private JPanel outputPanel; protected JComponent makePersonalDetails() { inputPanel = new JPanel(); inputPanel.setLayout(null); inputPanel.setBorder(BorderFactory.createTitledBorder("Input")); // Add Labels JLabel first = new JLabel("First Name"); first.setBounds( 10, 15, 80, 20 ); JLabel middle = new JLabel("Middle Name"); middle.setBounds( 210, 15, 80, 20 ); JLabel surname= new JLabel("Surname"); surname.setBounds( 420, 15, 80, 20 ); JLabel maiden = new JLabel("Maiden Name"); maiden.setBounds( 10, 65, 80, 20 ); JLabel gender= new JLabel("Gender"); gender.setBounds( 210, 65, 80, 20 ); JLabel photo= new JLabel("Photo"); photo.setBounds( 10, 115, 80, 20 ); JLabel notes= new JLabel("Notes"); notes.setBounds( 210, 115, 80, 20 ); submit = new JButton("Submit"); submit.setBounds( 10, 165, 80, 20 ); submit.addActionListener(this); firstNameField = new JTextField("Jack"); firstNameField.setBounds( 90, 15, 80, 20 ); firstNameField.addActionListener(this); middleNameField = new JTextField("Paul"); middleNameField.setBounds( 290, 15, 80, 20 ); middleNameField.addActionListener(this); surnameField = new JTextField("Smith"); surnameField.setBounds( 490, 15, 80, 20 ); surnameField.addActionListener(this); maidenNameField = new JTextField("N/A"); maidenNameField.setBounds( 90, 65, 80, 20 ); maidenNameField.addActionListener(this); backgroundMenu = makeMenu("Choose"); backgroundMenu.setBounds( 290, 65, 60, 20 ); this.add(backgroundMenu); photoField = new JTextField("Photo"); photoField.setBounds( 90, 115, 80, 20 ); photoField.addActionListener(this); notesField = new JTextField("random stuff"); notesField.setBounds( 290, 115, 80, 20 ); notesField.addActionListener(this); // Add to panel inputPanel.add(submit); inputPanel.add(first); inputPanel.add(firstNameField); inputPanel.add(middle); inputPanel.add(middleNameField); inputPanel.add(surname); inputPanel.add(surnameField); inputPanel.add(maiden); inputPanel.add(maidenNameField); inputPanel.add(gender); inputPanel.add(backgroundMenu); inputPanel.add(photo); inputPanel.add(photoField); inputPanel.add(notes); inputPanel.add(notesField); inputPanel.validate(); return inputPanel; } public void actionPerformed(ActionEvent event) { if (event.getActionCommand().equals("Submit")) inputData(); else System.out.println("fdsdafas"); } private Choice makeMenu(String inIntialChoice) { Choice menu = new Choice(); String theString = "male"; menu.add(theString); theString = "female"; menu.add(theString); theString = "unknown"; menu.add(theString); theString = "other"; menu.add(theString); return menu; } private void inputData() { String firstName = firstNameField.getText(); String middleName = middleNameField.getText(); String surname = surnameField.getText(); String maidenName = maidenNameField.getText(); String gender = backgroundMenu.getSelectedItem(); String photo = photoField.getText(); //String dob = dobField.getText(); String notes = notesField.getText(); Connection conn = null; // try-catch needed when connecting to the database in case of connection disruption try { // Java DataBase Connection driver //Class.forName("oracle.jdbc.driver.OracleDriver"); DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); // passing in the parameters for the DB conenction conn = DriverManager.getConnection("jdbc racle:thin:@oracle.isg.computing.dcu.ie:1521 racle","username","password"); // creation and execution of the SQL query to check the username and password againt the DB Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT max(individid) FROM people"); int count = 0; if(rs.next()) count = rs.getInt("max(individid)"); count++; System.out.println(count); String query = "INSERT INTO people VALUES("+count+",'"+firstName+"','"+middleName+"','"+surname +"','"+maidenName+"','"+gender.substring(0,1)+"','"+photo+"','"+notes+"')"; System.out.println(query); stmt.executeQuery(query); stmt.executeQuery("commit"); } catch(SQLException e) // catch Exceptions realating to SQL commands { System.out.println("SQLException: " + e.getMessage()); while((e = e.getNextException()) != null) System.out.println(e.getMessage() + "<BR>"); } finally // only completes the following if after successful run of try block { //Clean up resources, close the connection. if(conn != null) { try { conn.close(); } catch (Exception ignored) { } } } } } import java.awt.event.*; import java.awt.Color; import java.awt.Choice; import javax.swing.*; import javax.swing.JScrollPane; //for scroll bars import javax.swing.text.JTextComponent; import java.sql.*; import java.applet.*; import java.awt.*; class EventDetails extends JPanel implements ActionListener { private JTextField firstNameField; private JTextField middleNameField; private JTextField surnameField; private JTextField maidenNameField; private Choice eventList; private Choice peopleList; private Choice dayList; private Choice monthList; private Choice yearList; private JButton submit; private JPanel inputPanel; protected JComponent makeEventDetails(String text) { inputPanel = new JPanel(); inputPanel.setBorder(BorderFactory.createTitledBorder("Events")); JLabel event = new JLabel("Choose Event"); event.setBounds( 100, 15, 80, 20 ); eventList = makeEventMenu("Choose"); eventList.setBounds( 190, 15, 150, 60 ); this.add(eventList); JLabel people = new JLabel("Choose person this relates to"); people.setBounds( 210, 215, 180, 20 ); peopleList = makePeopleMenu("Choose"); peopleList.setBounds( 410, 215, 150, 60 ); this.add(peopleList); DateComboBox date = new DateComboBox(); date.setBounds( 10, 215, 80, 20 ); this.add(new DateComboBox()); submit = new JButton("Submit"); submit.setBounds( 190, 415, 80, 20 ); inputPanel.add(event); inputPanel.add(eventList); inputPanel.add(people); inputPanel.add(peopleList); inputPanel.add(date); inputPanel.add(submit); return inputPanel; } public void actionPerformed(ActionEvent event) { if (event.getActionCommand().equals("Submit")) inputData(); else System.out.println("fdsdafas"); } private Choice makeEventMenu(String inIntialChoice) { Choice menu = new Choice(); Connection conn = null; // try-catch needed when connecting to the database in case of connection disruption try { // Java DataBase Connection driver DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); // passing in the parameters for the DB conenction conn = DriverManager.getConnection("jdbc racle:thin:@oracle.isg.computing.dcu.ie:1521 racle","username","password"); // creation and execution of the SQL query to check the username and password againt the DB Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT count(*) FROM eventcode"); int count = 0; if(rs.next()) count = rs.getInt("count(*)"); String [] list = new String [count]; rs = stmt.executeQuery("SELECT Type FROM eventcode"); int i=0; while(rs.next() == true) { list[i] = rs.getString("Type"); i++; } for (i=0;i<count ;i++ ) { String theString =list[i]; menu.add(theString); } } catch(SQLException e) // catch Exceptions realating to SQL commands { System.out.println("SQLException: " + e.getMessage()); while((e = e.getNextException()) != null) System.out.println(e.getMessage()); } return menu; } private Choice makePeopleMenu(String inIntialChoice) { Choice menu = new Choice(); Connection conn = null; // try-catch needed when connecting to the database in case of connection disruption try { // Java DataBase Connection driver DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); // passing in the parameters for the DB conenction conn = DriverManager.getConnection("jdbc racle:thin:@oracle.isg.computing.dcu.ie:1521 racle","username","password"); // creation and execution of the SQL query to check the username and password againt the DB Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT count(*) FROM people"); int count = 0; if(rs.next()) count = rs.getInt("count(*)"); String [] list = new String [count]; rs = stmt.executeQuery("SELECT fname, lname FROM people"); int i=0; while(rs.next() == true) { list[i] = rs.getString("fname"); list[i] += " " +rs.getString("lname"); i++; } for (i=0;i<count ;i++ ) { String theString =list[i]; menu.add(theString); } } catch(SQLException e) // catch Exceptions realating to SQL commands { System.out.println("SQLException: " + e.getMessage()); while((e = e.getNextException()) != null) System.out.println(e.getMessage()); } return menu; } Choice [] MakeCalendar() { Choice [] menu; dayList = makePeopleMenu("Choose"); for(int i =0;i<31;i++) dayList.add(""+i); dayList.setBounds( 190, 65, 150, 60 ); monthList = makePeopleMenu("Choose"); for(int i =0;i<12;i++) dayList.add(""+i); monthList.setBounds( 190, 65, 150, 60 ); yearList = makePeopleMenu("Choose"); for(int i =1600;i<2500;i++) dayList.add(""+i); yearList.setBounds( 190, 65, 150, 60 ); this.add(dayList); this.add(monthList); this.add(yearList); return null; } private void inputData() { } }
|
 |
Don Kiddick
Ranch Hand
Joined: Dec 12, 2002
Posts: 580
|
|
Why do you add some or your components to 2 panels ? e.g. in EventDetails.makeEventDetails this.add( eventList ); ... inputPanel.add( eventList ); This is not a good thing ! D.
|
 |
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
Originally posted by Don Kiddick: Why do you add some or your components to 2 panels ? e.g. in EventDetails.makeEventDetails this.add( eventList ); ... inputPanel.add( eventList ); This is not a good thing ! D.
Yeah sorry, should be only once, messed about with it so much i've forgotten what i'm doing, still doesn't solve the problem though
|
 |
Don Kiddick
Ranch Hand
Joined: Dec 12, 2002
Posts: 580
|
|
I think the problem is that you are mixing awt & swing. Here is a document that examines the problems of mixing awt and Swing. This is particularly pertinent :
When a lightweight component overlaps a heavyweight component, the heavyweight component is always on top, regardless of the relative z-order of the two components.
To fix, I suggest you use JComboBox instead of Choice. D. [ March 01, 2004: Message edited by: Don Kiddick ]
|
 |
Ritchie Partridge
Greenhorn
Joined: Mar 01, 2004
Posts: 17
|
|
Originally posted by Don Kiddick: I think the problem is that you are mixing awt & swing. Here is a document that examines the problems of mixing awt and Swing. This is particularly pertinent : To fix, I suggest you use JComboBox instead of Choice. D. [ March 01, 2004: Message edited by: Don Kiddick ]
Yep, that fixed it. Thanks a lot
|
 |
 |
|
|
subject: JTabbedPane
|
|
|
|