Results 1 to 20 of 20
- 09-20-2009, 08:26 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
how to getJTabbedPane in other class
Dear All,
I have the main class which declared the JFrame...called AFMB.java
------AFMB.JAVA-------------------
Java Code:public AFMB() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setup(); pack(); addWindowListener(new WindowEventHandler()); show(); } private void setup() { setJMenuBar(fsu.setupMenuBar()); contentPane.add(tabbedPane, BorderLayout.CENTER); tabbedPane.addTab("Login", sp.showPane()); }
the other class called:
-------DATABASE.java----------
I would like to Remove the Login Tabbed (0), and display the new tabbed("My Database")...but it does not work..Java Code:{ String param3 = r.getString(3).trim(); System.out.println(param3); if (param3.equals("accounts")) { foundrec = 1; tabbedPane.removeTabAt(0); tabbedPane.addTab("My Database", sp4.showpane4()); } else if(param3.equals("principal")) { foundrec = 1; //will show other JTabbedPane.... } }
Please help how to add the new tab from the main class.AFMB...
Thank you for the help...Last edited by javamula; 09-20-2009 at 08:37 PM. Reason: Not completed before posting
- 09-20-2009, 08:30 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Do you have a question then?
-
Hm, I think that we're supposed to guess at this,... something I'm no good at.
To the original poster, you may need to supply us with more information (a lot more) and perhaps more code. When posting code, please use code tags as this will make your code much more readable (please see my signature for more on this). Finally, in a little bit, I'm going to move this thread over to the Swing forum in order to better align its content to the forum.
Much luck!
- 09-20-2009, 08:41 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
sorry for not finishing the post...but i have edited them...
Please help...TIA
- 09-20-2009, 08:44 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Oh great, you've now added a question.
Put some println statements to verify that the remove is being run at all.
You also need to make the interface repaint itself after making changes.
-
Hm, it's still not nearly enough information to know how your app is set up, what you're doing wrong, etc...
Often problems of this type are solved by getting a reference to the object of interest and calling its methods, and another solution is to use a Observer/Observable type pattern, but I have absolutely no idea of how to implement any of this in your current situation given the information provided. Maybe someone else (r035198x?) does.
Much luck.
-
For instance, you could give your main class (AFMB?) public methods such as removeLoginTab() and addTab(...) or even a more specific addDatabaseTab(), and then have your Database class hold a reference to the main object and call the appropriate methods on the main object when necessary. This would work, but it may couple the two classes together more tightly than you'd like. Another option is allow outside classes to add a "Control" object to the Database object, a listener (ActionListener for instance) object, that will do the above for you. In this case the Database class needs to know nothing about the AFMB class. Again, more specific advice can possibly be given if you provide us with more information.
Much luck
- 09-20-2009, 09:07 PM #8
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
yeap....the code...is too long to be posted... but here i try to excerpt it...
------AFMB.Java------------------------
then LOGIN button it will call the method accessDB (in DatabaseB.java) with the following code.Java Code:public static void main(String args[]){ AFMB app = new AFMB(); app.setSize (300,300); app.setTitle("The title"); } public AFMB() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setup(); pack(); addWindowListener(new WindowEventHandler()); show(); } private void setup() { setJMenuBar(fsu.setupMenuBar()); // go to another class FSU.JAVA...which just set up the Menu Bar... contentPane.add(tabbedPane, BorderLayout.CENTER); tabbedPane.addTab("Login", sp.showPane()); // go to another class to display the LOGIN for entering the username and password. //here the LOGIN tab (Tab index 0) is successfully to be displayed... }
Java Code:public void accessDB(String var1,String var2) { try { // <editor-fold defaultstate="collapsed" desc="Connection"> ConnectionJDBC cj = new ConnectionJDBC(); Connection conn; Statement statement; // </editor-fold> String varName = var1; String varPass = var2; String sql = "SELECT * FROM login WHERE username='"+varName +"' AND password='"+varPass +"'"; conn = cj.makeConnection(); statement = conn.createStatement(); boolean hasResults = statement.execute(sql); if(hasResults) { ResultSet result = statement.getResultSet(); if(result!=null) { displayResults(result); } conn.close(); } } catch(Exception ex){} } public void displayResults(ResultSet r) throws SQLException { JPanel jpa = new JPanel(); ResultSetMetaData rmeta = r.getMetaData(); int foundrec = 0; int numColumns = rmeta.getColumnCount(); while(r.next()) { String param3 = r.getString(3).trim(); System.out.println(param3); if (param3.equals("accounts")) { foundrec = 1; ///HERE IS MY PROBLEM...cannot add new tab (My Database) and remove tabbedPane (index =0)...how to call JTabbedPane name tabbedPane from AFMB Class????? tabbedPane.addTab("My Database", sp4.showpane4()); tabbedPane.removeTabAt(0); } else if(param3.equals("principal")) { foundrec = 1; // Will code later to show another Tabbed Pane/database... } } if(foundrec==0){ String dialogmessage = "Please Re-Login"; dialogtype = JOptionPane.INFORMATION_MESSAGE; JOptionPane.showMessageDialog((Component) null, dialogmessage, "Login Failed", dialogtype); } } }
Everything is ok, including accessing the database file...because the "FOUNDREC = TRUE / 1"..
if need to repaint...how to do it...Please guide further...
See the Comment above as follows:
///HERE IS MY PROBLEM...cannot add new tab (My Database) and remove tabbedPane (index =0)...how to call JTabbedPane name tabbedPane from AFMB Class?????
-
As I mentioned, you have a reference problem. You call tabbedPane.removeTabAt(0) from within the DatabaseB class as if tabbedPane were a variable within this class, but it isn't. Some possible solutions are as I mentioned above:
Your DatabaseB class could have a a reference to the AFMB class and call a public method in this class (a method that you must first create) that removes the tab at 0.
Or you could use an Observer Pattern here and have a Control class that does this for you.
- 09-20-2009, 09:19 PM #10
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
Dear Fubarable,
I will try your advise to make the new method in main class - AFMB (removeLoginTab() and addTab(...)
but before that can i make a new instances of tabbedPane from Declaration in AFMB relating to tabbedPane?
JTabbedPane tabbedPane = new JTabbedPane();
I have tried it...in Database.Java as follows:
JTabbedPane jtp = new JTabbedPane();
AFMB af;
jtp = af.tabbedPane
jtp.removetab(0);
jtp.addtab("My Database", sp4.showPane4());
but it does not work? please guide further...
TIQ.
-
Here's an example of my first suggestion:
MainAFMB.java
Java Code:import java.awt.*; import javax.swing.*; public class MainAFMB { private JTabbedPane tabbedPane = new JTabbedPane(); public MainAFMB() { tabbedPane.setPreferredSize(new Dimension(300, 300)); LoginPane loginPane = new LoginPane(this); // pass reference to main class here! tabbedPane.addTab("Login", loginPane.getMainComponent()); } public JComponent getMainComponent() { return tabbedPane; } // give this class a public method to allow other classes to remove the Login tab public void removeLoginTab() { String title = tabbedPane.getTitleAt(0); if (title.equals("Login")) { // first check to see if the tab is still there tabbedPane.remove(0); // then remove it } } // and allow other classes to add tabs. public void addTab(String title, JComponent component) { tabbedPane.addTab(title, component); } private static void createAndShowUI() { JFrame frame = new JFrame("Main"); frame.getContentPane().add(new MainAFMB().getMainComponent()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
LoginPane.java
Note that this posted code is compilable and runnable, something which if you do when asking questions here, makes it 1000 times easier to consider.Java Code:import java.awt.Color; import java.awt.event.*; import javax.swing.*; class LoginPane { private JPanel mainPanel = new JPanel(); private MainAFMB main; // the constructor accepts a reference to the main class public LoginPane(MainAFMB main) { this.main = main; JButton swapBtn = new JButton("Swap"); swapBtn.addActionListener(new SwapListener()); mainPanel.add(swapBtn); } public JComponent getMainComponent() { return mainPanel; } private class SwapListener implements ActionListener { // this class uses the reference to the main class here, in the button's action listener public void actionPerformed(ActionEvent e) { main.removeLoginTab(); // remove the login tab JPanel pinkPanel = new JPanel(); pinkPanel.setBackground(Color.pink); main.addTab("DataBase", pinkPanel); add a new tab } } }Last edited by Fubarable; 09-20-2009 at 09:35 PM.
- 09-20-2009, 09:38 PM #12
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
Thank you for the help and will try to adopt your code....
Will advise at the earlier...when i succeed in...
- 09-20-2009, 09:44 PM #13
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Well database access classes (the model) should know nothing about JFrames (the view).
Read this popular article I created a while ago.
- 09-20-2009, 09:55 PM #14
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Since Fubi has posted some code, here's my example
Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.SwingUtilities; class DyingTab { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { deadTab(); } }); } public static void deadTab() { JFrame frame = new JFrame("Frame "); frame.setSize(300, 300); final JTabbedPane tabs = new JTabbedPane(); JPanel logIn = new JPanel(); final JButton log = new JButton("Click me and I'm dead"); log.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // In this approach, your view know about your model // I'll leave it as an exercise to decouple your view // and your model. if (new LogInCheck().login("test", "test")) { tabs.remove(0); log.setText("Look ma, a new one!"); JPanel newOne = new JPanel(); newOne.add(log); tabs.add(newOne); } } }); logIn.add(log); tabs.add(logIn); frame.add(tabs); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } // In some db access package or business logic package somewhere class LogInCheck { public boolean login(String userName, String password) { // do some fancy SQL foot work System.out.println("Attempting SQL check"); return true; } }
-
- 09-20-2009, 10:02 PM #16
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
thank you for both of you...
highly appreciated that...still figuring out the code and article....:)
- 09-20-2009, 10:18 PM #17
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You won't grasp everything in the article right now but you should be able to pick up a few things right away. The example I posted above though should make a lot more sense.
Your LogInCheck class can be used by programs that use different views (JSPs,mobile applications e.t.c). They would all be accessing the same model so only write that code once and secure it in one place. The views may choose to use the model objects (not very elegant IMO) or they may go through controllers. Separating the view from the model makes the view itself reusable as well.
- 09-22-2009, 08:57 AM #18
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
Dear Fubarable / r035198x / All,
After so many hours trying to figure out how to remove the Tab(0) and add new Tab. still not successfully, please help.
as your advice...i miss the object reference which i cannot solve.
Here is the simplify version of the code...
AFMB class
MainTabbedPane ClassJava Code:package JavaForum; import javax.swing.*; public class AFMB extends JFrame { public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } private static void createAndShowUI() { JFrame frame = new JFrame("The Winning Team - Purple Solution"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setSize(800,400); frame.getContentPane().add(new MainTabbedPane().getMainComponent()); frame.setVisible(true); } }
LoginPane ClassJava Code:package JavaForum; import java.awt.*; import javax.swing.*; public class MainTabbedPane{ private String title; private JTabbedPane tabbedPane = new JTabbedPane(); AFMB af; public MainTabbedPane(){ tabbedPane.setPreferredSize(new Dimension(300, 300)); LoginPane loginPane=new LoginPane(); tabbedPane.addTab("Login",loginPane.showLoginPane()); setTitle(); } public JTabbedPane getMainComponent() { return tabbedPane; } public void removeLoginTab(){ title = tabbedPane.getTitleAt(0); if (title.equals("Login")) { tabbedPane.remove(0); } } public void addTab(String title, JComponent component){ tabbedPane.addTab(title, component); } public String getTitle(){ return title; } public void setTitle(){ title = tabbedPane.getTitleAt(0); } }
ButtonHandlerB Class (THE Class needed the help...PLEASE).Java Code:package JavaForum; import java.awt.*; import javax.swing.*; public class LoginPane{ // <editor-fold defaultstate="collapsed" desc="Declaration"> public JTextField login = new JTextField("First",30); public JTextField password = new JTextField("Second",30); private JPanel p1 = new JPanel(); private Font titleFont = new Font("courier new", Font.BOLD, 14); private Font dataFont = new Font("courier new", Font.PLAIN, 12); private JButton button = new JButton("LOGIN"); private GridBagLayout gridbag = new GridBagLayout(); private GridBagConstraints c = new GridBagConstraints(); private JLabel jLabel1, jLabel2 = new JLabel(); // </editor-fold> public JPanel showLoginPane(){ p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Login Panel")); p1.setFont(titleFont); p1.setLayout(gridbag); jLabel1 = new JLabel("Enter Username : "); jLabel1.setFont(dataFont); c.weighty = 0.0; c.ipady = 2; c.ipadx = 2; c.anchor = GridBagConstraints.WEST; c.gridwidth = 1; c.gridx = 0; c.gridy = 0; gridbag.setConstraints(jLabel1, c); p1.add(jLabel1); login = new JTextField(" "); c.ipady = 2; c.ipadx = 2; c.weighty = 0.0; c.anchor = GridBagConstraints.WEST; c.gridwidth = 2; c.gridx = 1; c.gridy = 0; gridbag.setConstraints(login, c); p1.add(login); jLabel2 = new JLabel("Enter Password : "); jLabel2.setFont(dataFont); c.ipady = 2; c.ipadx = 2; c.weighty = 0.0; c.anchor = GridBagConstraints.WEST; c.gridwidth = 1; c.gridx = 0; c.gridy = 1; gridbag.setConstraints(jLabel2, c); p1.add(jLabel2); password = new JTextField(" "); c.ipady = 2; c.ipadx = 2; c.weighty = 0.0; c.anchor = GridBagConstraints.WEST; c.gridwidth = 2; c.gridx = 1; c.gridy = 1; gridbag.setConstraints(password, c); p1.add(password); c.ipady = 2; c.ipadx = 2; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER; c.gridwidth = 1; c.gridx = 1; c.gridy = 2; gridbag.setConstraints(button, c); p1.add(button); button.addActionListener(new ButtonHandlerB(this)); return p1; } public String getName(){ return login.getText().trim(); } public String getPass(){ return password.getText().trim(); } public void setName(String li){ login.setText(li); } public void setPass(String pw){ this.password.setText(pw); } }
Java Code:package JavaForum; import java.awt.event.*; import javax.swing.*; public class ButtonHandlerB implements ActionListener{ LoginPane lp = new LoginPane(); JPanel jpa = new JPanel(); JTabbedPane jtp = new JTabbedPane(); MainTabbedPane mtp; public ButtonHandlerB (LoginPane LP){ } public void actionPerformed(ActionEvent ev){ String L=ev.getActionCommand(); if(L.equals("LOGIN")){ System.out.println(new MainTabbedPane().getTitle()); [COLOR="Red"][B]//mtp.removeLoginTab(); // Does not work...Please help //mtp.addTab("test", jpa); // Does not work....Plese help[/B][/COLOR] } } }
- 09-22-2009, 10:18 AM #19
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Your LogInPane class is misleading. It does not represent a Pane at all.
Java Code:import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Observable; import java.util.Observer; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.JTextField; class AFMB extends JFrame { public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } private static void createAndShowUI() { JFrame frame = new JFrame("The Winning Team - Purple Solution"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setSize(800, 400); frame.getContentPane().add(new MainTabbedPane().getMainComponent()); frame.setVisible(true); } } class MainTabbedPane { private String title; private JTabbedPane tabbedPane = new JTabbedPane(); AFMB af; public MainTabbedPane() { tabbedPane.setPreferredSize(new Dimension(300, 300)); LoginPane loginPane = new LoginPane(new ButtonHandlerB(this)); tabbedPane.addTab("Login", loginPane); setTitle(); } public JTabbedPane getMainComponent() { return tabbedPane; } public void removeLoginTab() { title = tabbedPane.getTitleAt(0); if (title.equals("Login")) { tabbedPane.remove(0); } } public void addTab(String title, JComponent component) { tabbedPane.addTab(title, component); } public String getTitle() { return title; } public void setTitle() { title = tabbedPane.getTitleAt(0); } } class LoginPane extends JPanel { // <editor-fold defaultstate="collapsed" desc="Declaration"> public JTextField login = new JTextField("First", 30); public JTextField password = new JTextField("Second", 30); private JPanel mainPanel = new JPanel(); private Font titleFont = new Font("courier new", Font.BOLD, 14); private Font dataFont = new Font("courier new", Font.PLAIN, 12); private JButton button = new JButton("LOGIN"); private GridBagLayout gridbag = new GridBagLayout(); private GridBagConstraints constraints = new GridBagConstraints(); private JLabel userNameLabel, jLabel2 = new JLabel(); // </editor-fold> public LoginPane(ButtonHandlerB listener) { mainPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Login Panel")); mainPanel.setFont(titleFont); mainPanel.setLayout(gridbag); userNameLabel = new JLabel("Enter Username : "); userNameLabel.setFont(dataFont); constraints.weighty = 0.0; constraints.ipady = 2; constraints.ipadx = 2; constraints.anchor = GridBagConstraints.WEST; constraints.gridwidth = 1; constraints.gridx = 0; constraints.gridy = 0; gridbag.setConstraints(userNameLabel, constraints); mainPanel.add(userNameLabel); login = new JTextField(" "); constraints.ipady = 2; constraints.ipadx = 2; constraints.weighty = 0.0; constraints.anchor = GridBagConstraints.WEST; constraints.gridwidth = 2; constraints.gridx = 1; constraints.gridy = 0; gridbag.setConstraints(login, constraints); mainPanel.add(login); jLabel2 = new JLabel("Enter Password : "); jLabel2.setFont(dataFont); constraints.ipady = 2; constraints.ipadx = 2; constraints.weighty = 0.0; constraints.anchor = GridBagConstraints.WEST; constraints.gridwidth = 1; constraints.gridx = 0; constraints.gridy = 1; gridbag.setConstraints(jLabel2, constraints); mainPanel.add(jLabel2); password = new JTextField(" "); constraints.ipady = 2; constraints.ipadx = 2; constraints.weighty = 0.0; constraints.anchor = GridBagConstraints.WEST; constraints.gridwidth = 2; constraints.gridx = 1; constraints.gridy = 1; gridbag.setConstraints(password, constraints); mainPanel.add(password); constraints.ipady = 2; constraints.ipadx = 2; constraints.weighty = 0.0; constraints.anchor = GridBagConstraints.CENTER; constraints.gridwidth = 1; constraints.gridx = 1; constraints.gridy = 2; gridbag.setConstraints(button, constraints); button.addActionListener(listener); mainPanel.add(button); add(mainPanel); } @Override public String getName() { return login.getText().trim(); } public String getPass() { return password.getText().trim(); } @Override public void setName(String li) { login.setText(li); } public void setPass(String pw) { this.password.setText(pw); } } class ButtonHandlerB implements ActionListener { // LoginPane lp = new LoginPane(); // JPanel jpa = new JPanel(); // JTabbedPane jtp = new JTabbedPane(); MainTabbedPane tabbedPane; // we want to close tabs so we pass the tabbed pane public ButtonHandlerB(MainTabbedPane tabbedPane) { this.tabbedPane = tabbedPane; } public void actionPerformed(ActionEvent ev) { String L = ev.getActionCommand(); if (L.equals("LOGIN")) { // System.out.println(new MainTabbedPane().getTitle()); tabbedPane.removeLoginTab(); } } }
- 09-22-2009, 10:39 AM #20
Member
- Join Date
- Sep 2009
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 07:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks