-2

I'm trying to add a JComponent based on what's in the .txt file for example

inside Component.txt

"[button]
buttonName
buttonText
Location(x,y)"

so how can i make a JButton named as buttonName, with a text of buttonText at the location x & y

here's my code:

private void btnRetrieveActionPerformed(java.awt.event.ActionEvent evt) {                                            
    String filename=txtFilename.getText()+".txt";
    int x=1;
    String text=null;
    String bname=new String(),btxt;
    double bsizex,bsizey,blocx,blocy;

    try (BufferedReader fw = new BufferedReader(new FileReader(new File(filename))))
    {
        String s;
        while((s = fw.readLine()) != null)
        {
            if(s.equals("[button]"))
            {
                String btnName = fw.readLine(); //read the next line after [a]
                JButton button1=new JButton(btnName);
                this.add(button1);
            }
        }
    }catch(IOException e)
    {
        e.printStackTrace();
    }
}                                           
tshepang
  • 12,111
  • 21
  • 91
  • 136
newbie07
  • 83
  • 3
  • 16
  • 2
    And your question would be? – MadProgrammer Jul 08 '14 at 05:05
  • sorry I forgot :D I've edited the post :D – newbie07 Jul 08 '14 at 05:10
  • would it be possible without using xml? – newbie07 Jul 08 '14 at 05:14
  • use [Property](http://docs.oracle.com/javase/tutorial/essential/environment/properties.html) file – A Stranger Jul 08 '14 at 05:18
  • @newbie07 It would be easier and would be more flexible – MadProgrammer Jul 08 '14 at 05:20
  • `Location(x,y)` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jul 08 '14 at 06:04
  • BTW: If you could use XML, there was an out-of-the-box solution: http://swixml.org/ – Marco13 Jul 08 '14 at 08:05

1 Answers1

2

You need to read each line after the expected marker until you have read everything you're expecting.

Once you have all the valid information, you need to parse the results and build the button, for example...

try (BufferedReader fw = new BufferedReader(new FileReader(new File(filename)))) {
    String s = null;
    while((s = fw.readLine()) != null) {
        if(s.equals("[button]")) {
            String btnName = fw.readLine();
            if (btnName != null) {
                String btnText = fw.readLine();
                if (btnText != null) {
                    String location = fw.readLine();
                    if (location != null) {
                        JButton button1=new JButton(btnText);
                        button1.setName(btnName);
                        //String xPos = location.substring(location.indexOf("(") + 1);
                        //String yPos = xPos.substring(xPos.indexOf(",") + 1);
                        //yPos = yPos.substring(0, yPos.indexOf(")"));
                        //xPos = xPos.substring(0, xPos.indexOf(","));
                        String parts[] = location.split(" ");
                        int x = Integer.parseInt(parts[0]);
                        int y = Integer.parseInt(parts[1]);
                        // Add this is where I tell you to use a layout manager...
                        this.add(button1);
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }
}catch(IOException e) {
    e.printStackTrace();
}

To be frank, this kind of thing is better suited to XML

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • hmm I was told not to use any other else ((: but thx anyway :D – newbie07 Jul 08 '14 at 05:31
  • Who told you not use any other `else`, those `else` statements are protecting your from ending up with an `IOException` as you read pass the end of the file – MadProgrammer Jul 08 '14 at 05:32
  • no I mean... I was told not to use XML and etc... :D – newbie07 Jul 08 '14 at 05:33
  • the .txt file contains "[button] BUTTON1 CLICK ME 30 10" and when i compile it it gives "Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -1" – newbie07 Jul 08 '14 at 05:36
  • Okay, that doesn't match the expectations of what you put in your question. Does all the text exist on a single line or are the split over multiple lines (the text for a single control) – MadProgrammer Jul 08 '14 at 05:37
  • multiple lines (: first line would be [button] second line would be [button name] third line would be [button text] fourth line is location(x,y) – newbie07 Jul 08 '14 at 05:39
  • So the location data would be more like `30 10` on a single line? – MadProgrammer Jul 08 '14 at 05:40
  • all the lines are in multiple except for location... I don't know why in comment I can't make it multiple lines.. sorry for confusion :D – newbie07 Jul 08 '14 at 05:43
  • thanks :D I figured out the first try... I tried setBounds ((: and it worked ((: – newbie07 Jul 08 '14 at 06:51
  • @newbie07 Be prepared for that to go horribly wrong – MadProgrammer Jul 08 '14 at 06:52