Created
August 25, 2016 13:52
-
-
Save riversun/5eb2459944ee160a3d9d0530c2d69c22 to your computer and use it in GitHub Desktop.
[Java]LinearLayout like component for Pure java GUI component like AWT/Swing.You can put the components of pure java's as Android's LinearLayout-like style.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2006-2016 Tom Misawa(riversun.org@gmail.com) | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
package org.riversun; | |
import java.awt.Color; | |
import java.awt.Component; | |
import java.awt.GridBagConstraints; | |
import java.awt.GridBagLayout; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
/** | |
* LinearLayout like component for Pure java GUI component like AWT/Swing<br> | |
* <br> | |
* You can put the components of a pure java as Android's LinearLayout-like | |
* style.<br> | |
* | |
* @author Tom Misawa (riversun.org@gmail.com) | |
*/ | |
@SuppressWarnings("serial") | |
public class JLinearLayout extends Component { | |
public enum Orientation { | |
HORIZONTAL, VERTICAL, | |
} | |
private JPanel mBasePanel = new JPanel(); | |
private Orientation mOrientation = Orientation.VERTICAL; | |
private List<ComponentHolder> mChildViewList = new ArrayList<ComponentHolder>(); | |
private class ComponentHolder { | |
public Component component; | |
public double weight; | |
} | |
public JLinearLayout() { | |
mBasePanel.setBackground(Color.black); | |
mBasePanel.setOpaque(true); | |
} | |
public JLinearLayout setChildOrientation(Orientation orientation) { | |
this.mOrientation = orientation; | |
return JLinearLayout.this; | |
} | |
/** | |
* Add view(component) to this LayoutGroup | |
* | |
* @param component | |
* @return | |
*/ | |
public JLinearLayout addView(Component component) { | |
addView(component, 1.0d); | |
return JLinearLayout.this; | |
} | |
/** | |
* Add view(component) to this LayoutGroup | |
* | |
* @param component | |
* @param weight | |
* @return | |
*/ | |
public JLinearLayout addView(Component component, double weight) { | |
final ComponentHolder compontentHolder = new ComponentHolder(); | |
compontentHolder.component = component; | |
compontentHolder.weight = weight; | |
mChildViewList.add(compontentHolder); | |
return JLinearLayout.this; | |
} | |
/** | |
* Set visible | |
* | |
* @param visible | |
* @return | |
*/ | |
public JLinearLayout setVisibility(boolean visible) { | |
mBasePanel.setVisible(visible); | |
return JLinearLayout.this; | |
} | |
public JLinearLayout insertToFrame(JFrame frame) { | |
frame.add(getAsPanel()); | |
return JLinearLayout.this; | |
} | |
/** | |
* Get this layout group as a JPanel | |
* | |
* @return | |
*/ | |
public JPanel getAsPanel() { | |
final int countOfChildObject = mChildViewList.size(); | |
final GridBagLayout layout = new GridBagLayout(); | |
if (mOrientation == Orientation.VERTICAL) { | |
mBasePanel.setLayout(layout); | |
} else { | |
mBasePanel.setLayout(layout); | |
} | |
for (int i = 0; i < countOfChildObject; i++) { | |
ComponentHolder childComponentHolder = mChildViewList.get(i); | |
final Component childComponent = childComponentHolder.component; | |
final double childComponentWeight = childComponentHolder.weight; | |
final GridBagConstraints gbc = new GridBagConstraints(); | |
if (mOrientation == Orientation.VERTICAL) { | |
gbc.gridx = 0; | |
gbc.gridy = i; | |
gbc.weightx = 1.0d; | |
gbc.weighty = childComponentWeight; | |
} else { | |
gbc.gridx = i; | |
gbc.gridy = 0; | |
gbc.weightx = childComponentWeight; | |
gbc.weighty = 1.0d; | |
} | |
gbc.gridwidth = 1; | |
gbc.gridheight = 1; | |
gbc.fill = GridBagConstraints.BOTH; | |
if (!(childComponent instanceof JLinearLayout)) { | |
// If child component is Swing or AWT component | |
layout.setConstraints(childComponent, gbc); | |
mBasePanel.add(childComponent); | |
} else { | |
// If child component is JLayoutGroup | |
final JLinearLayout childLayoutGroup = (JLinearLayout) childComponent; | |
gbc.gridwidth = 1; | |
gbc.gridheight = 1; | |
gbc.weightx = 1.0d; | |
gbc.weighty = 1.0d; | |
if (mOrientation == Orientation.VERTICAL) { | |
gbc.weighty = childComponentWeight;// childLayoutGroup.mWeight; | |
} else { | |
gbc.weightx = childComponentWeight;// childLayoutGroup.mWeight; | |
} | |
// Set weight to the panel that becomes base of the panel | |
layout.setConstraints(childLayoutGroup.mBasePanel, gbc); | |
JPanel childPanel = childLayoutGroup.getAsPanel(); | |
mBasePanel.add(childPanel); | |
} | |
} | |
return mBasePanel; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2006-2016 Tom Misawa(riversun.org@gmail.com) | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
package org.riversun; | |
import java.awt.Color; | |
import java.awt.event.WindowEvent; | |
import java.awt.event.WindowListener; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.UIManager; | |
import org.riversun.JLinearLayout.Orientation; | |
/** | |
* Example of JLayoutGroup | |
*/ | |
public class JLinearLayoutExample { | |
public static void main(String[] args) throws Exception { | |
System.setProperty("jsse.enableSNIExtension", "false"); | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
buildWindowUsingJLayoutGroup(); | |
} | |
public static void buildWindowUsingJLayoutGroup() { | |
// build upper area | |
// prepare LayoutGroup for upper area | |
final JLinearLayout upperArea = new JLinearLayout().setChildOrientation(Orientation.HORIZONTAL); | |
JButton bt1 = new JButton(); | |
bt1.setText("Button1"); | |
double bt1Wait = 0.6d; | |
JButton bt2 = new JButton(); | |
bt2.setText("Button2"); | |
double bt2Wait = 0.4d; | |
upperArea.addView(bt1, bt1Wait); | |
upperArea.addView(bt2, bt2Wait); | |
// prepare bottom area component | |
JPanel buttomArea = new JPanel(); | |
buttomArea.setBackground(Color.black); | |
// setWeight | |
final JLinearLayout parentLayoutGroup = new JLinearLayout().setChildOrientation(Orientation.VERTICAL); | |
parentLayoutGroup.addView(upperArea, 0.10d); | |
parentLayoutGroup.addView(buttomArea, 0.90d); | |
// show as window | |
JFrame frame = new JFrame(); | |
frame.setTitle("My Title"); | |
frame.addWindowListener(new MyWindowListener()); | |
frame.getContentPane().add(parentLayoutGroup.getAsPanel()); | |
frame.setSize(640, 480); | |
frame.setVisible(true); | |
} | |
static class MyWindowListener implements WindowListener { | |
@Override | |
public void windowActivated(WindowEvent e) { | |
} | |
@Override | |
public void windowClosed(WindowEvent e) { | |
} | |
@Override | |
public void windowClosing(WindowEvent e) { | |
System.exit(0); | |
} | |
@Override | |
public void windowDeactivated(WindowEvent e) { | |
} | |
@Override | |
public void windowDeiconified(WindowEvent e) { | |
} | |
@Override | |
public void windowIconified(WindowEvent e) { | |
} | |
@Override | |
public void windowOpened(WindowEvent e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment