Results 1 to 3 of 3
  1. #1
    AARON22 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default Button in a jpanel

    HELLO I WANT TO ADD A BUTTON UNDER THE TABLE I HAVE DONE!!!HOW CAN I DO THIS ANY HELPS PLEASE...I HAVE STUCK HERE!!
    HERE IS MY CODE, I DIPLAY A TXT FILE IN A TABLE AND I WANT TO ADD A BUTTON UNDER IT!
    Java Code:
    import javax.swing.table.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    /**
     *
     * @author AcerLaptop
     */
    public class Buy extends Items {
    
    
    
         public int countLines(String filename){
            int lines = 0; //mporei na metrhsei mexri "int" grammes (~2.1 dis grammes)
            try {
                // Open the file that is the first command line parameter
                FileInputStream fstream = new FileInputStream(filename);
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                //Read File Line By Line
                while ((strLine = br.readLine()) != null) {
                    lines++;//metrhths grammwn/eggrafwn
                }
                //close input stream
            } catch (Exception e) {//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
    
        return lines;}
    
    
    
            public String[] showAllRegisteredItems(String filename, int size) {
    
            String[] temp = new String[size+1]; //mexri "size" kataxwrhseis ma8hmatwn dld (mege8os "int")
    
            try {
                int x = 0;
                // Open the file that is the first command line parameter
                FileInputStream fstream = new FileInputStream(filename);
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                //Read File Line By Line
    
                while ((strLine = br.readLine()) != null) {
                    //System.out.println(strLine.replace("#", " "));
                    temp[x] = strLine;
                    x++;
                }
                //close input stream
            } catch (Exception e) {//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
            return temp;
        }
    
    
    
    
    
    
    public JPanel pinakas(String[] pinaka){
            int sr = 0;
            //int ari8mos  =0;
    
            String[] COLUMN_NAMES = {"Κωδικός", "Ποσότητα", "Τιμή", "Περιγραφή", "Μέγεθος", "Ράτσα"};
    //pio panw mporoume na pros8esoume ws prwto column to "#", wste na deixnei ton ari8mo ths ka8e kataxwrhshs
            DefaultTableModel modelM = new DefaultTableModel(COLUMN_NAMES, 0);
            JTable tableM = new JTable(modelM);
            JPanel mainPanel = new JPanel(new BorderLayout());
            JButton button1 = new JButton("Button Text");
            mainPanel.add(new JScrollPane(tableM),BorderLayout.CENTER);
            mainPanel.add(button1);
    
            // Install a button renderer in the last column
    
    
            Display disp = new Display();
            while (pinaka[sr] != null) // !!!!tha ektupwsei kai mia parapanw "/n" logo ths kataxwrhshs prwtou h teleytaiou mahmatos
            {
                String[] temp5 = disp.lineDelimiter(pinaka[sr],6, "#");
                Object[] doge = { temp5[0], temp5[1], temp5[2], temp5[3], temp5[4], temp5[5]};//edw mporoume sthn arxh na valoume to ari8mos gia na fainetai o ari8mos twn kataxwrhsewn
                modelM.addRow(doge);
                sr++;
                //ari8mos++;
            }
    
            return mainPanel;
        }
    
    
    
    
          public static void main(String[] args) {
    
    
     
     Display disp= new Display();
          Dogs dog=new Dogs();
          int numberofline=disp.countLines("Dogss.txt");
          String[] tempΜ1 = disp.showAllRegisteredItems("Dogss.txt",numberofline);
    
          JOptionPane.showMessageDialog(null, dog.pinakas(tempΜ1), "Καταχωρημένα Kατοικίδια", JOptionPane.PLAIN_MESSAGE);
    
       
          }
    
    }

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

    Default

    Suggestions:
    • First and foremost, read/study the layout section of the Oracle Java Swing tutorials. Your problem (I believe) is one of not understanding how to use layouts properly, in this situation the BorderLayout since you appear to be adding more than one component to the BorderLayout.CENTER position of a JPanel that uses Borderlayout, and that won't work.
    • Please avoid using all-uppercase in your posts here because that is equivalent to SHOUTING.


    Much luck!

  3. #3
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default

    I say follow Fubarable's suggestions and study the layout and swing tutorials, however if you are desperate to create a working gui (sigh) then i would recommend using a tool like googles window builder :
    WindowBuilder User Guide - Google Web Toolkit - Google Code

    and if you choose to use this method you should keep watch of the code generated by it and use it as a learning experience. However you will still need a basic grasp of layouts, since it demands you to choose a layout for each component. But at least you can play around with it and visually see the differences.

    Other links :
    Using Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    Working examples:
    Layout Example : Layout*«*SWT JFace Eclipse*«*Java
    JavaTechniques » GridBagLayout Example: A Simple Form Layout

Similar Threads

  1. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 06:52 PM
  2. Placing a new JPanel over a paint overriden JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-08-2010, 06:00 PM
  3. Replies: 4
    Last Post: 09-25-2010, 09:03 AM
  4. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 12:50 PM
  5. trying to animate jpanel with button
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-16-2009, 12:02 AM

Posting Permissions

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