Results 1 to 2 of 2
- 03-16-2011, 11:22 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 23
- Rep Power
- 0
Adding custom Jtable to GUI in nested class
i'm trying to add a custome JTable to my GUI. i'm following the steps on the oracle tutorial on 'how to use tables'. this is pushing my knowledge of java. the GUI is part of a system for scheduling staff. the table will (eventually) display booked shifts that a user has selected from another screen.
so far this is what i've done:
created an outer class ViewShiftsGUITable.
created a static innner class createTable to build the table, following the guides on oracle.
created a class buildTheGUI to create the GUI and attach the table
created a main method to see if it works, which it doesnt. when i run it it says errors exist, but none are appearing in the console. i'm think it might be the way the classes are formatted but not too sure.
all help welcome.
here's the code:
Java Code:package employeeSide; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; public class ViewShiftsTableGUI { public static class createTable extends AbstractTableModel { private static String[] columnNames = {"Day","Date","Morning","Afternoon","Evening"}; private static Object[][] data; private static JTable table = new JTable(data, columnNames); public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { return data[row][col]; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int row, int col) { return false; } public void setValueAt(Object value, int row, int col) { data[row][col] = value; fireTableCellUpdated(row, col); } } class BuildTheGUI extends JFrame //implements ActionListener { private static final int FRAME_WIDTH = 750; private static final int FRAME_HEIGHT = 500; private static final int BUTTON_WIDTH = 100; private static final int BUTTON_HEIGHT = 40; private JButton cancel; private JLabel title; private JPanel top; private JPanel middle; private JPanel bottom; public BuildTheGUI() { setSize(FRAME_WIDTH, FRAME_HEIGHT); setTitle("Online Roster System"); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); setVisible(true); Container cPane = this.getContentPane(); setLayout(new GridLayout(3,1)); title = new JLabel("VIEW SHIFTS"); title.setFont(new Font("Arial", Font.BOLD, 20)); title.setForeground(Color.GRAY); title.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0)); top = new JPanel(); top.setBorder(BorderFactory.createLineBorder(Color.BLACK)); top.setSize(745, 60); top.add(title); middle = new JPanel(); middle.setBorder(BorderFactory.createLineBorder(Color.BLACK)); middle.add(createTable.table.getTableHeader(), BorderLayout.PAGE_START); cPane.add(top); cPane.add(middle); } } public static void main(String[]args) { ViewShiftsTableGUI test = new ViewShiftsTableGUI(); //test.setVisible(true); } }
- 03-16-2011, 12:55 PM #2
Member
- Join Date
- Sep 2010
- Posts
- 23
- Rep Power
- 0
ok, i think the problem is with the way i'm trying to create an instance of the GUI.
I have:
So I've tried to create an instance like so:Java Code:class OuterClass which is ViewShiftsTableGUI { ... static class StaticNestedClass which is createTable { ... } class InnerClass which is BuildTheGUI { ... } }
but, i'm being prompted to create a new class BuidTheGUI....?Java Code:public static void main(String[]args) { ViewShiftsTableGUI.createTable theTable = new ViewShiftsTableGUI.createTable(); ViewShiftsTableGUI.BuildTheGUI theGUI = theTable.new BuildTheGUI(); }
anyone any ideas with what's going wrong?
Similar Threads
-
Help Creating Custom File and Adding an Editor
By Fondor in forum Advanced JavaReplies: 6Last Post: 04-23-2010, 04:06 AM -
Adding custom buttons - JOptionPane.showOptionDialog(...)
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 09:46 AM -
Adding custom highlight to JEditorPane
By andrewb in forum AWT / SwingReplies: 0Last Post: 06-22-2007, 06:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks