Results 1 to 16 of 16
  1. #1
    braskez is offline Member
    Join Date
    Oct 2012
    Posts
    14
    Rep Power
    0

    Default AttributedString?

    Java Code:
    	
    String[] cmds = {"exit", "keystrokes", "screenshots", "auditinfo [caller name]", "callerprofile [caller name]", "about", "commandbook", "editcaller [caller name]", "newcaller", "options", "devOptions"};
    
    String[] cmdExp = {" Closes the program", "prints all the keystrokes for that specific caller.", "prints screenshots taken of that specific caller's screen.", "prints the number of audits that specific caller has booked.", "prints general information about the caller.", "opens the about window.", "opens the commandbook.", "opens a window that allows you to edit the caller's info.", "creates a new caller and lets you add info about he/she.", "Opens the options menu.", "Opens the developer menu."};
    
    		for(int i = 0;i<=10;i++){
    	    	cmdattribute[i] = new AttributedString(cmds[i]);
    	    	cmdEXPattribute[i] = new AttributedString(cmdExp[i]);
    	        }
    I have no idea what I am doing wrong here. Also I declared cmdattribute and cmdEXPattribute earlier.
    Java Code:
    public AttributedString[] cmdattribute = null;
    public AttributedString[] cmdEXPattribute = null;
    Here is the error I get:

    Any advice is appreciated, thanks

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: AttributedString?

    You have to create an array object / allocate memory for 11 AttributeString objects!

    -> Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    braskez is offline Member
    Join Date
    Oct 2012
    Posts
    14
    Rep Power
    0

    Default Re: AttributedString?

    I tried doing the following:
    Java Code:
    public AttributedString[] cmdattribute = AttributedString[11];
    public AttributedString[] cmdEXPattribute = AttributedString[11];
    But it just told me that AttributedString couldn't be resolved to a variable.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: AttributedString?

    did you read the link? It appears not!

    new AttributedString[11] !

  5. #5
    braskez is offline Member
    Join Date
    Oct 2012
    Posts
    14
    Rep Power
    0

    Default Re: AttributedString?

    Thanks, I'm closer to where I need to be, but I am trying to drawString the attributed string, but I am not sure how to specify which JFrame I want it to draw to. The one I want it to draw to is named 'commandBookWindow'.
    I tried
    Java Code:
    			commandBookWindow.add(g.drawString(cmdattribute[0].getIterator(), 30, 30));
    			commandBookWindow.add(g.drawString(cmdEXPattribute[0].getIterator(),30,30));
    but that didn't work at all. Any ideas?

  6. #6
    braskez is offline Member
    Join Date
    Oct 2012
    Posts
    14
    Rep Power
    0

    Default Re: AttributedString?

    Anyone?

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

    Default Re: AttributedString?

    I would recommend that you "draw" your String in a JLabel via its setText(...) method rather than drawing with a Graphics object. If you're required to use the latter due to homework requirements, then you'd draw in a JPanel's paintComponent method and would use the Graphics object passed into the method's parameter from the JVM. The Swing Performing Custom Painting Tutorial will tell you more about how to draw with Swing.

  8. #8
    braskez is offline Member
    Join Date
    Oct 2012
    Posts
    14
    Rep Power
    0

    Default Re: AttributedString?

    I tried the JLabel but instead of printing the string value I assigned earlier it printed something weird.

    Java Code:
    commandBookWindow = new JFrame();
    JPanel topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );
    commandBookWindow.getContentPane().add( topPanel );
    commandBookWindow.setSize(300,300);
    JLabel commandBookText = new JLabel(); 
    String[] cmds = {"exit", "keystrokes", "screenshots", "auditinfo [caller name]", "callerprofile [caller name]", "about", "commandbook",
    	"editcaller [caller name]", "newcaller", "options", "devOptions"};
    String[] cmdExp = {" Closes the program", "prints all the keystrokes for that specific caller.", "prints screenshots taken of that specific caller's screen.",
    	"prints the number of audits that specific caller has booked.", "prints general information about the caller.",
    	"opens the about window.", "opens the commandbook.", "opens a window that allows you to edit the caller's info.",
    	"creates a new caller and lets you add info about he/she.", "Opens the options menu.", "Opens the developer menu."};
    for(int i = 0;i<=10;i++){
    	    	cmdattribute[i] = new AttributedString(cmds[i].toString());
    	    	cmdEXPattribute[i] = new AttributedString(cmdExp[i].toString());
    	    }
    String fontType=this.getFont().getFontName();
    int fontSize=this.getFont().getSize();
    Font bold = new Font(fontType,Font.BOLD,fontSize);
    Font italics = new Font(fontType,Font.ITALIC,fontSize);
    for(int i = 0;i<=10;i++){
    	cmdattribute[i].addAttribute(TextAttribute.FONT, bold);
    	cmdEXPattribute[i].addAttribute(TextAttribute.FONT, italics);
    }
    commandBookText.setText(
    	cmdattribute[0]+" - "+cmdEXPattribute[0]+"\n\n"+
    	cmdattribute[1]+" - "+cmdEXPattribute[1]+"\n\n"+
    	cmdattribute[2]+" - "+cmdEXPattribute[2]+"\n\n"+
    	cmdattribute[3]+" - "+cmdEXPattribute[3]+"\n\n"+
    	cmdattribute[4]+" - "+cmdEXPattribute[4]+"\n\n"+
    	cmdattribute[5]+" - "+cmdEXPattribute[5]+"\n\n"+
    	cmdattribute[6]+" - "+cmdEXPattribute[6]+"\n\n"+
    	cmdattribute[7]+" - "+cmdEXPattribute[7]+"\n\n"+
    	cmdattribute[8]+" - "+cmdEXPattribute[8]+"\n\n"+
    	cmdattribute[9]+" - "+cmdEXPattribute[9]+"\n\n"+
    	cmdattribute[10]+" - "+cmdEXPattribute[10]
    );
    cbwScrollBar = new JScrollPane();
    cbwScrollBar.setVisible(true);
    commandBookWindow.add(cbwScrollBar);
    cbwScrollBar.getViewport().add(commandBookText);
    commandBookText.setVisible(true);
    I apologize for it being length. Anyways here is the string it wrote back to me:

    java.text.AttributedString@6ff3cb0e - java.text.AttributedString@4d6c3541
    java.text.AttributedString@3863aace - java.text.AttributedString@1762d74e
    java.text.AttributedString@5a254de0 - java.text.AttributedString@59ebd60d
    java.text.AttributedString@1e71b324 - java.text.AttributedString@26e7ee9f
    java.text.AttributedString@f7fb9e0 - java.text.AttributedString@a842913
    java.text.AttributedString@6aa4a3c9 - java.text.AttributedString@5efd17d1
    java.text.AttributedString@3bb508b6 - java.text.AttributedString@6a4f0359
    java.text.AttributedString@6964fe95 - java.text.AttributedString@65861e41
    java.text.AttributedString@482c7179 - java.text.AttributedString@5dcde3f3
    java.text.AttributedString@783096a1 - java.text.AttributedString@3df161ac
    java.text.AttributedString@324382f9 - java.text.AttributedString@6e4bc336
    Except don't include the line breaks above. It printed it all on 1 line.
    Last edited by braskez; 10-09-2012 at 02:04 AM.

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

    Default Re: AttributedString?

    There's nothing strange about this result as you're seeing the default String returned by the AttributedString class's toString() method. It defaults to the method used by Object which shows the class name, @, and the hashCode for the object. Why instead of printing out the object's toString(), why not get its text. So if the class has a getText() method, then call it. Also use a for loop here to reduce your code by > 50%

  10. #10
    braskez is offline Member
    Join Date
    Oct 2012
    Posts
    14
    Rep Power
    0

    Default Re: AttributedString?

    From what I see, unless I am doing something incorrectly, the class doesn't have a getText() method. Any other alternatives?

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

    Default Re: AttributedString?

    Quote Originally Posted by braskez View Post
    From what I see, unless I am doing something incorrectly, the class doesn't have a getText() method. Any other alternatives?
    Surely the class has an equivalent. What method do you have that returns the String that was passed into the constructor?

  12. #12
    braskez is offline Member
    Join Date
    Oct 2012
    Posts
    14
    Rep Power
    0

    Default Re: AttributedString?

    Would getConstructor(String.class); work? I'm still pretty new to this whole thing.

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

    Default Re: AttributedString?

    Quote Originally Posted by braskez View Post
    Would getConstructor(String.class); work? I'm still pretty new to this whole thing.
    No, that's only for building the object. Surely the class has a method for extracting that String that passing into your constructor, right? If not, please give it a "getter" method for this purpose and use it in your code above.

  14. #14
    braskez is offline Member
    Join Date
    Oct 2012
    Posts
    14
    Rep Power
    0

    Default Re: AttributedString?

    Quote Originally Posted by Fubarable View Post
    If not, please give it a "getter" method for this purpose and use it in your code above.
    So, how would I do that?

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

  16. #16
    braskez is offline Member
    Join Date
    Oct 2012
    Posts
    14
    Rep Power
    0

    Default Re: AttributedString?

    Quote Originally Posted by braskez View Post
    So, how would I do that?
    Well, I'm not exactly sure what you mean. I just started learning java a couple days ago so I don't know all the lingo. I do have a lot of experience with javascript, html, css, php, all that stuff so I do have some general knowledge of programming. So I suppose I will just give you my entire script. Just warning you it isn't very well organized and I probably did things the long and tedious way. But I'll get better I just got to get the hang of it. Anyways here is the code:

    Java Code:
    package callcenter;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.font.TextAttribute;
    import java.text.AttributedString;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.plaf.basic.BasicBorders;
    
    
    
    public class Callcenter extends JFrame implements ActionListener{
    
    	public static void main(String[] args){
    		new Callcenter();
    	}
    	public JTextArea commandBar;
    	public JButton commandEnter;
    	public JMenuItem exit;
    	public JMenuItem copy;
    	public JMenuItem cut;
    	public JMenuItem paste;
    	public JMenuItem commandBook;
    	public JMenuItem about;
    	public JScrollPane cbwScrollBar;
    	public AttributedString[] cmdattribute = new AttributedString[11];
    	public AttributedString[] cmdEXPattribute = new AttributedString[11];
    	public JFrame commandBookWindow;
    
    	
    
    	
    	private Callcenter(){
    		super("Workforce Monitoring Center");
    		setSize(800,800);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setResizable(false);
    		setVisible(true);
    		Container cp = getContentPane();
    		cp.setLayout(new FlowLayout());
    		cp.setVisible(true);
    		
    		//Creating JMenu
    		JMenuBar bar = new JMenuBar();
    		JMenu file = new JMenu("File");
    		bar.add(file);
    		JMenu help = new JMenu("Help");
    		file.add(help);
    		commandBook = new JMenuItem("Command Book");
    		commandBook.addActionListener(this);
    		help.add(commandBook);
    		about = new JMenuItem("About");
    		about.addActionListener(this);
    		help.add(about);
    		file.addSeparator();
    		exit = new JMenuItem("Exit");
    		exit.addActionListener(this);
    		file.add(exit);
    		JMenu edit = new JMenu("Edit");
    		bar.add(edit);
    		copy = new JMenuItem("Copy");
    		copy.addActionListener(this);
    		edit.add(copy);
    		cut = new JMenuItem("Cut");
    		cut.addActionListener(this);
    		edit.add(cut);
    		paste = new JMenuItem("Paste");
    		paste.addActionListener(this);
    		edit.add(paste);
    		bar.setVisible(true);
    		setJMenuBar(bar);
    		commandBookWindow = new JFrame();
    		JPanel topPanel = new JPanel();
    		topPanel.setLayout( new BorderLayout() );
    		commandBookWindow.getContentPane().add( topPanel );
    		commandBookWindow.setSize(300,300);
    		JLabel commandBookText = new JLabel(); 
    		String[] cmds = {"exit", "keystrokes", "screenshots", "auditinfo [caller name]", "callerprofile [caller name]", "about", "commandbook", "editcaller [caller name]", "newcaller", "options", "devOptions"};
    		String[] cmdExp = {" Closes the program", "prints all the keystrokes for that specific caller.", "prints screenshots taken of that specific caller's screen.", "prints the number of audits that specific caller has booked.", "prints general information about the caller.", "opens the about window.", "opens the commandbook.", "opens a window that allows you to edit the caller's info.", "creates a new caller and lets you add info about he/she.", "Opens the options menu.", "Opens the developer menu."};
    
    		for(int i = 0;i<=10;i++){
    	    	cmdattribute[i] = new AttributedString(cmds[i]);
    	    	cmdEXPattribute[i] = new AttributedString(cmdExp[i]);
    	    }
    	    String fontType=this.getFont().getFontName();
    		int fontSize=this.getFont().getSize();
    		Font bold = new Font(fontType,Font.BOLD,fontSize);
    		Font italics = new Font(fontType,Font.ITALIC,fontSize);
    		for(int i = 0;i<=10;i++){
    			cmdattribute[i].addAttribute(TextAttribute.FONT, bold);
    			cmdEXPattribute[i].addAttribute(TextAttribute.FONT, italics);
    		}
    		commandBookText.setText(
    					cmdattribute[0]+" - "+cmdEXPattribute[0]+"\n\n"+
    					cmdattribute[1]+" - "+cmdEXPattribute[1]+"\n\n"+
    					cmdattribute[2]+" - "+cmdEXPattribute[2]+"\n\n"+
    					cmdattribute[3]+" - "+cmdEXPattribute[3]+"\n\n"+
    					cmdattribute[4]+" - "+cmdEXPattribute[4]+"\n\n"+
    					cmdattribute[5]+" - "+cmdEXPattribute[5]+"\n\n"+
    					cmdattribute[6]+" - "+cmdEXPattribute[6]+"\n\n"+
    					cmdattribute[7]+" - "+cmdEXPattribute[7]+"\n\n"+
    					cmdattribute[8]+" - "+cmdEXPattribute[8]+"\n\n"+
    					cmdattribute[9]+" - "+cmdEXPattribute[9]+"\n\n"+
    					cmdattribute[10]+" - "+cmdEXPattribute[10]
    			);
    
    		cbwScrollBar = new JScrollPane();
    		cbwScrollBar.setVisible(true);
    		commandBookWindow.add(cbwScrollBar);
    		cbwScrollBar.getViewport().add(commandBookText);
    		commandBookText.setVisible(true);
    
    
    		
    		commandBar = new JTextArea();
    		commandBar.setBounds(20,20,200,50);
    		commandBar.setBorder( BasicBorders.getTextFieldBorder() );
    		cp.add(commandBar);
    		
    		
    		commandEnter = new JButton("Submit");
    		commandEnter.addActionListener(this);
    		cp.add(commandEnter);
    
    	}
    
        public void actionPerformed(ActionEvent e) {
        	System.out.println("actionPerformed: " + e.getSource().toString());
            if(e.getSource() == commandEnter){
            	String command = commandBar.getText();
            	System.out.println("Command Value: \""+command+"\"\n");
            		if(command.equals("exit")){
            			dispose();
            			System.exit(0);
            		}else{
            			System.out.println("Command not recognized");
            		}
            }else if(e.getSource() == exit){
    			dispose();
    			System.exit(0);
            }else if(e.getSource() == commandBook){
        		commandBookWindow.setVisible(true);
            }
        }
    
    
    }
    Last edited by braskez; 10-10-2012 at 10:24 PM.

Posting Permissions

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