About getting values from JList - java

I am trying to get values from a database via Jlist; but when I select a value of Jlist, no values return and the "Jtable" becomes empty instead of titles. That's my code for UI.
Thanks for your help...
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import model.Category;
import model.Person;
import service.AddressBookService;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JSplitPane;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.AbstractListModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
public class UserInterfaceMain extends JFrame {
private JPanel contentPane;
private JPanel panel;
private JButton btnNew;
private JSplitPane splitPane;
private JList list;
private JTable table;
private JScrollPane scrollPane;
private List<Category> categories = new ArrayList<>();
private List<Person> personList = new ArrayList<>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserInterfaceMain frame = new UserInterfaceMain();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public UserInterfaceMain() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 624, 395);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
btnNew = new JButton("NEW");
panel.add(btnNew);
splitPane = new JSplitPane();
contentPane.add(splitPane, BorderLayout.CENTER);
list = new JList();
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
do_list_valueChanged(arg0);
}
});
splitPane.setLeftComponent(list);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.EAST);
table = new JTable();
splitPane.setRightComponent(table);
scrollPane.setViewportView(table);
loadCategories();
}
public void loadCategories() {
categories = new AddressBookService().getAllCategories();
DefaultListModel<Category> listModel = new DefaultListModel<>();
for (int i = 0; i < categories.size(); i++) {
listModel.addElement(categories.get(i));
//listModel.addElement(categories.get(i).getName());
}
list.setModel(listModel);
}
public void loadPersonList() {
String[] columns = new String[] { "NAME", "LAST NAME", "E-MAIL", "CITY" };
Object[][] personData = new Object[personList.size()][];
for (int i = 0; i < personData.length; i++) {
personData[i] = new Object[] { personList.get(i).getName(), personList.get(i).getLastName(),
personList.get(i).getEmail(), personList.get(i).getCity() };
}
TableModel tableModel = new DefaultTableModel(personData, columns);
table.setModel(tableModel);
}
protected void do_list_valueChanged(ListSelectionEvent arg0) {
personList = new AddressBookService().getPersonsForTable(((Category)list.getSelectedValue()).getId());
loadPersonList();
System.out.println(personList.size());
}
}

I don't understand exactly what you want, but I modified your code to run it in this way:
Obviously I have all hardcoded in order to run your program. If your problem is that in the left side instead of the names of the categories appears the hash code of the object "com.mypackage.Category#12AAAF", you must modify your Category class and override the toString method.
#Override
public String toString() {
return getName();
}
If it isn't your problem, please add a comment describing it.

Related

How to create an unknown number of Swing components?

I need to display a user-inputted number of tabs (JTabbedPane) (with the same initial content, but the ability to individually change the content), is there a way to create these components procedurally so I don't have to know how many there will be before the user input?
Here is an example of change number of tabse depended on user input:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TabbedPaneTest3 implements Runnable {
private final JTabbedPane tabber = new JTabbedPane();
public static void main(String[] args) {
SwingUtilities.invokeLater(new TabbedPaneTest3());
}
#Override
public void run() {
final JSpinner spinner = new JSpinner(new SpinnerNumberModel(3, 1, 10, 1));
spinner.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
Object val = spinner.getValue();
if (val instanceof Number) {
updateTabsNumber((Number) val);
}
}
});
updateTabsNumber(3);
JFrame frm = new JFrame("Tab Pane");
JPanel panel = new JPanel();
panel.add(new JLabel("Number of tabs: "));
panel.add(spinner);
frm.add(panel, BorderLayout.NORTH);
frm.add(tabber);
frm.setSize(600, 400);
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
private void updateTabsNumber(Number val) {
int count = val.intValue();
tabber.removeAll();
for (int i = 0; i < count; i++) {
String text = "Tab " + (i + 1);
JTextArea area = new JTextArea(10, 30);
area.setText("Initial text for tab: " + (i + 1));
tabber.addTab(text, new JScrollPane(area));
}
}
}

getSelectedIndex() for JList always return -1 eventhough an item is selected

http://prntscr.com/9jhrwa "How the GUI looks"
public class Okno1 extends javax.swing.JFrame {
static Konto[]konto;
static DefaultListModel listModel;
static int indexKonta;
public Okno1() {
initComponents();
napolniKonto();
jScrollPane1.setVisible(false);
button_potrdiKonto.setVisible(false);
}
here I fill my array with Objects and add them to DefaultListModel, also I create a new list with the mentioned DefaultListModel
listModel=new DefaultListModel();
list_konto.setModel(listModel);
konto=new Konto[4];
konto[0]=new Konto("10000/20000", "Test konto primer1");
konto[1]=new Konto("20000/30000", "Test konto primer2");
konto[2]=new Konto("50000/60000", "Test konto primer3");
konto[3]=new Konto("30000/50000", "Test konto primer4");
for (int i = 0; i < konto.length; i++) {
listModel.addElement(konto[i].getID()+" | "+konto[i].getOpis());
}
list_konto=new JList(listModel);
jScrollPane1.repaint();
}
Here I show the jScrollPanel when this button is pressed, I also show the button which must be pressed if I want to get the index of the selected element in the JList displayed
private void button_prikaziKontoActionPerformed(java.awt.event.ActionEvent evt) {
jScrollPane1.setVisible(true);
button_potrdiKonto.setVisible(true);
//revalidate();
//repaint();
}
Here I press a button and it should get me the index of the selected item, but it keeps giving me -1 and it doesn't matter if an item on the JList is selected or is not
private void button_potrdiKontoActionPerformed(java.awt.event.ActionEvent evt) {
//indexKonta=list_konto.getSelectedIndex();
text_opisKonta.setText(Integer.toString(list_konto.getSelectedIndex()));
}
It's not clear where your code is going awry. This compete example may allow you to study the problem in isolation. Also consider adding a ListSelectionListener to see the effect.
myList.addListSelectionListener((ListSelectionEvent e) -> {
myLabel.setText(getSelectionIndex());
});
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
/** #see http://stackoverflow.com/a/34497773/230513 */
public class Test extends JPanel {
private final String[] values = {"Value1", "Value2", "Value3", "Value4"};
private final JList myList = new JList(values);
private final JLabel myLabel = new JLabel();
public Test() {
myList.setSelectedIndex(values.length - 1);
myLabel.setText(getSelectionIndex());
this.add(myList);
this.add(myLabel);
this.add(new JButton(new AbstractAction("Show Selected Index") {
#Override
public void actionPerformed(ActionEvent e) {
myLabel.setText(getSelectionIndex());
}
}));
}
private String getSelectionIndex() {
return String.valueOf(myList.getSelectedIndex());
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Test());
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
});
}
}
don't use static variables
always to test if (list.getSelectedIndex() > -1) {
use ListSelectionListener for JList, by always testing if (list.getSelectedIndex() > -1) {
for example (without using ListSelectionListener)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class JListAndSelection {
private JFrame frame = new JFrame();
private DefaultListModel listModel = new DefaultListModel();
private JList list = new JList(listModel);
private JScrollPane scrollPane = new JScrollPane(list);
private JLabel label = new JLabel("nothing is selected");
private JButton button1 = new JButton("print me selected value");
public JListAndSelection() {
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
if (list.getSelectedIndex() > -1) {
label.setText((String) list.getSelectedValue());
} else {
label.setText("nothing is selected");
}
}
});
listModel.addElement("10000/20000 - Test konto primer1");
listModel.addElement("20000/30000 - Test konto primer2");
listModel.addElement("50000/60000 - Test konto primer3");
listModel.addElement("30000/50000 - Test konto primer4");
list.setVisibleRowCount(5);
frame.setTitle("JFrame");
frame.add(label, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(button1, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new JListAndSelection();
});
}
}

Display selected row index from a list in another panel using SWING

I have a list in which I display elements. I want each time I select an element from the list to display its index inside another JPanel.
The code:
leftPanel.setLayout(new BoxLayout(leftPanel,BoxLayout.PAGE_AXIS));
leftList = new JList(listModel);
leftList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listScrollPane = new JScrollPane(leftList);
listScrollPane.setBorder(BorderFactory.createTitledBorder("Proteins"));
rightPanel.setBackground(Color.magenta);
rightPanel.setLayout(new FlowLayout());
for (String name : allNames) {
listModel.addElement(name);
}
leftList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent event) {
int nameIndex = leftList.getSelectedIndex();
JLabel msglabel = new JLabel("index = " + nameIndex, JLabel.CENTER);
rightPanel.add(msglabel);
}
});
This doesn't display anything in the JPanel
In this code when a element is selected it can be moved to another list by pressing a button check this code it will help you
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
public class Exp extends JFrame {
private JList rl;
private JList ll;
private JButton b;
private static String[] foods = { "asd", "asd", "dfg", "wer", "tyu" };
public Exp() {
super("Title");
setLayout(new FlowLayout());
ll = new JList(foods);
ll.setVisibleRowCount(3);
ll.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
ll.setFixedCellHeight(15);
ll.setFixedCellWidth(100);
add(new JScrollPane(ll));
b = new JButton("Move -->");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
rl.setListData(ll.getSelectedValues());
}
});
add(b);
rl = new JList();
rl.setVisibleRowCount(3);
rl.setFixedCellHeight(15);
rl.setFixedCellWidth(100);
rl.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(rl));
}
}

Create JInternalFrame with 3 JPanels with variable size and TextFields

I'm trying to make a window with an internal window, using a JSpinner to set integer values, a button to generate the JInternalFrame, passing JSpinner value as parameter.
This JInternalFrame will contain 3 JPannels, where they must have colmuns and rows equals to the value of JSpinner.
So, I thought of using an ArrayList of JFormatedTextFields, and a for to add TextFields to the ArrayList, with a class I've create previously to create JFormatedTextFields with one line, but I don't know how to use the for to add values.
There's the code:
JDialog:
package br.edu.faculdadedosguararapes;
import java.awt.EventQueue;
import javax.swing.JDialog;
import javax.swing.JSpinner;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.beans.PropertyVetoException;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.JButton;
import javax.swing.SpinnerNumberModel;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Soma extends JDialog {
private JSpinner spinner;
private JPanel panelLinhasEColunas;
private JPanel panelTop;
private JScrollPane scrollPanePrincipal;
private JButton btnGerarMatrizes;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Soma dialog = new Soma();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the dialog.
* #throws PropertyVetoException
*/
public Soma() throws PropertyVetoException {
setTitle("Soma de Matrizes");
setModalityType(ModalityType.APPLICATION_MODAL);
setBounds(100, 100, 573, 447);
panelTop = new JPanel();
getContentPane().add(panelTop, BorderLayout.NORTH);
panelTop.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
panelLinhasEColunas = new JPanel();
panelLinhasEColunas.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Numero de Colunas e Linhas", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(51, 51, 51)));
panelTop.add(panelLinhasEColunas);
spinner = new JSpinner();
spinner.setModel(new SpinnerNumberModel(2, 2, 10, 1));
panelLinhasEColunas.add(spinner);
btnGerarMatrizes = new JButton("Gerar Matrizes");
btnGerarMatrizes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int valor = (Integer) spinner.getValue();
GerarMatrizes soma = new GerarMatrizes();
soma.GerarSoma(valor, scrollPanePrincipal, btnGerarMatrizes);
btnGerarMatrizes.setEnabled(false);
}
});
panelLinhasEColunas.add(btnGerarMatrizes);
scrollPanePrincipal = new JScrollPane();
getContentPane().add(scrollPanePrincipal, BorderLayout.CENTER);
}
}
Class to create JInternalFrame:
package br.edu.faculdadedosguararapes;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
public class GerarMatrizes {
public void GerarSoma(int valor, JScrollPane pane, final JButton botao){
ArrayList<JFormattedTextField> campos = new ArrayList<JFormattedTextField>();
JInternalFrame internalFrame = new JInternalFrame("");
internalFrame.addInternalFrameListener(new InternalFrameAdapter() {
#Override
public void internalFrameClosed(InternalFrameEvent arg0) {
botao.setEnabled(true);
}
});
internalFrame.setClosable(true);
internalFrame.setBorder(null);
internalFrame.setVisible(true);
pane.setViewportView(internalFrame);
Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
internalFrame.getContentPane().add(rigidArea, BorderLayout.NORTH);
Component rigidArea_1 = Box.createRigidArea(new Dimension(20, 20));
internalFrame.getContentPane().add(rigidArea_1, BorderLayout.SOUTH);
JPanel panel = new JPanel();
internalFrame.getContentPane().add(panel, BorderLayout.CENTER);
JPanel panelMatrizA = new JPanel();
panelMatrizA.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Matriz A", TitledBorder.CENTER, TitledBorder.TOP, null, null));
panel.add(panelMatrizA);
panelMatrizA.setLayout(new GridLayout(valor, valor, 5, 5));
Component horizontalStrut = Box.createHorizontalStrut(20);
panel.add(horizontalStrut);
JPanel panelMatrizB = new JPanel();
panelMatrizB.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Matriz B", TitledBorder.CENTER, TitledBorder.TOP, null, null));
panel.add(panelMatrizB);
panelMatrizB.setLayout(new GridLayout(valor, valor, 5, 5));
Component horizontalStrut_1 = Box.createHorizontalStrut(20);
panel.add(horizontalStrut_1);
JButton btnSomar = new JButton("Somar");
panel.add(btnSomar);
JPanel panelMatrizRes = new JPanel();
panelMatrizRes.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Matriz Resultante", TitledBorder.CENTER, TitledBorder.TOP, null, null));
panel.add(panelMatrizRes);
panelMatrizRes.setLayout(new GridLayout(valor, valor, 5, 5));
/*for (int i=1; i < valor; i++){
campos.add(i);
}*/
}
}
And class to create JFormattedTextFields:
package br.edu.faculdadedosguararapes;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.text.AbstractDocument;
import javax.swing.text.DocumentFilter;
public class GeraObjeto {
public JFormattedTextField novoFtf(JFormattedTextField nome, JPanel panel){
nome = new JFormattedTextField();
nome.setHorizontalAlignment(SwingConstants.CENTER);
nome.setColumns(2);
DocumentFilter filtro = new FiltroNumero();
((AbstractDocument) nome.getDocument()).setDocumentFilter(filtro);
panel.add(nome);
return nome;
}
}
Well, I've changed
ArrayList<JFormattedTextField> campos = new ArrayList<JFormattedTextField>();
to
JFormattedTextField[] camposA = new JFormattedTextField[valor*valor];
GeraObjeto textField = new GeraObjeto();
so with a simple for using the class to create JFormattedTextFields (GeraObjeto) like
for (int i=0; i < valor*valor; i++){
camposA[i] = textField.novoFtf(camposA[i], panelMatrizA);
}
it's working well.

Multiple JPanels inside JDialog, how to control panel re-sizing order?

When having multiple JPanels laid out with BorderLayout, what decides which panel should keep its size on the cost of other panels when the containing dialog is re-sized?
In the sample code below, I would like the south panel, containing a JComboBox and JCheckBoxes, to keep its size when the dialog is re-sized.
Original Size
Re-Sized, south panel messed up
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JCheckBox;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JList;
import javax.swing.border.TitledBorder;
import javax.swing.BoxLayout;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class GrantPrivilegesDialog extends JDialog {
private JTable queueTable;
private JComboBox groupComboBox;
private JCheckBox receiveCheckBox;
private JCheckBox sendCheckBox;
private JCheckBox browseCheckBox;
private JCheckBox viewCheckBox;
private JCheckBox createCheckBox;
private JCheckBox deleteCheckBox;
private JCheckBox modifyCheckBox;
private JCheckBox purgeCheckBox;
private TableRowSorter<DefaultTableModel> queueSorter;
private DefaultTableModel queueModel;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
GrantPrivilegesDialog dialog = new GrantPrivilegesDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public GrantPrivilegesDialog() {
setTitle("Grant Privileges To Group(s)");
setBounds(100, 100, 866, 461);
getContentPane().setLayout(new BorderLayout());
{
JPanel northPanel = new JPanel();
northPanel.setLayout(new BorderLayout());
getContentPane().add(northPanel, BorderLayout.NORTH);
{
queueTable = new JTable();
//Object[][] rows = null;
Object[][] rows ={ {"*", "localhost1", "someQueue1"},
{"", "localhost2", "someQueue2"},
{"", "localhost3", "someQueue3"},
{"", "localhost4", "someQueue4"},
{"", "localhost5", "someQueue5"},
{"", "localhost6", "someQueue6.sdfsdfssdf.sfsdfsdfpweerpowerpoiwer.werjsdgf.sdföw.slksdfsdf"}
};
String[] queueColumnNames = {"","Server","QueueName"};
queueModel = new DefaultTableModel(rows, queueColumnNames){
#Override
public Class getColumnClass(int colNum) {
switch (colNum) {
case 0:
return String.class; //Asterisk (*) for dynamic
case 1:
return String.class; //Server
case 2:
return String.class; //Queue Name
default:
return String.class;
}
}
#Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
queueTable.setModel(queueModel);
queueSorter = new TableRowSorter<DefaultTableModel>(queueModel);
queueTable.setRowSorter(queueSorter);
queueTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
queueTable.getColumnModel().getColumn(0).setPreferredWidth(2);
queueTable.getColumnModel().getColumn(0).setMaxWidth(2);
queueTable.getColumnModel().getColumn(1).setPreferredWidth(225);
queueTable.getColumnModel().getColumn(2).setPreferredWidth(600);
queueTable.setCellSelectionEnabled(true);
JScrollPane queueScrollPane = new JScrollPane(queueTable);
northPanel.add(queueScrollPane, BorderLayout.CENTER);
queueScrollPane.setViewportBorder(new TitledBorder(null, "Queues", TitledBorder.LEADING, TitledBorder.TOP, null, null));
{
queueScrollPane.setViewportView(queueTable);
}
}
}
{
JPanel southPanel = new JPanel();
getContentPane().add(southPanel, BorderLayout.SOUTH);
southPanel.setLayout(new BorderLayout());
{
{
//JList<? extends E> list = new JList();
//panel_1.add(list, BorderLayout.NORTH);
}
}
{
JPanel privilegesPanel = new JPanel();
privilegesPanel.setBorder(BorderFactory.createTitledBorder("Grant"));
southPanel.add(privilegesPanel, BorderLayout.CENTER);
privilegesPanel.setLayout(new BorderLayout());
{
JPanel groupListPanel = new JPanel();
groupListPanel.setBorder(BorderFactory.createTitledBorder("Select Group"));
privilegesPanel.add(groupListPanel, BorderLayout.WEST);
groupListPanel.setLayout(new BorderLayout());
{
String[] choices = {"GroupA", "GroupB", "GroupC", "GroupD", "GroupE", "GroupE", "GroupE", "GroupE", "GroupE", "GroupE", "GroupE", "GroupE"};
groupComboBox = new JComboBox(choices);
groupComboBox.setEditable(true);
groupComboBox.setPreferredSize(new Dimension(300,10));
groupComboBox.setSelectedIndex(4);
groupListPanel.add(groupComboBox, BorderLayout.EAST);
}
}
{
JPanel privilegesCheckBoxPanel = new JPanel();
privilegesCheckBoxPanel.setBorder(BorderFactory.createTitledBorder("Privileges"));
privilegesPanel.add(privilegesCheckBoxPanel, BorderLayout.EAST);
privilegesCheckBoxPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
{
receiveCheckBox = new JCheckBox("receive");
receiveCheckBox.setSelected(true);
privilegesCheckBoxPanel.add(receiveCheckBox);
}
{
sendCheckBox = new JCheckBox("send");
sendCheckBox.setSelected(true);
privilegesCheckBoxPanel.add(sendCheckBox);
}
{
browseCheckBox = new JCheckBox("browse");
browseCheckBox.setSelected(true);
privilegesCheckBoxPanel.add(browseCheckBox);
}
{
viewCheckBox = new JCheckBox("view");
privilegesCheckBoxPanel.add(viewCheckBox);
}
{
createCheckBox = new JCheckBox("create");
privilegesCheckBoxPanel.add(createCheckBox);
}
{
deleteCheckBox = new JCheckBox("delete");
privilegesCheckBoxPanel.add(deleteCheckBox);
}
{
modifyCheckBox = new JCheckBox("modify");
privilegesCheckBoxPanel.add(modifyCheckBox);
}
{
purgeCheckBox = new JCheckBox("purge");
privilegesCheckBoxPanel.add(purgeCheckBox);
}
}
}
{
JPanel confirmPane = new JPanel();
southPanel.add(confirmPane, BorderLayout.SOUTH);
confirmPane.setLayout(new BorderLayout(0, 0));
{
JPanel panel = new JPanel();
confirmPane.add(panel, BorderLayout.EAST);
{
JButton grantButton = new JButton("GRANT");
panel.add(grantButton);
grantButton.setActionCommand("GRANT");
getRootPane().setDefaultButton(grantButton);
grantButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(receiveCheckBox.isSelected());
System.out.println(sendCheckBox.isSelected());
System.out.println(browseCheckBox.isSelected());
System.out.println(viewCheckBox.isSelected());
System.out.println(createCheckBox.isSelected());
System.out.println(deleteCheckBox.isSelected());
System.out.println(modifyCheckBox.isSelected());
System.out.println(purgeCheckBox.isSelected());
}
} );
}
{
JButton cancelButton = new JButton("CLOSE");
panel.add(cancelButton);
cancelButton.setActionCommand("CLOSE");
}
}
{
JLabel errorMessageLabel = new JLabel("");
confirmPane.add(errorMessageLabel, BorderLayout.WEST);
}
}
}
setMinimumSize(new Dimension(870, 300));
pack();
}
}
Change getContentPane().add(northPanel, BorderLayout.NORTH); to getContentPane().add(northPanel, BorderLayout.CENTER); and panel with table will be resizable and second panel will be with fixed size:
Also read about BorderLayout. I recommend you to use another layout manager, for example GridBagLayout, because it is more suitable for complex form.

Categories

Resources