.drawLine is not drawing onto JTabbedPane (Java)
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)?
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();
Sorry for the blurge of bloated code
Yes, well that is why people can't solve problems, because the code is so bloated you can't see what you are doing.
If you want us to help you problem solve then you need to post a SSCCE
Someone told me a way to paint onto a Jframe
Well, that is wrong, in general you should not be overrding the paint() method, unless you have a specific reason.
Also, your whole program is wrong because you are extending JTabbedPane. You should never do this to create a GUI.
Your paint() method is never invoked because you never use that class anywhere. Look at your code. For your class variables you create a new JTabbedPane. Then in the constructor you add all these components to the frame and make the frame visible.
You need to take a look at the Swing tutorial and follow some of the example there for a better way to create a simple GUI.
I don't understand what you are trying to do by drawing a line on the tabbed pane. A tabbed pane displays different panels every time you click on a tab. Where exactly do you want the line to appear?
Also, you should learn how to use layout managers. Using a null layout will cause unnecessary problems.
Until you post a SSCCE, I can't be of much help.