Results 1 to 16 of 16
Thread: AttributedString?
- 10-08-2012, 07:21 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 14
- Rep Power
- 0
AttributedString?
I have no idea what I am doing wrong here. Also I declared cmdattribute and cmdEXPattribute earlier.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]); }
Here is the error I get:Java Code:public AttributedString[] cmdattribute = null; public AttributedString[] cmdEXPattribute = null;
Any advice is appreciated, thanks
- 10-08-2012, 07:26 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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)
- 10-08-2012, 08:03 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 14
- Rep Power
- 0
Re: AttributedString?
I tried doing the following:
But it just told me that AttributedString couldn't be resolved to a variable.Java Code:public AttributedString[] cmdattribute = AttributedString[11]; public AttributedString[] cmdEXPattribute = AttributedString[11];
- 10-08-2012, 08:12 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: AttributedString?
did you read the link? It appears not!
new AttributedString[11] !
- 10-08-2012, 08:43 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 14
- Rep Power
- 0
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 triedbut that didn't work at all. Any ideas?Java Code:commandBookWindow.add(g.drawString(cmdattribute[0].getIterator(), 30, 30)); commandBookWindow.add(g.drawString(cmdEXPattribute[0].getIterator(),30,30));
- 10-09-2012, 12:18 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 14
- Rep Power
- 0
Re: AttributedString?
Anyone?
-
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.
- 10-09-2012, 01:57 AM #8
Member
- Join Date
- Oct 2012
- Posts
- 14
- Rep Power
- 0
Re: AttributedString?
I tried the JLabel but instead of printing the string value I assigned earlier it printed something weird.
I apologize for it being length. Anyways here is the string it wrote back to me: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);
Except don't include the line breaks above. It printed it all on 1 line.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@6e4bc336Last edited by braskez; 10-09-2012 at 02:04 AM.
-
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-09-2012, 03:48 AM #10
Member
- Join Date
- Oct 2012
- Posts
- 14
- Rep Power
- 0
Re: AttributedString?
From what I see, unless I am doing something incorrectly, the class doesn't have a getText() method. Any other alternatives?
-
Re: AttributedString?
- 10-09-2012, 04:31 AM #12
Member
- Join Date
- Oct 2012
- Posts
- 14
- Rep Power
- 0
Re: AttributedString?
Would getConstructor(String.class); work? I'm still pretty new to this whole thing.
-
Re: AttributedString?
- 10-09-2012, 09:51 PM #14
Member
- Join Date
- Oct 2012
- Posts
- 14
- Rep Power
- 0
-
Re: AttributedString?
First please show us your AttributedString class code.
- 10-10-2012, 10:20 PM #16
Member
- Join Date
- Oct 2012
- Posts
- 14
- Rep Power
- 0
Re: AttributedString?
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.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks