File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes help!!!a problem about java gui Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "help!!!a problem about java gui" Watch "help!!!a problem about java gui" New topic
Author

help!!!a problem about java gui

Sean Gu
Greenhorn

Joined: Oct 05, 2007
Posts: 2
hi, everyone, i'm really a java beginner, and i'm trying to write down a pretty simple java gui. could anyone tell me that how to implement the correspondance between an element of a list and a cell in a table, i.e., if i choose "A1" in the list first, the "A1" cell in the table will be selected correspondingly; conversely, if i make a click on the "A1" cell in the table, the "A1" element will show up in the list. here's my codes:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.TableModel;

public class SimpleSheet {

JList list;
JTable table;

public static void main(String[] args) {

SimpleSheet gui = new SimpleSheet();
gui.go();

}

public void go() {

JFrame frame = new JFrame("SimpleSheet");

JPanel verPanel = new JPanel();
verPanel.setLayout(new BoxLayout(verPanel, BoxLayout.Y_AXIS));

JPanel horPanel = new JPanel();
horPanel.setLayout(new BoxLayout(horPanel, BoxLayout.X_AXIS));

JLabel label1 = new JLabel("0");
JLabel label2 = new JLabel("1");
JLabel label3 = new JLabel("2");
JLabel label4 = new JLabel("3");
JLabel label5 = new JLabel("4");
JLabel label6 = new JLabel("5");
JLabel label7 = new JLabel("6");
JLabel label8 = new JLabel("7");
JLabel label9 = new JLabel("8");
JLabel label10 = new JLabel("9");
JLabel label11 = new JLabel("10");

verPanel.add(label1);
verPanel.add(label2);
verPanel.add(label3);
verPanel.add(label4);
verPanel.add(label5);
verPanel.add(label6);
verPanel.add(label7);
verPanel.add(label8);
verPanel.add(label9);
verPanel.add(label10);
verPanel.add(label11);

String[] listEntries = {"A1", "B1", "C1", "D1", "E1", "A2", "B2", "C2", "D2", "E2", "A3", "B3", "C3", "D3", "E3", "A4", "B4", "C4", "D4", "E4", "A5", "B5", "C5", "D5", "E5", "A6", "B6", "C6", "D6", "E6", "A7", "B7", "C7", "D7", "E7", "A8", "B8", "C8", "D8", "E8", "A9", "B9", "C9", "D9", "E9", "A10", "B10", "C10", "D10", "E10"};
list = new JList(listEntries);

JScrollPane listScroller = new JScrollPane(list);
listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
listScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

horPanel.add(listScroller);

list.setVisibleRowCount(1);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


JTextField field = new JTextField("");

horPanel.add(field);

String[][] content = {{"1", "2", "1.5", "2.5", "1.3"}, {"2", "5", "3", "2.2", "8"}, {"", "", "", "", ""}, {"", "", "", "", ""}, {"", "", "", "", ""}, {"", "", "", "", ""}, {"", "", "", "", ""}, {"", "", "", "", ""}, {"", "", "", "", ""}, {"", "", "", "", ""}};
String[] caption = {"A", "B", "C", "D", "E"};

table = new JTable(content, caption);

JScrollPane tableScroller = new JScrollPane(table);

frame.getContentPane().add(BorderLayout.WEST, verPanel);
frame.getContentPane().add(BorderLayout.NORTH, horPanel);
frame.getContentPane().add(BorderLayout.CENTER, tableScroller);

frame.setSize(500, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);


}

}


thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18


Keep in mind though that this will probably create an infinite loop: the list selection will trigger the table selection, which will trigger the list selection, etc.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sean Gu
Greenhorn

Joined: Oct 05, 2007
Posts: 2
thanks a lot!Rob Prime
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help!!!a problem about java gui
 
Similar Threads
filewriter not working
filewriter not working
jmenubar,menuitem
ActionListener problem
Help designing a GUI