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 8725475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:02:59+00:00 2026-06-13T08:02:59+00:00

I’m working on this application and I just can’t get the GUI to display

  • 0

I’m working on this application and I just can’t get the GUI to display properly. I’m using FlowLayout, and everything just looks all jumbled (any other layout looks even worse). If there were just some way to add a horizontal rule between sections, that would work, but nothing I tried works.

Here is my code:

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;


public class ConnectToDB implements ActionListener {
    public static void main(String[] args){

        //GUI STUFF
        //constants
        final int windowX = 640; //pixels
        final int windowY = 655; //pixels
        final int fieldX = 20;   //characters
        final FlowLayout LAYOUT_STYLE = new FlowLayout();

        //window
        JFrame window = new JFrame("Mike's MySQL GUI Client");

        //Connection Details
        JLabel enterInfo = new JLabel("Enter Connection Details: ");
        JLabel driver = new JLabel("Database Driver: ");
        JTextField driverText = new JTextField(fieldX);
        JLabel dburl = new JLabel("Database URL: ");
        JTextField dburlText = new JTextField(fieldX);
        JLabel dbuser = new JLabel("Username: ");
        JTextField dbuserText = new JTextField(fieldX);
        JLabel dbpass = new JLabel("Password: ");
        JTextField dbpassText = new JTextField(fieldX);

        //Enter a SQL Command
        JLabel enterSQL = new JLabel("Enter a SQL Command:");
        JTextArea enterSQLText = new JTextArea(10, 30);

        //Connection Status and Command Buttons
        JLabel connectionStatus = new JLabel ("No Connection Now");
        JButton connectButton = new JButton("Connect");
        JButton executeButton = new JButton("Execute SQL Command");
        JButton clearCommandButton = new JButton("Clear Command");

        //SQL Execution Result
        JLabel executionResult = new JLabel("SQL Execution Result:");
        JButton clearResultsButton = new JButton("Clear Results");
        JTextArea executionResultText = new JTextArea(20, 50);

        //Configure GUI
        window.setSize(windowX, windowY);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        driverText.setEditable(false);
        dburlText.setEditable(false);
        dbuserText.setEditable(false);
        dbpassText.setEditable(false);
        executionResultText.setEditable(false);

        //Register Event Listeners
        connectButton.addActionListener(null);
        executeButton.addActionListener(null);
        clearCommandButton.addActionListener(null);
        clearResultsButton.addActionListener(null);

        //Construct Container
        Container c = window.getContentPane();

        c.setLayout(LAYOUT_STYLE);

        c.add(enterInfo);
        c.add(driver);
        c.add(driverText);
        c.add(dburl);
        c.add(dburlText);
        c.add(dbuser);
        c.add(dbuserText);
        c.add(dbpass);
        c.add(dbpassText);
        c.add(connectionStatus);
        c.add(connectButton);
        c.add(enterSQL);
        c.add(enterSQLText);
        c.add(executeButton);
        c.add(clearCommandButton);
        c.add(new JSeparator(SwingConstants.VERTICAL));
        c.add(executionResult);
        c.add(clearResultsButton);
        c.add(executionResultText);

        window.setVisible(true);//END GUI STUFF

        //DB Connection details
        System.out.println("Attempting to connect to database...");
        Connection conn = null;
        String url = "jdbc:mysql://localhost/";
        String dbName = "project3";
        String DBdriver = "com.mysql.jdbc.Driver";
        String userName = "root"; 
        String password = "OMGnpw=-0";

        driverText.setText(DBdriver);
        dburlText.setText(url);
        dbuserText.setText(userName);
        dbpassText.setText(password);

        try 
        {
          //Connect to DB and notify user
          Class.forName(DBdriver).newInstance();
          conn = DriverManager.getConnection(url+dbName,userName,password);
          System.out.println("Connected to the database");

          /*>>>>>>Do everything you need to do while connected to DB<<<<<<*/

          //HOW TO EXECUTE A STATEMENT AND PRINT IT
          //Create a statement
          Statement statement = conn.createStatement();
          //Execute a statement
          ResultSet resultSet = statement.executeQuery("SELECT * FROM riders");
          //Process query results
          ResultSetMetaData metaData = resultSet.getMetaData();
          int numberOfColumns = metaData.getColumnCount();

          for(int i = 1; i<= numberOfColumns; i++){
              System.out.printf("%20s\t", metaData.getColumnName(i));
          }
          System.out.println();

          while (resultSet.next()){
              for (int i = 1; i <= numberOfColumns; i++){
                  System.out.printf("%20s\t", resultSet.getObject(i));
              }
              System.out.println();
          }
          /*>>>>>>Finish DB activities<<<<<<*/

          //Disconnect from DB
          conn.close(); 
          System.out.printf("Disconnected from database");
        } 
        catch (Exception e) {
        e.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent e){
        System.out.println("Button Works!");
    }
}

I may need to use a different layout, but FlowLayout is the only one I’m familiar with. Can anyone suggest an easy fix?

  • 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-06-13T08:03:01+00:00Added an answer on June 13, 2026 at 8:03 am

    Try a box layout. And add some panels to it. Make the panels flow layout and put a pair of label and textfield in each panel

    |-----container with box layout-----|
    panel[[flow]label texfield] 
    panel[[flow]label texfield] 
    panel[[flow]label texfield] 
    panel[[flow]label texfield] 
    panel[[flow]label texfield] 
    panel[[flow]sqltextfields] 
    panel[[flow]buttons] 
    |-----------------------------------|
    

    and near the bottom put your sql textfields and buttons

    http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

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.