From: Lew on 2 May 2010 23:51 jason wrote: > i [sic] am using the java [sic] desktop application developer in NetBeans. > > When I use the following program: > > [CODE] > /* > * To change this template, choose Tools | Templates > * and open the template in the editor. > */ > > package readtextfileexample; > > /** > * > * @author amandaabdou > */ > import java.io.BufferedReader; > import java.io.File; > import java.io.FileReader; > import java.io.FileNotFoundException; > import java.io.IOException; > > > public class Main { > > /** > * @param args the command line arguments > */ > public static void main(String[] args) { > String A; > A=FileReader("/Users/"+System.getProperty("user.name")+"/ You should follow the naming conventions. > Desktop/ad_log.txt"); > System.out.println(A); > } > > public static String FileReader(String args){ > File file = new File(args); > StringBuffer contents = new StringBuffer(); > BufferedReader reader = null; > String For_Output=""; > try > { > reader = new BufferedReader(new FileReader(file)); > String text = null; > > // repeat until all lines is read > while ((text = reader.readLine()) != null) > { > contents.append(text) > .append(System.getProperty( > "line.separator")); > For_Output+=text; > } > } catch (FileNotFoundException e) > { > e.printStackTrace(); > } catch (IOException e) > { > e.printStackTrace(); > } finally > { > try > { > if (reader != null) > { > reader.close(); > } > } catch (IOException e) > { > e.printStackTrace(); > } > } > > // show file contents here > > return For_Output; > } > > } > [/CODE] > > in a java [sic] application in netbeans [sic], it runs perfectly and does exactly > what i [sic] want. Case matters in Java. It's good practice to use correct case in writing about Java matters. > if i [sic] attempt to migrate the method of FileReader to my basic desktop > application it no longer works. > this desktop application is using the exact same library imports and > is using identical code aside from one aspect. my FileReader method is > now called: > [CODE] > private String FileReader(String args) > [/CODE] And that didn't give you a compiler error? There is a significant difference between static and instance methods. But since we don't get the whole picture, we can't tell exactly why you didn't get a compiler error when the method changed from static to instance. Either way, the method name should be spelled with an initial lower-case letter, as with variables. <http://java.sun.com/docs/codeconv/index.html> I also suspect that markspace is onto something with hi comments about the "java [sic] desktop application developer". -- Lew
From: jason on 4 May 2010 12:22 On May 2, 10:06 pm, markspace <nos...(a)nowhere.com> wrote: > jason wrote: > > if i attempt to migrate the method of FileReader to my basic desktop > > application it no longer works. > > Well, I can't tell you what the problem is with the program, if you > don't show it to me. All I can say is I agree the program you posted > works. The program you didn't post ... shrug, I don't know. > > > this desktop application > > Ugh, you meant the NetBeans project type "Java Desktop Application?" > > Don't use that, I don't think that JSR ever really went anywhere. It's > being maintained by one guy at Kenai now who seems to have no resources > or customers. Notice that their front page basically refers you to the > NetBeans Platform (different than the IDE) for an application framework. > > https://appframework.dev.java.net/ > > Use the regular old "Java Application" instead, and just make regular > old Swing objects. It works fine, everybody does it. JSR 296 is, imo, > dead. markspace, thank you very much for taking the time to respond and decipher my errors. i was using the "Java Desktop App" for ease of introducing myself to Java GUI's. it helped and now i am taking your recommended path. i am having one minor issue. i cannot seem to locate components where i would like. i have made the components yet cannot place them where i would like. if you could assist me with this one last issue i will be on my way to completing my goal. my current code is below. a snippet that more aptly explains my issue is the following: [code] JLabel JLabel1 = new JLabel("Search: "); //JLabel1.setHorizontalAlignment(SwingConstants.LEFT); //JLabel1.setVerticalAlignment(SwingConstants.TOP); JLabel1.setLocation(100, 100); [/code] as you can notice, i am trying to use JLabel1.setLocation to denote the desired location of my label. this does not seem to be working. thank you in advance for any help! [code] //file: PopUpColorMenu.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PopUpColorMenu extends JFrame implements ActionListener { JPopupMenu colorMenu; Component selectedComponent; public PopUpColorMenu( ) { super("v1.0"); setSize(500, 200); setLocation(200, 200); addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent e) { System.exit(0); } }); MouseListener mouseListener = new MouseAdapter( ) { public void mousePressed(MouseEvent e) { checkPopup(e); } public void mouseClicked(MouseEvent e) { checkPopup(e); } public void mouseReleased(MouseEvent e) { checkPopup(e); } private void checkPopup(MouseEvent e) { if (e.isPopupTrigger( )) { selectedComponent = e.getComponent( ); colorMenu.show(e.getComponent(), e.getX(), e.getY( )); } } }; final Container content = getContentPane( ); JMenuBar menuBar = new JMenuBar( ); //create menu bar JMenu File_Menu = new JMenu("File"); //create main menu item File_Menu .setMnemonic(KeyEvent.VK_F); JMenuItem Def_Save=new JMenuItem("Save As Default"); Def_Save.setMnemonic(KeyEvent.VK_S); Def_Save.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0){ System.out.println("SAVED"); } }); JMenuItem Def_Restore=new JMenuItem("Restore Default"); Def_Restore.setMnemonic(KeyEvent.VK_R); Def_Restore.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0){ System.out.println("RESTORE"); } }); JMenuItem File_Exit=new JMenuItem("Exit"); File_Exit.setMnemonic(KeyEvent.VK_X); File_Exit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0){ //System.out.println("EXITED"); System.exit(0); } }); File_Menu.add(Def_Save); //add as submenu File_Menu.add(Def_Restore); File_Menu.add(File_Exit); content.setLayout(new FlowLayout( )); //menuBar.add(File_Menu); //setJMenuBar(menuBar); JLabel JLabel1 = new JLabel("Search: "); //JLabel1.setHorizontalAlignment(SwingConstants.LEFT); //JLabel1.setVerticalAlignment(SwingConstants.TOP); JLabel1.setLocation(100, 100); content.add(JLabel1); JButton button1 = new JButton("Go"); button1.addMouseListener(mouseListener); button1.setHorizontalAlignment(SwingConstants.RIGHT); button1.setVerticalAlignment(SwingConstants.BOTTOM); content.add(button1); colorMenu = new JPopupMenu("Color"); colorMenu.add(("Red")); colorMenu.add(("Green")); colorMenu.add(("Blue")); button1.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub System.out.println("Maybe"); } }); getContentPane( ).addMouseListener(mouseListener); setVisible(true); } public void actionPerformed(ActionEvent e) { String color = e.getActionCommand( ); if (color.equals("Red")) selectedComponent.setBackground(Color.red); else if (color.equals("Green")) selectedComponent.setBackground(Color.green); else if (color.equals("Blue")) selectedComponent.setBackground(Color.blue); } private JMenuItem makeMenuItem(String label) { JMenuItem item = new JMenuItem(label); item.addActionListener( this ); return item; } public static void main(String[] args) { new PopUpColorMenu( ); } } [/code]
From: markspace on 4 May 2010 12:51 I'll decipher your code in a bit. I like how you're trying to post complete examples now. That's good! But first I'll show you the easy way to do it. jason wrote: > as you can notice, i am trying to use JLabel1.setLocation to denote > the desired location of my label. this does not seem to be working. "There's an app for that." Check out the GUI builder in NetBeans: <http://netbeans.org/kb/trails/matisse.html>
From: jason on 4 May 2010 13:19 On May 4, 12:51 pm, markspace <nos...(a)nowhere.com> wrote: > I'll decipher your code in a bit. I like how you're trying to post > complete examples now. That's good! But first I'll show you the easy > way to do it. > > jason wrote: > > as you can notice, i am trying to use JLabel1.setLocation to denote > > the desired location of my label. this does not seem to be working. > > "There's an app for that." > > Check out the GUI builder in NetBeans: > > <http://netbeans.org/kb/trails/matisse.html> haha thanks for humoring me. due to the fact that i have now committed to building the GUI the good old fashioned way i am going to try my best to steer clear of GUI's for GUI's etc. my basic question is this: how do i create a blank GUI layout, and place items as I want. ie: JButton MyButton=new JButton; MyButton.size(10,10); MyButton.setLocation(0,0); JLabel MyLabel=new JLabel; JLabel.size(2,10);
From: markspace on 4 May 2010 14:28 jason wrote: > haha > > thanks for humoring me. due to the fact that i have now committed to I'm not humoring you or answering lightly. I'm dead serious. The only way to be productive and produce robust, multi-platform GUIs is to use a builder tool. Trying to do this by hand is basically a fool's errand. You'll just end up re-inventing the wheel. Put some effort into learning how to make the builder tool do what you want, the long term rewards are worth it. > building the GUI the good old fashioned way i am going to try my best > to steer clear of GUI's for GUI's etc. Learning is OK, just don't loose sight of the overall goal: producing code efficiently. Start with the Java tutorials. You'll need to understand LayoutManagers to lay out by hand. There's lots of good examples of how to layout components manually here: <http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html> You should also look up "Separation of Concerns" before rolling too many of your own GUI components. Using a GUI builder will help keep you honest there. > MyButton.setLocation(0,0); Use "setBounds" to position a component manually. You will need to remove the layout manager first, because a layout manager will call setBounds itself to reposition its components. See this example here: <http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html>
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Incremental Java Compile Next: get name of operation system |