Show the line numbers when the JScrollBar is moved - java

In a linux text editor, Kate, there is this nice functionality that when I click and drag the scroll bar it shows the current line numbers that are currently in view in the text component.
My question is how can I add this function in Java to my scroll pane containing a JTextArea. Which component can I use to show this notification?

Apparently you can do this with a JPopupMenu:
I tried with this class because I knew it had the method show(Component, x, y). But it might be possible with other classes, or trying to implement whatever that method does yourself.
I put a couple of mouse listeners to the scrollBar and toyed a little with the values x, y in the show() call until I was satisfied with the position where it was being drawn at.
Full code:
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ScrollbarTest
{
private JScrollPane scrollPane;
private JScrollBar scrollBar;
private JPopupMenu popupMenu;
private JLabel popupLabel;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new ScrollbarTest();
}
});
}
public ScrollbarTest()
{
JFrame frame = new JFrame("Test");
popupMenu = new JPopupMenu();
popupLabel = new JLabel();
popupMenu.add(popupLabel);
scrollPane = new JScrollPane(buildTestTextArea());
scrollBar = scrollPane.getVerticalScrollBar();
scrollBar.addMouseMotionListener(new PopUpMouseMotionListener());
scrollBar.addMouseListener(new PopUpMouseListener());
frame.add(scrollPane);
frame.setSize(new Dimension(400, 400));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public JTextArea buildTestTextArea()
{
JTextArea textArea = new JTextArea();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 10000; ++i)
{
builder.append("X");
}
textArea.setText(builder.toString());
textArea.setLineWrap(true);
return textArea;
}
private class PopUpMouseMotionListener extends MouseMotionAdapter
{
#Override
public void mouseDragged(MouseEvent e)
{
double value = scrollBar.getValue();
double max = scrollBar.getMaximum() - scrollBar.getVisibleAmount();
double h = scrollBar.getHeight();
popupLabel.setText("" + (int) (100*value/max) + "%");
popupMenu.show(scrollPane, scrollBar.getX() - popupMenu.getWidth() - 2, (int) ((h - popupMenu.getHeight())*value/max));
}
}
private class PopUpMouseListener extends MouseAdapter
{
#Override
public void mouseReleased(MouseEvent e)
{
popupMenu.setVisible(false);
}
}
}

Related

Replace button with a filled circle?

How to replace button shown on code with a filled circled.The circle (e.g. red) must move away from cursor just as same it was with button.
Is it another possible way to implement this type of task?
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class NewSwingTemplate {
private Random d = new Random();
public NewSwingTemplate() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add(createButton());
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JButton createButton() {
final JButton button = new JButton("Button");
button.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
int a = d.nextInt(200);
int b = d.nextInt(200);
button.setLocation(a, b);
}
});
return button;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new NewSwingTemplate();
}
});
}
}
Create your own button.
That is, you derive from JComponent and overwrite the paint method. It needs to render your circle - the way you want it. Add some keyboard/mouse handling and maybe an ActionListener to the circle can react similar to a JButton.
Finally you use it just like a JButton - I guess you have the code that makes your button move somewhere already.

Change properties of a group of JButtons at once

Hey I'm doing a calculator app with java swing (A clone of windows calculator ;) )
As it is a calculator, it has a lot of JButtons with same properties. So my question is can I change the common properties of a group JButtons at once, based on 'DRY'. If possible it will help me a lot...
A simple subclass of JButton should help, like this example - a modified version of what I saw from another question below:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
#SuppressWarnings("serial")
public class MyButton extends JButton {
private Color circleColor = Color.BLACK;
public MyButton(String label) {
super(label);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension originalSize = super.getPreferredSize();
int gap = (int) (originalSize.height * 0.2);
int x = originalSize.width + gap;
int y = gap;
int diameter = originalSize.height - (gap * 2);
g.setColor(circleColor);
g.fillOval(x, y, diameter, diameter);
}
#Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width += size.height;
return size;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new MyButton("Custom Button"));
frame.setVisible(true);
}
}
If you want more info, research:
Creating a custom JButton in Java,
which is the cited reference for the example above. Of course, you can code the properties to your liking with the assistance of the JButton Javadoc: https://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html.
But for modifying the custom button after instantiation:
Make sure to add a static ArrayList that contains the instantiated buttons, as well as means to add a constructed button to the arraylist:
public static ArrayList<MyButton> myButtons = new ArrayList<MyButton>();
public MyButton(){
//...
myButtons.add(this);
}
And for modification of the properties/attributes of all buttons (say for example, whether they are visible), do:
public void setVisibleAll(boolean b){
for(MyButton x: myButtons){
x.setVisible(b);
}
}
Not only does this apply to buttons, it should apply to other swing components, like a JLayeredPane designed to act like a button:
package source_code.view.components.buttons;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.SwingConstants;
import source_code.controllers.ButtonListener350x40;
import source_code.plus_functions.Texture;
#SuppressWarnings("serial")
public class Button350x40 extends JLayeredPane {
private JLabel buttonBg = new JLabel();
private JLabel buttonText = new JLabel();
private ButtonListener350x40 listener = new ButtonListener350x40();
private static ArrayList<Button350x40> buttons = new ArrayList<Button350x40>();
private String id;
public Button350x40(String title, String id) {
//Button Proper
super();
super.setSize(350, 40);
super.setLayout(null);
super.addMouseListener(listener);
//Button BG
buttonBg.setIcon(new ImageIcon(Texture.textures.get("button_350x40")));
super.setLayer(buttonBg, 0);
buttonBg.setBounds(0, 0, 350, 40);
super.add(buttonBg);
//Button Text
buttonText.setText(title);
buttonText.setForeground(Color.BLACK);
super.setLayer(buttonText, 1);
buttonText.setHorizontalAlignment(SwingConstants.CENTER);
buttonText.setFont(new Font("Arial", Font.PLAIN, 20));
buttonText.setBounds(0, 0, 350, 40);
super.add(buttonText);
this.id = id;
buttons.add(this);
}
public String getId() {
return id;
}
public static ArrayList<Button350x40> getButtons() {
return buttons;
}
public JLabel getButtonBg() {
return buttonBg;
}
public JLabel getButtonText() {
return buttonText;
}
public ButtonListener350x40 getListener() {
return listener;
}
}
So TLDR, create a separate class for the custom button, have a static array of created buttons, and to modify all of the buttons, create a method that loops through the array of buttons and sets their respective attributes.

Clicking a button within a JFrame passes an data to a JPanel

I have a Jframe with two buttons: '1' and '2'. Clicking the button '1' should display the capital letter A in the JPanel.
Code fore my JFrame:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawFrame extends JFrame{
private final int WIDTH = 500;
private final int HEIGHT = 300;
private JButton number1;
private JButton number2;
private JPanel numberPanel;
private DrawPanel graphicsPanel;
public DrawFrame()
{
createSelectionPanel();
createGraphicsPanel();
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void createSelectionPanel()
{
numberPanel = new JPanel();
number1 = new JButton("1");
number2 = new JButton("2");
numberPanel.setLayout(new GridLayout(2,2));
numberPanel.add(number1);
numberPanel.add(number2);
this.add(numberPanel, BorderLayout.WEST);
}
private void createGraphicsPanel()
{
//instantiate drawing panel
graphicsPanel = new DrawPanel();
//add drawing panel to right
add(graphicsPanel);
}
private class Number1ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
Number number = new Number();
number.setNumber('A');
}
}
//creates a drawing frame
public static void main(String[] args)
{
DrawFrame draw = new DrawFrame();
}
}
Code for my JPanel
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class DrawPanel extends JPanel{
public Coordinates current;
public DrawPanel(){
//nothing drawn initially
current = null;
//set white background for drawing panel
setBackground(Color.WHITE);
//add mouse listeners
MouseHandler mouseHandler = new MouseHandler();
this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);
}
public void paint(Graphics g){
super.paint(g);
if(current!=null){
I want to replace "A" with number.getNumber()
g.drawString("A", current.getX(), current.getY());
}
}
//class to handle all mouse events
private class MouseHandler extends MouseAdapter implements MouseMotionListener
{
public void mousePressed(MouseEvent event)
{
current = new Coordinates(event.getX(), event.getY());
}
public void mouseReleased(MouseEvent event)
{
repaint();
}
}
}
I'm not sure if this is possible. So sorry if I am mistaken in my logic. Please provide an alternate way for me to approach this problem. Appreciate any guidance.
Thanks!
The Coordinates and Number classes weren't included, so I had to modify the code somewhat.
Here's the GUI I created.
The first thing I did was create a model class for the GUI. By creating a model class, I could make the display string and the drawing coordinate available to the view and the controller classes. This is a simple example of the model / view / controller pattern.
package com.ggl.drawing;
import java.awt.Point;
public class GUIModel {
private String displayString;
private Point coordinate;
public GUIModel(String displayString) {
this.displayString = displayString;
}
public Point getCoordinate() {
return coordinate;
}
public void setCoordinate(int x, int y) {
this.coordinate = new Point(x, y);
}
public void setCoordinate(Point coordinate) {
this.coordinate = coordinate;
}
public void setDisplayString(String displayString) {
this.displayString = displayString;
}
public String getDisplayString() {
return displayString;
}
}
Now that we have a model, lets look at the DrawFrame class.
package com.ggl.drawing;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DrawFrame implements Runnable {
private final int WIDTH = 500;
private final int HEIGHT = 300;
private JFrame frame;
private GUIModel model;
public DrawFrame() {
this.model = new GUIModel("A");
}
#Override
public void run() {
frame = new JFrame("Draw Letters");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createSelectionPanel(), BorderLayout.WEST);
frame.add(new DrawPanel(WIDTH, HEIGHT, model), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private JPanel createSelectionPanel() {
JPanel numberPanel = new JPanel();
ButtonListener listener = new ButtonListener();
JButton number1 = new JButton("A");
number1.addActionListener(listener);
JButton number2 = new JButton("B");
number2.addActionListener(listener);
numberPanel.setLayout(new GridLayout(0, 2));
numberPanel.add(number1);
numberPanel.add(number2);
return numberPanel;
}
private class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
model.setDisplayString(event.getActionCommand());
}
}
// creates a drawing frame
public static void main(String[] args) {
SwingUtilities.invokeLater(new DrawFrame());
}
}
I started the Java Swing application on the Event Dispatch thread with the call to the SwingUtilities invokeLater method.
I separated the JFrame construction from the 2 JPanels construction. I used a JFrame, rather than extend a JFrame. The only time you should extend any Java class is if you want to override one or more of the class methods.
I used the same ButtonListener for both JButtons. I'm guessing what you want, but I drew either an "A" or a "B", depending on which button you left clicked.
Let's look at the DrawPanel class.
package com.ggl.drawing;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
public class DrawPanel extends JPanel {
private static final long serialVersionUID = 3443814601865936618L;
private GUIModel model;
public DrawPanel(int width, int height, GUIModel model) {
this.setPreferredSize(new Dimension(width, height));
this.model = model;
// add mouse listeners
MouseHandler mouseHandler = new MouseHandler();
this.addMouseListener(mouseHandler);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (model.getCoordinate() != null) {
Point p = model.getCoordinate();
Font font = g.getFont().deriveFont(48F);
g.setFont(font);
g.drawString(model.getDisplayString(), p.x, p.y);
}
}
// class to handle all mouse events
private class MouseHandler extends MouseAdapter {
#Override
public void mousePressed(MouseEvent event) {
model.setCoordinate(event.getPoint());
}
#Override
public void mouseReleased(MouseEvent event) {
DrawPanel.this.repaint();
}
}
}
The major change I made in this class was to use the paintComponent method, rather than the paint method. The paintComponent method is the correct method to override.
I set the size of the drawing panel in the DrawPanel constructor. It's much better to let Swing figure out the size of the JFrame. That's what the pack method in the DrawFrame run method does.
I increased the font size so you can see the drawn letter better.
I removed the mouse motion listener code, as it wasn't needed.
I hope this was helpful to you.
OK, all I know so far is that you want the text displayed in a JPanel to change if a button is pressed. If so, then your code looks to be way too complex for the job. Suggestions include:
Give the DrawingPanel a setter method, say, setText(String text), that allows outside classes to change the text that it displays.
Within that method, set a field of DrawingPanel, say called text, and call repaint().
Override DrawingPanel's paintComponent not its paint method, and call the super's method within your override.
Within the paintComponent method, call drawString to draw the String held by the text field, if the field is not null.
Give your buttons ActionListeners or AbstractActions that call the DrawingPanel's setText(...) method, setting the text to be displayed.
For example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class DrawAorB extends JPanel {
private DrawingPanel drawingPanel = new DrawingPanel();
public DrawAorB() {
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 5));
btnPanel.add(new JButton(new ButtonAction("A")));
btnPanel.add(new JButton(new ButtonAction("B")));
setLayout(new BorderLayout());
add(drawingPanel, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
}
private class ButtonAction extends AbstractAction {
public ButtonAction(String name) {
super(name);
}
#Override
public void actionPerformed(ActionEvent e) {
String text = e.getActionCommand();
drawingPanel.setText(text);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("DrawAorB");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new DrawAorB());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class DrawingPanel extends JPanel {
private static final int PREF_W = 200;
private static final int PREF_H = PREF_W;
private String text = null;
public void setText(String text) {
this.text = text; // set the JPanel's text
repaint(); // and draw it
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (text != null) {
int x = getWidth() / 2;
int y = getHeight() / 2;
// use FontMetrics if you want to center text better
g.drawString(text, x, y);
}
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}
Even simpler, easier, and probably better would be to display the text within a JLabel as it's much easier to center this text.

How can I detect if a JScrollPane's scroll bar changes visibility?

I have JScrollPane that contains some images in a horizontal row. If the row of images is too long for the view port, the JScrollPane shows a scroll bar, reducing the height of the view port. I'd like to resize the images to fit the view port. How can I detect that the view port changed its size? Unfortunately, registering change event handlers doesn't seem to work.
Using change listener on the ViewPort seems to work for me. Here is a small demo of that:
import java.awt.BorderLayout;
import java.io.UnsupportedEncodingException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingWorker;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
public static void main(String[] args) throws UnsupportedEncodingException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
final JPanel buttons = new JPanel();
final JScrollPane pane = new JScrollPane(buttons);
pane.getViewport().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
System.err.println("Change in " + e.getSource());
System.err.println("Vertical visible? " + pane.getVerticalScrollBar().isVisible());
System.err.println("Horizontal visible? " + pane.getHorizontalScrollBar().isVisible());
}
});
panel.add(pane);
frame.setContentPane(panel);
frame.setSize(300, 200);
frame.setVisible(true);
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
#Override
protected Void doInBackground() throws Exception {
for (int i = 0; i < 10; i++) {
Thread.sleep(800);
buttons.add(new JButton("Hello " + i));
buttons.revalidate();
}
return null;
}
};
worker.execute();
}
}
I used the HierarchyBoundsListener to detect changes in the JScrollPane's size:
onAncestorResized(scroll, evt -> resizeYourImages());
Here's a convenience method for attaching the listener:
static void onAncestorResized(JComponent component, Consumer<HierarchyEvent> callback) {
component.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
#Override
public void ancestorResized(HierarchyEvent e) {
callback.accept(e);
}
});
}

Center JDialog over JPanel on JTabbedPane

I have tried all of the suggestions I found here and on other sites.
I can't seem to get this JDialog to be centered over the panel on the JTabbedPane.
Please note, I must have the close button disabled, so I can not use the standard JOptionPane.showDialogXYZ() methods.
Any ideas?
import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
public class CenterDialog extends JFrame
{
public CenterDialog()
{
setResizable(false);
setName(getClass().getSimpleName());
setTitle("My Frame");
setSize(300, 300);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
// Add the panel
tabbedPane.addTab("Button panel", new MyButtonPanel());
add(tabbedPane, BorderLayout.CENTER);
getContentPane().add(tabbedPane);
}
private class MyButtonPanel extends JPanel
{
public MyButtonPanel()
{
JButton btnShowDialog = new JButton("Show Dialog");
btnShowDialog.addActionListener(new BtnShowDialogActionListener());
add(btnShowDialog);
}
private class BtnShowDialogActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// TODO: Figure out how to center this dialog box
final String YES = "Yup";
final String NO = "Nope";
final Object[] options = { YES, NO };
final JOptionPane optionPane = new JOptionPane("Is this centered.", JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION, null, options, NO);
Frame f = JOptionPane.getFrameForComponent(((JButton) e.getSource()).getParent());
final JDialog dialog = new JDialog(f, "Question", ModalityType.APPLICATION_MODAL);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.out.println("Ignoring close button");
}
});
optionPane.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent e)
{
String prop = e.getPropertyName();
if (dialog.isVisible() && (e.getSource() == optionPane))
{
if (prop.equals(JOptionPane.VALUE_PROPERTY))
{
dialog.setVisible(false);
}
}
}
});
dialog.pack();
dialog.setVisible(true);
}
}
}
private static void createAndShowGUI()
{
// Create and set up the window.
CenterDialog frame = new CenterDialog();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
createAndShowGUI();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
});
}
}
}
The method which centers a dialog relative to a given component (no manual calculation needed, handles component-to-screen coordinate mapping internally):
dialog.setLocationRelativeTo(someComponent);
Choose the component, depending on what exactly you want to achieve:
// center relative to the button
dialog.setLocationRelativeTo((Component) e.getSource());
// center relative to button's parent
dialog.setLocationRelativeTo(((Component) e.getSource()).getParent());
// center relative to the tabbedPane
JTabbedPane tabbed = // walk the parent chain until you reach it
dialog.setLocationRelativeTo(tabbed);
I got slightly closer to what you're after by setting tabbedPane as global and then dialog.setLocationRelativeTo(tabbedPane);
Edit: a more elaborate, and probably visually accurate, solution is to calculate the x, y coordinates of your JDialog, something like this:
int xDiff = (tabbedPane.getWidth() - dialog.getWidth()) / 2;
int x = tabbedPane.getX() + xDiff;
int yDiff = (tabbedPane.getHeight() - dialog.getHeight()) / 2;
int y = tabbedPane.getY() + yDiff;
dialog.setLocation(x, y);
If I'm honest, I didn't get it working perfectly, but there's my theory!

Categories