Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:14:23+00:00 2026-05-25T15:14:23+00:00

I’m trying to build a simple GUI using the Swing library. I don’t understand

  • 0

I’m trying to build a simple GUI using the Swing library. I don’t understand why my table is erasing everything that previously was added to the GUI and only then creates the table. I’m assuming it’s a certain command in addMainPanel, but I’m not sure which. Your advice would be much appreciated.

package fuelConsumption;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class LogView implements ActionListener {

    private Log myLog;
    private JFrame frame;

    public LogView (String frameName) {
        this.frame = new JFrame(frameName);
        this.frame.setPreferredSize(new Dimension(500,500));
        this.frame.getContentPane().setLayout(new BorderLayout());

        this.addMainPanel(frame);
        this.addTable(frame);
        //addMenu(frame);
        //addToolBar(frame);

        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.frame.pack();
        this.frame.setVisible(true);
    }

    private void addTable(JFrame frame2) {
        String[] columnNames = {"date",
                               "station",
                               "fuel grade",
                               "fuel amount",
                               "fuel unit cost",
                               "fuel cost",
                               "trip distance"};
        Object[][] data = {
            {"Shell", 89, 40, 109.5, "bla", 100, 123}
        };

        JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        //table.setFillsViewportHeight(true);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        //this.frame.setContentPane(scrollPane);
        frame2.getContentPane().add(scrollPane);
    }

    private void addMainPanel(JFrame frame2) {
        // TODO Auto-generated method stub

        JPanel panel = new JPanel(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 5;
        c.ipady = 50;
        c.anchor = GridBagConstraints.LINE_START;
        c.weightx = 0.5;
        c.weighty = 0.5;
        JLabel label = new JLabel("");
        panel.add(label,c);

        label = new JLabel("Info");
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.anchor = GridBagConstraints.LINE_START;
        c.weightx = 0.5;
        c.weighty = 0.5;
        panel.add(label,c);

        label = new JLabel("Label");
        c = new GridBagConstraints();
        c.gridx = 2;
        c.gridy = 1;
        c.anchor = GridBagConstraints.LINE_START;
        c.weightx = 0.5;
        c.weighty = 0.5;
        panel.add(label,c);

        label = new JLabel("Comments");
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 2;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.weightx = 0.5;
        c.weighty = 0.5;
        panel.add(label,c);

        JTextArea textArea = new JTextArea(4,30);
        JScrollPane textScroll = new JScrollPane(textArea);
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 4;
        c.ipadx = 30;
        c.ipady = 50;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.weighty = 0.5;
        panel.add(textScroll,c);

        JButton button = new JButton("Edit");
        button.addActionListener(this);
        button.setActionCommand("Edit");
        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 3;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.weighty = 0.5;
        panel.add(button,c);

        button = new JButton("Previous");
        button.addActionListener(this);
        button.setActionCommand("Previous");
        c = new GridBagConstraints();
        c.gridx = 2;
        c.gridy = 3;
        c.weightx = 0.5;
        c.weighty = 0.5;
        panel.add(button,c);

        button = new JButton("Next");
        button.addActionListener(this);
        button.setActionCommand("Next");
        c = new GridBagConstraints();
        c.gridx = 3;
        c.gridy = 3;
        //        c.weightx = 0.5;
        //        c.weighty = 0.5;
        panel.add(button,c);

        frame2.getContentPane().add(panel);
    }

    public static void main(String [] args){
        new LogView("Fuel Consumption");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:14:24+00:00Added an answer on May 25, 2026 at 3:14 pm

    Your GUI could be:

    enter image description here

    If you change

    private void addTable(JFrame frame2) { to private void addTable() {

    and

    frame2.getContentPane().add(scrollPane); to frame.add(scrollPane, BorderLayout.CENTER);

    to:

    private void addMainPanel(JFrame frame2) { to private void addMainPanel() {

    and

    frame2.getContentPane().add(panel); to frame.add(panel, BorderLayout.SOUTH);

    Because in BorderLayout only one JComponent can be placed in one of the areas, as there aren’t definitions for BorderLayout constants, then JComponent would be placed in the BorderLayout.CENTER area.

    3)

    Then you have to change

    this.addMainPanel(frame);
    this.addTable(frame);
    

    to

    this.addMainPanel();
    this.addTable();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.