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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:17:50+00:00 2026-05-18T09:17:50+00:00

Someone told me a way to paint onto a Jframe and it worked fine

  • 0

Someone told me a way to paint onto a Jframe and it worked fine in the example, but now I have tabs it doesn’t paint onto them.

I need the paint/drawLine to work in my Tabbed example:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener;
import java.io.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;


public class GUI extends JTabbedPane implements ActionListener
{
    static JFrame aWindow = new JFrame("Project");

    JTabbedPane myTabs = new JTabbedPane();
    
    JPanel  loginMainPanel = new JPanel();
    JPanel  displayMainPanel = new JPanel();
    JPanel  editMainPanel = new JPanel();

    JTextField myText1 = new JTextField("");
    JTextField myText2 = new JTextField("");
    JTextField myText3 = new JTextField("");
    
    JLabel loginLabel = new JLabel("Username:");

    JTextField loginField = new JTextField();
    JLabel loginLabel2 = new JLabel("Password:");
    JPasswordField loginPass = new JPasswordField();

    JButton displayButton = new JButton("Load Data");
    JButton loginButton = new JButton("Login");

    JLabel editLabel = new JLabel("Write:");
    JTextArea editArea = new JTextArea();

    public GUI()
    {
        Toolkit theKit = aWindow.getToolkit();
        Dimension wndSize = theKit.getScreenSize();  
        
        aWindow.setBounds(wndSize.width/3, wndSize.height/3, 250, 250); 
        aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        GridLayout grid = new GridLayout(1,1);
        Container content = aWindow.getContentPane();
        content.setLayout(grid);
        
        createLoginPanel();
        createDisplayPanel();
        createEditPanel();

        myTabs.addTab("Login", loginMainPanel);
        myTabs.addTab("Main Menu", displayMainPanel);
        myTabs.addTab("Setting", editMainPanel);

        myTabs.setSelectedIndex(0);
        myTabs.setEnabledAt(1,false);
        myTabs.setEnabledAt(2,false);
        
        content.add(myTabs);
        aWindow.setVisible(true);
    }
    
    public void createLoginPanel()
    {
        loginMainPanel.setLayout(null);
        loginLabel.setBounds(10, 15, 150, 20);
        loginMainPanel.add(loginLabel);
        loginField.setBounds(10, 35, 150, 20);
        loginMainPanel.add(loginField);
        loginLabel2.setBounds(10, 60, 150, 20);
        loginMainPanel.add(loginLabel2);
        loginPass.setBounds(10, 80, 150, 20);
        loginMainPanel.add(loginPass);
        loginButton.addActionListener(this);
        loginButton.setBounds(50, 110, 80, 20);
        loginMainPanel.add(loginButton);
    }
    
    public void createDisplayPanel()
    {
        displayMainPanel.setLayout(null);
        displayButton.addActionListener(this);
        displayButton.setBounds(50, 80, 150, 20);
        displayMainPanel.add(displayButton);
        myText1.setBounds(50, 170, 200, 30);
        myText2.setBounds(50, 140, 200, 30);
        myText3.setBounds(50, 110, 200, 30);
        displayMainPanel.add(myText1);
        displayMainPanel.add(myText2);
        displayMainPanel.add(myText3);
    }
    
    public void createEditPanel()
    {
        editMainPanel.setLayout(null);      
        editLabel.setBounds(10, 15, 150, 20);
        editMainPanel.add(editLabel);
        editArea.setBounds(10, 65, 150, 50);
        editMainPanel.add(editArea);        
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == loginButton)
        {
            //myTabs.setSelectedIndex(1);
        }
    }
 
    public void paint(Graphics g) {
        super.paint(g);
        int locX = 0;
        int locY = 0;
    int destX = 210;
    int destY = 210;
    g.setColor(Color.red);
        // draw a line (there is now drawPoint..)
        g.drawLine(locX, locY, destX, destY); 
    }


    public static void main(String[] args)
    {
        GUI tw1 = new GUI();

    }
}

How can I find a solution so it will paint that line on the tab (loginMainPanel)?

  • 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-18T09:17:51+00:00Added an answer on May 18, 2026 at 9:17 am

    If you want custom drawing on a JPanel, you should create a custom class that extends JPanel:

    class CustomPanel extends JPanel {
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawLine(x1, y1, x2, y2);
        }
    }
    

    Then:

    JPanel  loginMainPanel = new JPanel();
    

    Woudl become:

    JPanel  loginMainPanel = new CustomPanel();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Someone told me That Serialization was not the best way to send things over
Someone had told me about overloading the operators, but I'm not even sure how
Someone told me that openGL is for graphic only, and that it's very bad
Someone told me to do this in order to keep track of latest people
Someone told me once there was a good novel around the story of the
Someone on IRC told me I could create objective c objects directly from plist
Someone has asked the exact same question in April, without any answer. But since
I see that we now have both Windows Server AppFabric and Azure AppFabric. Is
Someone told me that I need to use a delimiter in my trigger. I'm
I'm working on an older script. Someone told me that the script uses ancient

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.