Page 2 of 11 FirstFirst 1234 ... LastLast
Results 21 to 40 of 219
Like Tree22Likes

Thread: How to do this?

  1. #21
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to do this?

    Quote Originally Posted by Tawi View Post
    How to give name to those elements and access them?
    Sorry, I do not understand you right now. I didn't change the data structure. I just changed the code a little bit so that it was easier to read (in my eyes). The controls still have the same name and they are still stored in your array:
    JTextField[] jawabAlternatif1 = new JTextField[100];

    All I wanted to show you is, how you could reduce the same code over and over by simply moving code a little around and building some (hopefully easier to understand) loops. But I didn't work on the whole code. You got a lot of replies and you should try to understand what we told you and simply play around with your code and try to change stuff as we sugegsted. (That is at least a nice practice even if you prefer it in a different way.)

    With kind regards,

    Konrad

  2. #22
    Tawi is offline Senior Member
    Join Date
    Feb 2013
    Posts
    118
    Rep Power
    0

    Default Re: How to do this?

    Quote Originally Posted by kneitzel View Post
    Sorry, I do not understand you right now. I didn't change the data structure. I just changed the code a little bit so that it was easier to read (in my eyes). The controls still have the same name and they are still stored in your array:
    JTextField[] jawabAlternatif1 = new JTextField[100];

    All I wanted to show you is, how you could reduce the same code over and over by simply moving code a little around and building some (hopefully easier to understand) loops. But I didn't work on the whole code. You got a lot of replies and you should try to understand what we told you and simply play around with your code and try to change stuff as we sugegsted. (That is at least a nice practice even if you prefer it in a different way.)

    With kind regards,

    Konrad
    Are you typing correct things? They are not working.

  3. #23
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to do this?

    Quote Originally Posted by Tawi View Post
    Are you typing correct things? They are not working.
    Indeed, you're supposed to hand over boiler plate code that can be copied and pasted without thinking; the OP has more important things to do you know, and after all, the OP repeatedly asked for an example, not hints or tips or anything; spoonfeeding boiler plate code is all that is wanted and needed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #24
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: How to do this?

    Quote Originally Posted by Tawi View Post
    Are you typing correct things? They are not working.
    I have no words.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #25
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: How to do this?

    Of course you do. But you'll be banned from the forum if use them. :)

    Regards,
    Jim
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #26
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to do this?

    Sorry, but I am not a native speaker and I am simply lost again.

    @Jos/gimbal2:
    I was used from other forums that it simply was ok to provide hints how something could have been done. So it was completly ok to just write some code parts directly inside the form without starting up an IDE to test around everything in detail. But I have no problem if that is not wanted at all in here.

    To this thread it would have meant that I simply pointed out that the add of the controls was missing and didn't bother at all about the rest.

    @Tiwi:
    I was just giving parts that was meant to show what I meant. I simply thought that you could get the point and then change your code yourself. The code was directly written inside the browser so there might be typing mistakes.

    But as requested: I simply did all the mentioned changes inside my IDE and made sure that everything compiles fine. I also added the button to the frame so it is visible.
    I didn't change anything with your event handling - that is something completly up to you now.

    Java Code:
    import javax.swing.*;
     
    public class NewClass extends JFrame {
        String[][] variabelTampung = new String[100][2];
        JTextField[] jawabAlternatif1 = new JTextField[100];
        JButton lanjut = new JButton("Lanjut");
        int x = 0;
        
        public void CreateElement(int element, int x, int y, int dx, int dy) {
        	jawabAlternatif1[element] = new JTextField();
        	this.add(jawabAlternatif1[element]);
        	jawabAlternatif1[element].setBounds(x,y,dx,dy);
        	jawabAlternatif1[element].setVisible(false);
        	jawabAlternatif1[element].setEnabled(false);
        }
        
        public NewClass(){
        	setSize(1024,768); 
        	this.setVisible(true);
        	setTitle("Penghitungan Data");
    	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    	    setLocationRelativeTo(null); 
    	   
    		setLayout(null);
    	
    		int startx = 506;
    		int starty = 150;
    		int dx = 101;
    		int dy = 30;
    		for (int rows = 0; rows < 5; rows++) { // Iterate through rows.
    			for (int column = 0; column < 5; column++) { // Iterate through columns
    				int element = rows * 5 + column; // Get the position inside the array.
    				CreateElement(element, startx + column * dx, starty + rows * dy, 100, 25);
    				CreateElement(element+25, startx + column * dx, starty + rows * dy, 100, 25); // We have to create 2 Controls!
    			}
    		}
    
    		this.add(lanjut);
    		lanjut.setBounds(910, 300, 100, 25);
    		lanjut.addActionListener(new java.awt.event.ActionListener() {         
                @Override 
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                        x++;
                        if (x == 1) {
                            for (int ma = 0; ma < 25; ma++) {
                            jawabAlternatif1[ma].setVisible(false);
                            jawabAlternatif1[ma].setEnabled(false);
                            }
                            for (int ma = 25; ma < 50; ma++) {
                            jawabAlternatif1[ma].setVisible(true);
                            jawabAlternatif1[ma].setEnabled(true);
                            }
                        }
                        else if (x == 2) {
                            kembali();
                        }
                    }
                public void kembali() {
                     for (int opi = 0; opi < 25; opi++) {
                         if (jawabAlternatif1[opi].getText() == null) {
                             for (int hui = 0; hui < 25; hui++) {
                                  jawabAlternatif1[hui].setVisible(true);
                                  jawabAlternatif1[hui + 25].setVisible(false);
                             }
                         }
                     }
                     for (int ou = 25; ou < 50; ou++) {
                         if (jawabAlternatif1[ou].getText() == null) {
                             for (int hui = 25; hui < 50; hui++) {
                                  jawabAlternatif1[hui].setVisible(true);
                                  jawabAlternatif1[hui - 25].setVisible(false);
                             }
                         }
                     }
                }           
        });
    }
    public static void main(String[] args) { 
            NewClass tesis = new NewClass();  
            tesis.setTitle("Program Pemilihan Obat dengan Cara Analytic Hierarchycal Process"); 
            tesis.setSize(1024,768); 
            tesis.setVisible(true); 
            tesis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            tesis.setLocationRelativeTo(null); 
            }
    }

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: How to do this?

    @kneitzel If you are going to provide sample code, another technique that needs to be shown is removing magic numbers.
    I see the same 25 and 50 in your code as the OP used. Perhaps some variables like:
    int StartGroup1 = 0, StartGroup2 = 25, StartGroup3 = 50
    then
    Java Code:
     for (int ou = 25; ou < 50; ou++) {
    // vs
     for (int ou = StartGroup2; ou < StartGroup3; ou++) {
    That's like you did with startx and starty

    Another question:
    Why are there calls to setVisible() and setSize() in two places?

    And another:
    Should tesis.setLocationRelativeTo(null) be before setVisible? As is it shows and then jumps on my PC.

    I was used from other forums that it simply was ok to provide hints how something could have been done. So it was completly ok to just write some code parts directly inside the form without starting up an IDE to test around everything in detail. But I have no problem if that is not wanted at all in here.
    Most of us agree with that approach: show hints but don't write full code.
    This OP is a special case that seems unable to understand hints and suggestions. Most of what we post as responses to this OP seem to be completely ignored. I'll leave it there and let the Dutch gang pick up the trail now.
    Last edited by Norm; 02-24-2014 at 10:30 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #28
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to do this?

    Hi Norm,

    you are correct. But I didn't did a full refactor of the code. I just wanted to provide a full working code that simply showed the stuff that I mentioned already.

    I see a lot more issues in the code that need to be fixed (which I will not provide here):
    - The Event code is far away from beeing optimized or correct. (Inside the first loop, the first 25 elements could be made visible multiple times. And then inside the next loop that could be reveresed multiple times.)
    - The array is 100 elements big but we only create 50 textboxes. (Could be a result form not changing magic numbers at all locations before. But that is guessing around.)

    But at least my code is now showing something which hopefully could be used to look at it with an debugger.

    With kind regards,

    Konrad

  9. #29
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to do this?

    Quote Originally Posted by kneitzel View Post
    Sorry, but I am not a native speaker and I am simply lost again.

    @Jos/gimbal2:
    I was used from other forums that it simply was ok to provide hints how something could have been done. So it was completly ok to just write some code parts directly inside the form without starting up an IDE to test around everything in detail. But I have no problem if that is not wanted at all in here.
    Nothing to worry about and your command of the English language is fine; I was just being sarcastic because this OP is beyond help (see his/her threads).

    kind regards,

    Jos
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  10. #30
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: How to do this?

    Quote Originally Posted by JosAH View Post
    I was just being sarcastic because this OP is beyond help (see his/her threads).
    Those were the words I was missing.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #31
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to do this?

    Hi all,

    Thank you for the clarification. I was just unsure, because when writing some small code parts inside the browser it is quite easy to make small bugs. But the intention is just to show, how something could be done and not a full code example (e.g. doing the homework if someone requests it :) )
    I will (of course) always try to make as less errors as possible but I normaly find time to check the forum in small pauses while at work and starting an IDE is not always possible.

    With kind regards,

    Konrad
    Corvanh likes this.

  12. #32
    Tawi is offline Senior Member
    Join Date
    Feb 2013
    Posts
    118
    Rep Power
    0

    Default Re: How to do this?

    Quote Originally Posted by kneitzel View Post
    Sorry, but I am not a native speaker and I am simply lost again.

    @Jos/gimbal2:
    I was used from other forums that it simply was ok to provide hints how something could have been done. So it was completly ok to just write some code parts directly inside the form without starting up an IDE to test around everything in detail. But I have no problem if that is not wanted at all in here.

    To this thread it would have meant that I simply pointed out that the add of the controls was missing and didn't bother at all about the rest.

    @Tiwi:
    I was just giving parts that was meant to show what I meant. I simply thought that you could get the point and then change your code yourself. The code was directly written inside the browser so there might be typing mistakes.

    But as requested: I simply did all the mentioned changes inside my IDE and made sure that everything compiles fine. I also added the button to the frame so it is visible.
    I didn't change anything with your event handling - that is something completly up to you now.

    Java Code:
    import javax.swing.*;
     
    public class NewClass extends JFrame {
        String[][] variabelTampung = new String[100][2];
        JTextField[] jawabAlternatif1 = new JTextField[100];
        JButton lanjut = new JButton("Lanjut");
        int x = 0;
        
        public void CreateElement(int element, int x, int y, int dx, int dy) {
        	jawabAlternatif1[element] = new JTextField();
        	this.add(jawabAlternatif1[element]);
        	jawabAlternatif1[element].setBounds(x,y,dx,dy);
        	jawabAlternatif1[element].setVisible(false);
        	jawabAlternatif1[element].setEnabled(false);
        }
        
        public NewClass(){
        	setSize(1024,768); 
        	this.setVisible(true);
        	setTitle("Penghitungan Data");
    	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    	    setLocationRelativeTo(null); 
    	   
    		setLayout(null);
    	
    		int startx = 506;
    		int starty = 150;
    		int dx = 101;
    		int dy = 30;
    		for (int rows = 0; rows < 5; rows++) { // Iterate through rows.
    			for (int column = 0; column < 5; column++) { // Iterate through columns
    				int element = rows * 5 + column; // Get the position inside the array.
    				CreateElement(element, startx + column * dx, starty + rows * dy, 100, 25);
    				CreateElement(element+25, startx + column * dx, starty + rows * dy, 100, 25); // We have to create 2 Controls!
    			}
    		}
    
    		this.add(lanjut);
    		lanjut.setBounds(910, 300, 100, 25);
    		lanjut.addActionListener(new java.awt.event.ActionListener() {         
                @Override 
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                        x++;
                        if (x == 1) {
                            for (int ma = 0; ma < 25; ma++) {
                            jawabAlternatif1[ma].setVisible(false);
                            jawabAlternatif1[ma].setEnabled(false);
                            }
                            for (int ma = 25; ma < 50; ma++) {
                            jawabAlternatif1[ma].setVisible(true);
                            jawabAlternatif1[ma].setEnabled(true);
                            }
                        }
                        else if (x == 2) {
                            kembali();
                        }
                    }
                public void kembali() {
                     for (int opi = 0; opi < 25; opi++) {
                         if (jawabAlternatif1[opi].getText() == null) {
                             for (int hui = 0; hui < 25; hui++) {
                                  jawabAlternatif1[hui].setVisible(true);
                                  jawabAlternatif1[hui + 25].setVisible(false);
                             }
                         }
                     }
                     for (int ou = 25; ou < 50; ou++) {
                         if (jawabAlternatif1[ou].getText() == null) {
                             for (int hui = 25; hui < 50; hui++) {
                                  jawabAlternatif1[hui].setVisible(true);
                                  jawabAlternatif1[hui - 25].setVisible(false);
                             }
                         }
                     }
                }           
        });
    }
    public static void main(String[] args) { 
            NewClass tesis = new NewClass();  
            tesis.setTitle("Program Pemilihan Obat dengan Cara Analytic Hierarchycal Process"); 
            tesis.setSize(1024,768); 
            tesis.setVisible(true); 
            tesis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            tesis.setLocationRelativeTo(null); 
            }
    }
    Sorry sirs if this harm your feeling but this is not working either, what is wrong?

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: How to do this?

    this is not working either,
    Please explain. If there are error messages, copy the full text and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  14. #34
    Tawi is offline Senior Member
    Join Date
    Feb 2013
    Posts
    118
    Rep Power
    0

    Default Re: How to do this?

    I do not know. The Java produces no error messages. It just give red mark to them after I merge the code in to my code, what is wrong? That is the reason I go back to my code.
    Last edited by Tawi; 03-02-2014 at 03:21 PM.

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: How to do this?

    It just give red mark
    What does that mean?
    Sorry,I can not see any red mark here.

    Can you compile the code and copy the full text of the compiler's error messages and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  16. #36
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to do this?

    Quote Originally Posted by Tawi View Post
    I do not know. The Java produces no error messages. It just give red mark to them after I merge the code in to my code, what is wrong? That is the reason I go back to my code.
    Those red marks are an indication of compilation errors; you can click in the left margin of those lines to see the error messages.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #37
    Tawi is offline Senior Member
    Join Date
    Feb 2013
    Posts
    118
    Rep Power
    0

    Default Re: How to do this?

    I have no idea, it says that int two times require but four int can be found, so? How to solve this?
    Last edited by Tawi; 03-02-2014 at 04:18 PM. Reason: After I change it, I can see the JTextFields already.

  18. #38
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: How to do this?

    You need to:
    Copy the full text of the compiler's error message and paste it here
    AND
    Copy the program code and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  19. #39
    Tawi is offline Senior Member
    Join Date
    Feb 2013
    Posts
    118
    Rep Power
    0

    Default Re: How to do this?

    I have already revised it. Do I need to dump the code in here or put the code in Ziddu? Although I have revised it since several days ago, Java still marked it with red at the tab, why? At the time I compile it, java produces the message that there are errors but does not show where so, what do I have to do?
    Last edited by Tawi; 03-03-2014 at 06:55 AM.

  20. #40
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to do this?

    Hi,

    I think we are running around in circles with asking for the same information over and over.

    You said, that my code does not work? Can you give details about what you are doing and what exact messages you get?

    Some simple instructions, what you could do:
    a) Go and mark all my code (And just the code!)
    b) Create a new file with a text editor called NewClass.java and copy everything in there.
    (E.g. use Notepad on windows. Make sure that the Filename is really NewClass.java. On windows systems. an editor might want to add a .txt and on unix systems, names are case sensitive so you have to be sure that the spelling is ok.)
    c) Now compile it. Simply call
    javac NewClass.java
    You should not get any message back if everything is correct. If you made an mistake, then you get an error and you can copy the error in here. That could look like:
    konrad@eeepc:~/tmp$ javac Newclass.java
    Newclass.java:3: error: class NewClass is public, should be declared in a file named NewClass.java
    public class NewClass extends JFrame {
    ^
    1 error
    konrad@eeepc:~/tmp$
    (So as an example I just renamed the file to something wrong! Read the error message: You see that the error is explained!)
    d) Start the application:
    java NewClass
    Now a windows should open which is empty with just a single button visible. When you click this button, some input-boxes are shown.

    Please be aware: All the logic inside the application is still YOUR logic. All I changed is a little bit the structure of your code so that it is easier to maintain and I added a few add-Calls so that the new controls are added to the Frame which was missing.
    It is just meant as a starting point for you where you could start using a debugger to see what is going on to fix YOUR logic. All I did was taking some smaller things out of your way so that you have a point where something is visible. (And I would suggest to always start slowly. Do small steps and test them. That way you do not make stuff to complex for your. One big rule in software development is Keep It Simple, Stupid - KISS.)

    With kind regards,

    Konrad

Page 2 of 11 FirstFirst 1234 ... LastLast

Posting Permissions

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