简体   繁体   中英

How do I get an instance of a class that extends a JPanel to display in another classes JPanel?

incomePanel displays inside of IncomeAndSpendingPanel, but if I move all the incomePanel code into it's own class (like I have with SpendingPanel and TransactionAdder) and try to add them to IncomeAndSpendingPanel that way nothing displays. I'm just trying to clean up my code and separate it into different parts.

public class IncomeAndSpendingPanel extends JPanel {

    private ColorX cx = new ColorX();
    DefaultTableModel incomeTableModel;
    DefaultTableModel spendingTableModel;


    private String[] incomeColumnHeadings = {"Date","Description","Amount"};
    private String[] spendingColumnHeadings = {"Date","Description","Amount","Category"};

    Border empty = BorderFactory.createEmptyBorder(10, 10, 10, 10);
    Border blackLine = BorderFactory.createLineBorder(Color.black);
    CompoundBorder line = new CompoundBorder(empty, blackLine);

    public IncomeAndSpendingPanel() {

        setLayout(new BorderLayout());
        add(new TransactionAdder(), BorderLayout.NORTH);
        add(new SpendingPanel(), BorderLayout.EAST);


        JPanel incomePanel;
        Border incomePanelBorder = BorderFactory.createTitledBorder(line, "INCOME");
        JTable incomeTransactionInputTable;


        incomePanel = new JPanel(new GridLayout());

        incomePanel.setBorder(incomePanelBorder);

        incomeTableModel = new DefaultTableModel(50, incomeColumnHeadings.length);
        incomeTableModel.setColumnIdentifiers(incomeColumnHeadings);

        incomeTransactionInputTable = new JTable(incomeTableModel);
        incomeTransactionInputTable.getColumnModel().getColumn(0).setPreferredWidth(1);
        incomeTransactionInputTable.getColumnModel().getColumn(1).setPreferredWidth(200);
        incomeTransactionInputTable.getColumnModel().getColumn(2).setPreferredWidth(10);

        incomePanel.add(new JScrollPane(incomeTransactionInputTable));

        add(incomePanel, BorderLayout.CENTER);
    }

This is the class I built for TransactionAdder, and nothing displays when I try to add it to the IncomeAndSpendingPanel, I've tried creating a TransactionAdder transactionAdder = new TransactionAdder(); and then adding the instance of TransactionAdder like this

add(transactionAdder, BorderLayout.NORTH);

But nothing displays on the IncomeAndSpendingPanel that way either

public class TransactionAdder extends JPanel {

    DefaultTableModel model3;
    DefaultTableModel model4;

    private String[] incomeColumnHeadings = {"Date","Description","Amount"};
    private String[] spendingColumnHeadings = {"Date","Description","Amount","Category"};

    Border empty = BorderFactory.createEmptyBorder(10, 10, 10, 10);
    Border blackLine = BorderFactory.createLineBorder(Color.black);
    CompoundBorder line = new CompoundBorder(empty, blackLine);

    JTable incomeTransactionTable;
    JTable spendingTransactionTable;
    JPanel transactionAdderPanel;
    Border transactionAdderPanelBorder = BorderFactory.createTitledBorder(line, "ADD TRANSACTION");
    JScrollPane jScrollPane;
    JScrollPane jScrollPane1;

    public TransactionAdder() {

        transactionAdderPanel = new JPanel(new GridLayout());

        transactionAdderPanel.setBorder(transactionAdderPanelBorder);

        model3 = new DefaultTableModel(1, incomeColumnHeadings.length);
        model3.setColumnIdentifiers(incomeColumnHeadings);
        model4 = new DefaultTableModel(1, spendingColumnHeadings.length);
        model4.setColumnIdentifiers(spendingColumnHeadings);

        transactionAdderPanel.add(new JButton("Add New Income"));

        incomeTransactionTable = new JTable(model3);
        jScrollPane = new JScrollPane(incomeTransactionTable);
        jScrollPane.setPreferredSize(new Dimension(300, 38));
        transactionAdderPanel.add(jScrollPane);

        transactionAdderPanel.add(new JButton("Add New Spending"));

        spendingTransactionTable = new JTable(model4);
        jScrollPane1 = new JScrollPane(spendingTransactionTable);
        jScrollPane1.setPreferredSize(new Dimension(300, 38));
        transactionAdderPanel.add(jScrollPane1);



    }
}

Lots to say here...firstly you probably need to be using a JFrame so please read about top level containers here .

Also since you are using inheritance you definitely need to move all those base class method calls to a method and only use the constructor to initialize instance variables. If you don't you could have all kinds of problems for when any JPanel object is created because you have override-able method calls during initialization.

However, when you add incomePanel and TransactionAdder

  1. You never set either of these .setVisible(true)
  2. You never set your IncomeAndSpendingPanel as .setVisible(true)
  3. You never set IncomeAndSpendingPanel to any size (use the Dimension class)
  4. You never used the container hierarchy

Please go make those contained within a JFrame and change your code to look something more like this:

class IncomeAndSpendingPanel extends JFrame
{
   //Instance variables

   public IncomeAndSpendingPanel()
   {
     //initialize instance variables
     createAndShowGui();
   }

   private void createAndShowGui()
   {
     //Your code
     this.setVisible(true);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM