Results 1 to 8 of 8
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default JButton click problem

    Hi, here again :). I have problem with JButton.
    Theses is what I expect to happen when I click JButton(VIEW):
    1. Check JCombo box' selectedindex.
    2. resize appropriate panel for the selected index in JCombo box
    3. Do some SELECT query.
    4. Show to JTable and resize JTable.
    Here is the code:
    Java Code:
        //[b]activeTable() - check the Jcombobox' index and resize the right panel and hide other panel[/b]
        private void [b]activeTable()[/b]{
            if(cmbTables.getSelectedIndex() == 0)
            {   pnlStocks.setVisible(true);
                pnlStocks.setEnabled(true);
                pnlStocks.setSize(670, 220);
                pnlStocks.setLocation(6, 97);
            }
            else
            {   pnlStocks.setVisible(false);
                pnlStocks.setEnabled(false);
                pnlStocks.setSize(1, 1);
                pnlStocks.setLocation(0, 1);
            }
    
            if(cmbTables.getSelectedIndex() == 1)
            {   pnlReq.setVisible(true);
                pnlReq.setEnabled(true);
                pnlReq.setSize(670, 220);
                pnlReq.setLocation(6, 97);
            }
            else
            {   pnlReq.setVisible(false);
                pnlReq.setEnabled(false);
                pnlReq.setSize(1, 1);
                pnlReq.setLocation(0, 1);
            }
        }
    
        //[b]JButton click event[/b]
        private void [b]btnViewClicked(java.awt.event.ActionEvent evt)[/b] {
            String myQuery=null;
            [i]activeTable();[/i]
    
            switch (cmbTables.getSelectedIndex())
            {   case 0:
                    [b][i]//do something.
                    //If I choose this in JCombo and click this button everything works fine[/i][/b]
                    break;
                case 1:
                    [b][i]//If I choose this in JCombo and click this button then the panel did not resize
                    //but on the second time that I click it the panel will show..[/i][/b]
                    myQuery = "SELECT * FROM mytable";
                    String[] reqHeader = {"TITLE#1", "TITLE#2", "TITLE#3", "TITLE#4"};
                    MaterialsQuery.selectMat(myQuery, tbl_Mat, reqHeader);
                    resizeReqTable();
                    break;
            }
        }
    Thanks for always helping me,
    geje
    Last edited by mine0926; 06-08-2010 at 08:28 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure what your problem is based on the code as shown, but perhaps you need to do your database work in a background thread such as by using a SwingWorker. If this doesn't help you, then consider creating a very small program (without netbeans generated code if possible) that show your problem.

    As an aside, you will likely find that by using an appropriate layout manager or combination of managers and calling pack() on your GUI, you will have an easier time of having your app resize as appropriate.

    Much luck!

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I copy the codes(except to the generated codes) and paste it in notepad then run the program through command prompt. Everything works as expected. You think I am having problem with Layouts and grouping the netbeans generated?

    Thanks for the reply and for pointing me in SwingWorker:D.

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Everything is fine now and working as expected. I just change JFrame and JPanels layout from Free Design to Null Layout.

    Thanks. :)
    geje

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mine0926 View Post
    Everything is fine now and working as expected. I just change JFrame and JPanels layout from Free Design to Null Layout.
    This is not what I suggested, and I do not recommend this. Rather study the more user friendly layouts (not group layout) and use those. If you do this, it will help you more in the long run, trust me.

    Luck.

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Hi,
    pardon, I misunderstood your answer. I still use Null Layout in Design mode, should I put it back in Free Design?. But in code I use some layout manegers and add JPanels inside JPanels to set components alignment.

    Right now I am having problem aligning it but I will solve it myself first then if I cant really find how to do things then I will post back here.

    Thanks for pointing me.
    geje

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you use one or more of the user-friendly layout managers in one or more nested JPanels such as BorderLayout, BoxLayout, FlowLayout, GridLayout, and if necessary, GridBagLayout.

  8. #8
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Thats what I did... or am I getting thing the other way?
    At first I did not add pnlStocks(JPanel) and pnlReq(JPanel) in pnlSecnd.
    Java Code:
        public frmMaterials()
        {
    
            this.setLayout(new BorderLayout());
            this.add(pnlFirst, BorderLayout.PAGE_START);
            this.add(pnlSecnd, BorderLayout.CENTER);
            this.add(jScrollPane1, BorderLayout.PAGE_END);
    
            pnlFirst.setLayout(new GridLayout(0,2));
            pnlFirst.add(jPanel1);
            pnlFirst.add(jPanel2);
    
            pnlSecnd.setLayout(new BorderLayout());
            pnlSecnd.add(jPanel3,BorderLayout.LINE_END);
    
            pnlStocks.setVisible(false);
            pnlReq.setVisible(false);
        }

    then in button click
    Java Code:
            activeTable();
            ...

    activeTable() method remove and add JPanels.
    Java Code:
        private void activeTable(){
            if(cmbTables.getSelectedIndex() == 0)
            {   pnlStocks.setVisible(true);
                pnlSecnd.add(pnlStocks, BorderLayout.LINE_START);
            }
            else
            {   pnlStocks.setVisible(false);
                pnlSecnd.remove(pnlStocks);
            }
    
            if(cmbTables.getSelectedIndex() == 1)
            {   pnlReq.setVisible(true);
                pnlSecnd.add(pnlReq, BorderLayout.LINE_START);
            }
            else
            {   pnlReq.setVisible(false);
                pnlSecnd.remove(pnlReq);
            }
        }
    Thanks,
    geje

Similar Threads

  1. Problem with click to move ball algorithm
    By Laythe in forum Java Applets
    Replies: 1
    Last Post: 12-19-2009, 01:00 AM
  2. JButton Problem
    By wassim in forum AWT / Swing
    Replies: 6
    Last Post: 02-18-2009, 11:29 PM
  3. Problem in mouse click n repaint
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 07-04-2008, 12:16 PM
  4. how to click a jbutton and open an url
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 11:44 PM
  5. Problem with JButton
    By Marcus in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •