1

I designed JFrame class and got problem which is it the buttons does not appear under the table.

this my codes

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class GalaxyTable2 extends JFrame{ //JApplet

    private static final int PREF_W = 700;
    private static final int PREF_H = 600;
    String[] columnNames
                    = {"Phone Name", "Brief Description", "Picture", "price",
                        "Buy"};

// Create image icons
    ImageIcon Image1 = new ImageIcon(
                    getClass().getResource("s1.png"));
    ImageIcon Image2 = new ImageIcon(
                    getClass().getResource("s2.png"));
    ImageIcon Image3 = new ImageIcon(
                    getClass().getResource("s3.png"));
    ImageIcon Image4 = new ImageIcon(
                    getClass().getResource("s4.png"));
    ImageIcon Image5 = new ImageIcon(
                    getClass().getResource("note.png"));
    ImageIcon Image6 = new ImageIcon(
                    getClass().getResource("note2.png"));
    ImageIcon Image7 = new ImageIcon(
                    getClass().getResource("note3.png"));

    Object[][] rowData = {
        {"Galaxy S", "3G Support,CPU 1GHz",
            Image1, 120, false},
        {"Galaxy S II", "3G Support,CPU 1.2GHz",
            Image2, 170, false},
        {"Galaxy S III", "3G Support,CPU 1.4GHz",
            Image3, 205, false},
        {"Galaxy S4", "4G Support,CPU 1.6GHz",
            Image4, 230, false},
        {"Galaxy Note", "4G Support,CPU 1.4GHz",
            Image5, 190, false},
        {"Galaxy Note2 II", "4G Support,CPU 1.6GHz",
            Image6, 190, false},
        {"Galaxy Note 3", "4G Support,CPU 2.3GHz",
            Image7, 260, false},};

    MyTable ss = new MyTable(
                    rowData, columnNames);

    // Create a table
    JTable jTable1 = new JTable(ss);

    public GalaxyTable2() {
    jTable1.setRowHeight(70);

    add(new JScrollPane(jTable1),
   BorderLayout.CENTER);
   //JFrame f = new JFrame();

   this.setTitle("Galaxy Phones");
   //JButton button = new JButton("Home");
  //button.setSize(new Dimension(200, 500));
  //button.setLocation(400, 500);
  //f.getContentPane().add(button);
  //JButton button2 = new JButton("Confirm");
  //button2.setSize(new Dimension(100, 500));
  //button2.setLocation(100, 300);
  // button2.addActionListener(null);
  //f.getContentPane().add(button2);
  //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   this.setSize(800, 700);
  this.setLocation(300, 0);
  }
    @Override

    public Dimension getPreferredSize() {
        return new Dimension(PREF_W, PREF_H);
    }

    public void actionPerformed(ActionEvent e) {
        new AMainFrame7().setVisible(true);
    }

    public static void main(String[] args) {

         GalaxyTable2 b=new GalaxyTable2();



        b.pack();

        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        b.setLocation((d.width - b.getSize().width) / 2,
                        (d.height - b.getSize().height) / 2);
        b.setVisible(true);

    }
}

and this my table class

import javax.swing.*;
import javax.swing.table.*;
import java.util.*;

public class MyTable extends DefaultTableModel {
public MyTable() {
  }

  /** Construct a table model with specified data and columnNames */
 public MyTable(Object[][] data, Object[] columnNames) {
 super(data, columnNames);
  } 

 /** Override this method to return a class for the column */
 public Class getColumnClass(int column) {
 return getValueAt(0, column).getClass();
    }

 /** Override this method to return true if cell is editable */
 public boolean isCellEditable(int row, int column) {
 Class columnClass = getColumnClass(column);
 return columnClass != ImageIcon.class &&
  columnClass != Date.class;
     }
     }

better to show me the solution in codes.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 3
    That code has 3 compilation errors. 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Jan 01 '14 at 11:53
  • Also, now I've got it to compile, absolutely nothing appears.. Post an SSCCE. Since the images are only used for the table, replace (for E.G.) `Image1` with the `String` - `"Image1"`.. – Andrew Thompson Jan 01 '14 at 11:57
  • see The updated code.now only the table is appear.How to add buttons under this table?? – loving java Jan 01 '14 at 12:01
  • That still shows 3 ***compilation errors*** here, and you have done nothing to fix the problem with 'missing images'. Sometimes I feel like I'm talking to myself.. :-/ – Andrew Thompson Jan 01 '14 at 12:03

2 Answers2

1

It will be better if you add all buttons in separate panel.

    JPanel buttonPane = new JPanel();

    JButton button = new JButton("Home");
    JButton button2 = new JButton("Confirm");

    buttonPane.add(Box.createRigidArea(new Dimension(250,0)));

    buttonPane.add(button);
    buttonPane.add(button2);

    getContentPane().add(buttonPane, BorderLayout.SOUTH);

and then place buttonpane at some place, that will be better idea.

user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
0

"loving java", You haven't add the button to the right place.

I suggest you to put the two button in a panel,and add the panel to the Frame's ContentPane.

wangdq
  • 1,874
  • 17
  • 26