How to connect JLabels with a line on JPanel with MigLayout? - java

I searched a lot on google on this subject, but just can't come out with right solution. I tried "painting" with Graphics paintComponent and everything seems fine, but lines just doesn't appear on my JPanel.
Part of my code with JLabels created:
frame = new JFrame();
frame.setTitle("New family tree");
...
JPanel panel = new JPanel();
panel.setBackground(new Color(30, 144, 255));
frame.getContentPane().add(panel, BorderLayout.EAST);
panel.setLayout(new MigLayout("", "[]", "[][][][][][][][]"));
JButton newPersonButton = new JButton("New Person");
panel.add(newPersonButton, "cell 0 5");
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
scrollPane = new JScrollPane();
tabbedPane.addTab("Tree", null, scrollPane, null);
panel_1 = new JPanel();
scrollPane.setViewportView(panel_1);
panel_1.setLayout(new MigLayout("",
"[][][][][][][][][][][][][][][][][]",
"[][][][][][][][][][][][][][][][][][][][][]"));
final JLabel lblAddGreatgrandmother = new JLabel("Add Great-grandmother");
panel_1.add(lblAddGreatgrandmother, "cell 3 4,growx");
final JLabel lblAddGrandmother_1 = new JLabel("Add Grandmother");
panel_1.add(lblAddGrandmother_1, "cell 2 5");
Should I use painting? Or put JLabels in array list and use Point? I'll appriciate any help.
EDIT: Runnable example - http://pastebin.com/NFug1QA1

The problem with the java-sl.com/connector solution is that the connection itself becomes a component- ie needs layout, accepts events etc. I put together a solution for this years ago, which you can find in my sourceforge project. Specifically, see ConnectionPanel and Connection.
... Also, just in my experience, they kinds of connections are most useable when the layout manager is set to null and components are not restricted to being locked into position. But you will know what's best for your case.

Related

How to re-size JButton

I've bee teaching myself java and following along with the problems in the book. I'm trying to make a display for my calculator. In the example(I did not attach this) the buttons were a smaller size than what mine are and I can't figure out how to reformat them. I tried using the dimension class but it had no affect. Also, I can't get my text at the top of the calculator to align left.
Here is my code:
public class Calculator extends JFrame {
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(300, 300);
setLayout(new BorderLayout());
JPanel numberPanel = new JPanel();
add(numberPanel, BorderLayout.CENTER);
numberPanel.setLayout(new GridLayout(4, 3, 3, 3));
for(int i = 1; i < 10; i++) {
JButton button = new JButton(String.valueOf(i));
numberPanel.add(button);
}
JButton zero = new JButton("" + 0);
JButton dot = new JButton(".");
JButton clear = new JButton("C");
numberPanel.add(zero);
numberPanel.add(dot);
numberPanel.add(clear);
JPanel keyPanel = new JPanel();
add(keyPanel, BorderLayout.EAST);
keyPanel.setLayout(new GridLayout(4, 1, 3, 3));
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton times = new JButton("*");
JButton divide = new JButton("/");
keyPanel.add(plus);
keyPanel.add(minus);
keyPanel.add(times);
keyPanel.add(divide);
JPanel equalsPanel = new JPanel();
add(equalsPanel, BorderLayout.SOUTH);
equalsPanel.setLayout(new GridLayout(1, 1));
JButton equals = new JButton("=");
equalsPanel.add(equals);
JPanel textPanel = new JPanel();
add(textPanel, BorderLayout.NORTH);
JTextField inputBox = new JTextField("0.0");
inputBox.setHorizontalAlignment(JTextField.LEFT);
inputBox.setEditable(false);
Font font = new Font("MonoSpaced", Font.BOLD, 20);
inputBox.setFont(font);
textPanel.add(inputBox);
setVisible(true);
}
public static void main(String[] args) {
new Calculator();
}
}
Imports were left off for brevity
GridLayout will laugh at you when you try and set a dimension. It does respect preferred sizes. You should select a layout manager that will respect preferred sizes. Or you can simply pack() (after you add all your components) your frame instead of setSize() and all the components preferred sizes will kick in. (Disclaimer - because of GridLayout though, if you try and resize the frame after that, you components will resize again)
See more at How to use Layout Managers. For a quick view of which layout managers respect preferred sizes and which ones don't, have a look at this post.
A common approach is to nest panels with different layout managers also, as seen here
UPDATE
As mentioned preciously, you should just call pack on the frame instead of set size. With your current code, this would cause the frame to be very small because of the preferred sizes of the components. If you want the buttons to have a bigger preferred size, you can set the font to a bigger font and/or use button.setMargins(new Insets(w,x,y,x)); to make the margins bigger. But it is preferred to pack the frame.
I would recommend using the Window Builder add-on if you’re using Eclipse. This tool will help you with many aspects of Swing. Learn by doing.
WindowBuilder Dowload Link

How to fix JPanel with Grid that occupies full width of frame?

I'm working on a layout for a simple login screen. I am using GridLayout in order to manipulate elements, but have came across an issue, It occupies full frame width like this:
Where as I want it to be with a fixed position and width,height and be vertically, horizontally centered inside a frame, but not occupy it's full size.
Possibly make Username and Password labels occupy less space so text fields are actually closer to them. I tried setting main size of my panel like .setMaximumSize(Dimension (200, 150));
But it doesn't seem to work.
//Create the frame.
JFrame frame = new JFrame("Online Shop");
JPanel mainPanel = new JPanel();
JPanel usernamePanel = new JPanel();
JPanel passwordPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JButton loginButton = new JButton("Login");
loginButton.setMaximumSize(new Dimension(100, 50));
JTextField username = new JTextField();
JLabel usernameLabel = new JLabel("Username");
JPasswordField password = new JPasswordField();
JLabel passwordLabel = new JLabel("Password");
//Panel
frame.setContentPane(mainPanel);
usernamePanel.setLayout(new GridLayout(1,2));
usernamePanel.add(usernameLabel);
usernamePanel.add(username);
passwordPanel.setLayout(new GridLayout(1,2));
passwordPanel.add(passwordLabel);
passwordPanel.add(password);
mainPanel.setLayout(new GridLayout(3, 1));
mainPanel.add(usernamePanel);
mainPanel.add(passwordPanel);
mainPanel.add(loginButton);
//Event Listeners
frame.addWindowListener(new MyWindowListener());
loginButton.addActionListener(new MyActionListener());
//Sizes, Positioning
frame.setSize(720, 480);
frame.setLocationRelativeTo(null);
//Show Frame
frame.setVisible(true);
frame.setSize(720, 480);
Don't use the setSize() method.
Instead use:
frame.pack();
Also use:
JTextField username = new JTextField(10);
To give the text fields a preferred number of characters.
Now when the frame is made visible the components will be displayed at their preferred size and the frame will be sized appropriately to fit the components.
If you want a little extra space between the components and the frame then you can do:
mainPanel.setBorder( new EmptyBorder(20, 20, 20, 20) );

Adding JPanel to JFrame?

I'm trying to add a JLabel to a JPanel to a JFrame. I set the border for the JPanel, but all I see on the JFrame is a small black square in the center of my frame. Whatever I do I can't change the size or location of it. Please help.
Start main = new Start();
Random random = new Random();
JFrame mainFrame = new JFrame("MainFrame");
JPanel mainPanel = new JPanel();
JLabel welcomeLabel = new JLabel();
mainFrame.add(main);
mainFrame.setLayout(new GridBagLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setTitle(names[random.nextInt(names.length)]);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setSize(mainFrameX, mainFrameY);
mainFrame.setResizable(false);
mainFrame.setLocationRelativeTo(null);
mainFrame.add(mainPanel);
mainPanel.add(welcomeLabel);
mainPanel.setBorder(new LineBorder(Color.BLACK));
mainPanel.setSize(new Dimension(200, 200));
welcomeLabel.setFont(new Font("Verdana", 1, 20));
welcomeLabel.setLocation(100, 100);
main.start();
Suggestions:
You will want to read the tutorial, Laying out Components, as it will explain how to code with the Swing layout managers, and this information is essential to solve your current problem.
One caveat: I urge you to avoid the temptation to use the null layout as use of it will lead to creation of code that is very hard to maintain or upgrade.
Your JLabel, welcomeLabel, will of course need some text to be visible.
Don't set it's location via setLocation(...) but again use the layout managers to do the dirty work of placing and sizing your components.
You will also want to call pack() and setVisible(true) on your JFrame after adding all initial components.
Hovercraft is right (+1), make sure you understand how the layout managers are working.
The order in which you do things are important, especially when dealing with the top level containers...
Start main = new Start();
Random random = new Random();
JFrame mainFrame = new JFrame("MainFrame");
JPanel mainPanel = new JPanel();
JLabel welcomeLabel = new JLabel();
welcomeLabel.setFont(new Font("Verdana", 1, 20));
mainPanel.add(welcomeLabel);
mainPanel.setBorder(new LineBorder(Color.BLACK));
// Do this first
mainFrame.setLayout(new GridBagLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setTitle(names[random.nextInt(names.length)]);
// Add your components
mainFrame.add(main);
mainFrame.add(mainPanel);
// Prepare the window for showing, now you have some content.
mainFrame.setResizable(false);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setLocationRelativeTo(null);
main.start();
This will still only produce a small black square in the window, because the JLabel has no content and therefore it's preferred size is going to be (something like) 2x2 (because of the border).
Try adding some text to...
welcomeLabel.setText("Welcome");
And then see the difference

MiGLayout multiple components on border

I have a MigLayout that I'd like to add components to dynamically, while keeping two buttons at the bottom of the frame (because that's intuitive).
Firstly, I'd like to know if what I'm currently doing is the best way to go about it, and secondly how to get what I'm trying to do to actually work.
At the moment, I'm using the MigLayout's "grid" to position the dynamically-added components, and then using the MigLayout's "border" to position the fixed components but I can't get both buttons to sit on the south border next to one another.
According to the Quickstart PDF, this should be possible (and I quote, "you aren't confined to use only one component per side") but it doesn't go on to say how you achieve this.
Personnally I'd rather split my JFrame in 2 JPanels with a BorderLayout. Place the MigLayout form within a JPanel in the CENTER area, and the the buttons within a Box in the SOUTH area.
EDIT
With an example it even better ;-)
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
// == MigLayout Form ==
JPanel panelCenter = new JPanel();
panelCenter.setLayout(
new MigLayout(
new LC().wrapAfter(4),
new AC().size(":80:", 0).size("115:115:115", 1, 2, 3).align("right", 0, 2),
new AC().size("19:19:19")
));
panelCenter.setOpaque(false);
panelCenter.add(new JLabel("Label1"));
panelCenter.add(new JTextField(), new CC().growX());
panelCenter.add(new JLabel("Label2"));
panelCenter.add(new JTextField(), new CC().growX());
panelCenter.add(new JLabel("Label3"));
panelCenter.add(new JTextField(), new CC().growX());
panelCenter.add(new JLabel("Label4"));
panelCenter.add(new JTextField(), new CC().growX());
panelCenter.add(new JLabel("Label5"));
panelCenter.add(new JTextField(), new CC().growX());
panelCenter.add(new JLabel("Label6"));
panelCenter.add(new JTextField(), new CC().growX());
frame.add(panelCenter, BorderLayout.CENTER);
// == Buttons ==
Box southPanel = Box.createHorizontalBox();
southPanel.add(Box.createHorizontalGlue());
southPanel.add(new JButton("Ok"));
southPanel.add(new JButton("Cancel"));
southPanel.add(Box.createHorizontalGlue());
frame.add(southPanel, BorderLayout.SOUTH);
frame.setVisible(true);
frame.setSize(500, 150);
}
I did it like this:
create = new JButton("Create");
create.addActionListener(this);
mainPanel.add(create, "tag ok, span, split 2, sg btn");
cancel = new JButton("Cancel");
cancel.addActionListener(this);
mainPanel.add(cancel, "tag cancel, sg btn");
This is actually just the last row of my grid, but the key seems to be using span and split (sg just groups the sizes of the buttons, and tag positions them - lovely feature). I've found the example here (search "button bar").

Two JTables over each other

As a part for my first GUI App I want to show 2 Tables the following way:
http://i.stack.imgur.com/6mq0m.jpg
I´m unable to print the 2 tables that way!
Here is my code so far:
// Center
JPanel panel_center = new JPanel();
panel_overview.add(panel_center, BorderLayout.CENTER);
panel_center.setLayout(new BorderLayout());
JPanel panel_center_table = new JPanel();
panel_center.add(panel_center_table, BorderLayout.NORTH);
panel_center_table.setLayout(new GridLayout(2, 1));
JPanel panel_table_north = new JPanel();
panel_center_table.add(panel_table_north);
JPanel panel_table_south = new JPanel();
panel_center_table.add(panel_table_south);
JPanel panel_center_combobox = new JPanel();
panel_center.add(panel_center_combobox, BorderLayout.NORTH);
panel_center_combobox.setLayout(new BorderLayout());
panel_center_combobox.add(combobox_table_chooser, BorderLayout.WEST);
Consider using a different layout manager. I'd recommend using a BoxLayout, since you'll be able to easily stack components on top of each other. Here's a How To Use BoxLayout tutorial.
You could also try using a split pane. That way, the user can control the height of each of the tables.

Categories

Resources